簡體   English   中英

Android中TextView上的空指針異常

[英]A null pointer exception on a textView in android

我正在為學校設計一個應用程序,其中我必須創建一個購物清單,當我選擇一個清單時,應該會使用該清單上的產品進行一項新的活動。 現在我的問題是,當我嘗試在listView中獲取clicked元素,然后將產品放在新活動的listview中的該列表上時,我在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> 

這是我的新活動中的listView:

<?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">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/shopping_list_details">
    </ListView>

</LinearLayout>

這是新活動的Java代碼:

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;
    }
}

這是一個例外:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
at com.gnirt69.slidingmenuexample.fragment.Fragment1$1.onItemClick(Fragment1.java:44)

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;
    }

=======求解空指針后進行編輯=======

現在,它打開了新活動,但它是空的,沒有填充列表視圖。 我正在嘗試找出問題出在哪里。

這是我的片段:

 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
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

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

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

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

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

    }

和適配器:

@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);

您可以通過將TextView從Main傳遞到Fragment或更改xml Layout來解決此問題。

您可以通過將具有相同id的textview添加到fragment1布局中來測試是否為真。

您在新的Activity和您的Fragment R.id.listView_shopping_lists是相同的ListView R.id.listView_shopping_lists ,您從未在包含每個項目的Activity布局文件中定位目標shopping_list_details ,因此TextView為null。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM