Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java android中textView上的空指针异常_Java_Android_Listview - Fatal编程技术网

Java android中textView上的空指针异常

Java android中textView上的空指针异常,java,android,listview,Java,Android,Listview,我正在为学校开发一个应用程序,在这个应用程序中,我必须列出一个购物清单,当我选择一个清单时,我应该会得到一个包含该清单上产品的新活动。现在我的问题是,当我尝试在listView中获取clicked元素,然后将产品放在新活动的listView中的该列表中时,我在textview中得到一个空指针异常 这是获取异常的textview <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://s

我正在为学校开发一个应用程序,在这个应用程序中,我必须列出一个购物清单,当我选择一个清单时,我应该会得到一个包含该清单上产品的新活动。现在我的问题是,当我尝试在listView中获取clicked元素,然后将产品放在新活动的listView中的该列表中时,我在textview中得到一个空指针异常

这是获取异常的textview

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:textSize="20dp"
        android:textColor="#236B8E"
        android:layout_marginTop="20dp"
        android:id="@+id/products_on_list"/>
</LinearLayout> 
这是我有购物清单的片段。这里是我创建新活动意图的地方:

public class DisplayShoppingListDetailsActivity extends AppCompatActivity{


 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //defines the activity layout
        setContentView(R.layout.shopping_list_details);

        ListView listView = (ListView) findViewById(R.id.listView_shopping_lists);

        Intent intent = getIntent();
        String name = intent.getStringExtra("list");

        ProductsOnListAdapter ad = new ProductsOnListAdapter(this, -1, Service.getService().getProductsOnList(name));
        listView.setAdapter(ad);

    }
}
public class Fragment1 extends Fragment {
    public Fragment1() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment1, container, false);

        final ListView listView = (ListView) rootView.findViewById(R.id.listView_shopping_lists);
        // get data from the table by the ListAdapter
        ShoppingListAdapter listAdapter = new ShoppingListAdapter(getActivity(), -1, Service.getService().getShoppingLists());
        listView.setAdapter(listAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                // selected item
                // HERE IS THE PROBLEM 
                // I have tried to use rootView instead of view
                TextView textview = (TextView) view.findViewById(R.id.products_on_list);
                String list = textview.getText().toString();

                // Launching new Activity on selecting single List Item
                Intent intent = new Intent(getActivity(), DisplayShoppingListDetailsActivity.class);
                // sending data to new activity
                intent.putExtra("list", list);
                startActivity(intent);

            }
        });

        //System.out.println("Test: " + Storage.getStorage().getShopingLists());
        return rootView;
    }
}
ShoppingListAdapter:

@Override
    public View getView(int position, View convertView, final ViewGroup parent){

        // Get the data item for this position
        //final ShoppingList list = getItem(position);
        ViewHolder holder;

        // Check if an existing view is being reused, otherwise inflate the view
        if (convertView == null) {
            convertView = View.inflate(getContext(), R.layout.items_lists_row, null);

            //set up the viewHolder
            holder = new ViewHolder();
            holder.shoppingListTextView = (TextView) convertView.findViewById(R.id.textView1);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        // get the reference to the specific item
        final ShoppingList list = getItem(position);
        //TextView tv = (TextView) convertView.findViewById(R.id.textView1);

        //manipulate the view
        holder.shoppingListTextView.setText(list.getName());

        return convertView;
    }

    public static class ViewHolder {
        private TextView shoppingListTextView;
    }
======解决空指针后编辑=======

现在它打开了新的活动,但它是空的,它不会填充listview。我在努力找出我到底哪里做错了什么

这是我的片段:

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment1, container, false);

        final ListView listView = (ListView) rootView.findViewById(R.id.listView_shopping_lists);
        // get data from the table by the ListAdapter
        final ShoppingListAdapter listAdapter = new ShoppingListAdapter(getActivity(), -1, Service.getService().getShoppingLists());
        listView.setAdapter(listAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                ShoppingList shoppingList = listAdapter.getItem(position);

                // selected item
                TextView textview = (TextView) rootView.findViewById(R.id.products_on_list);
                String list = textview.getText().toString();

                // Launching new Activity on selecting single List Item
                Intent intent = new Intent(getActivity(), DisplayShoppingListDetailsActivity.class);
                // sending data to new activity
                intent.putExtra("list", list);
                //System.out.println(shoppingList.getProductsOnList());
                startActivity(intent);

            }
        });

        //System.out.println("Test: " + Storage.getStorage().getShopingLists());
        return rootView;
    }
和适配器:

@Override
    public View getView(int position, View convertView, final ViewGroup parent){

        // Get the data item for this position
        //final ShoppingList list = getItem(position);
        ViewHolder holder;

        // Check if an existing view is being reused, otherwise inflate the view
        if (convertView == null) {
            convertView = View.inflate(getContext(), R.layout.shopping_list_details, null);

            //set up the viewHolder
            holder = new ViewHolder();
            holder.productsTextView = (TextView) convertView.findViewById(R.id.products_on_list);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        // get the reference to the specific item
        final ProductOnList product = getItem(position);
        //TextView tv = (TextView) convertView.findViewById(R.id.textView1);

        //manipulate the view
        holder.productsTextView.setText(product.toString());

        return convertView;
    }

    public static class ViewHolder {
        private TextView productsTextView;
    }

我认为问题在于您想从布局调用Textview

setContentView(R.layout.shopping_list_details);
但是你的片段使用了布局

final View rootView = inflater.inflate(R.layout.fragment1, container, false);
您可以通过将文本视图从主视图传递到片段或更改xml布局来解决此问题


您可以通过向fragment1布局添加具有相同id的textview来测试这是否正确。

您在新活动和片段中引用的是相同的ListView
R.id.ListView\u shopping\u列表
,您永远不会将包含每个项目的活动布局文件中的
shopping\u list\u details
作为目标,因此,文本视图为空。

您可以上传
ShoppingListAdapter
类吗?我已经在帖子末尾添加了它。您应该可以执行类似
ShoppingListList=listAdapter.getItem(position)的操作
以便在单击的位置获取列表。好的,然后如何将数据发送到新活动,因为如果我这样做:intent.putExtra(“列表”,shoppingList);它给了我一个错误。真实的故事,我已经修复了这个问题,现在它没有遇到任何错误,它打开了新的活动,但为空,列表视图中没有数据。如果活动启动时没有显示数据,我会说您已经将问题缩小到活动和适配器。我会使用日志打印输出或断点来查看您的服务返回了哪些数据(如果有),然后检查适配器中是否正确设置了这些数据。@AuroraBrignola如果您已经修复了问题中的代码,你能对你的帖子做一个新的编辑来解释这个新问题吗?@cricket_007完成了…我已经用一个打印输出测试了我的服务,里面的一切都很好,项目打印正确。我想问题出在我的片段中,我把购物清单传递给了intent,但当我尝试这样做时:intent.putExtra(“ShoppingList”),购物清单);它给出了一个强制转换错误。我已经在fragment1中添加了一个具有相同id的textview,它现在运行,但它没有获取数据,视图为空。您从哪里开始片段?AppCompative活动是否看到了它@AuroraBrignolaI认为碎片与此无关,但我可能错了。我从主要活动开始。
@Override
    public View getView(int position, View convertView, final ViewGroup parent){

        // Get the data item for this position
        //final ShoppingList list = getItem(position);
        ViewHolder holder;

        // Check if an existing view is being reused, otherwise inflate the view
        if (convertView == null) {
            convertView = View.inflate(getContext(), R.layout.shopping_list_details, null);

            //set up the viewHolder
            holder = new ViewHolder();
            holder.productsTextView = (TextView) convertView.findViewById(R.id.products_on_list);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        // get the reference to the specific item
        final ProductOnList product = getItem(position);
        //TextView tv = (TextView) convertView.findViewById(R.id.textView1);

        //manipulate the view
        holder.productsTextView.setText(product.toString());

        return convertView;
    }

    public static class ViewHolder {
        private TextView productsTextView;
    }
setContentView(R.layout.shopping_list_details);
final View rootView = inflater.inflate(R.layout.fragment1, container, false);