2016-03-02 73 views
0

我正在一個學校的應用程序,我必須創建一個購物清單,當我選擇一個列表,我應該得到一個新的活動與列表上的產品。現在我的問題是,當我嘗試獲取listView中的單擊元素,然後將產品放在新活動的列表視圖中的列表中時,我在textview上獲得空指針異常。在android的textView空指針異常android

這是獲取異常

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

這是我的新的活動列表視圖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"> 

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

=======編輯解決空指針後=======

現在它打開新的活動,但它是空的,它不填充列表中的EW。我想知道我究竟在做錯什麼。

這是我的片段:

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

你可以上傳'ShoppingListAdapter'類嗎? –

+0

我在文章末尾添加了 –

+0

您應該可以執行類似'ShoppingList list = listAdapter .getItem(position)'的操作,以便在點擊的位置獲取列表。 –

回答

0

我認爲問題在於你想從佈局調用文本視圖

setContentView(R.layout.shopping_list_details); 

但你的片段使用佈局

final View rootView = inflater.inflate(R.layout.fragment1, container, false); 

可以解決此問題從主傳遞的TextView到您的片段或改變你的XML佈局

您可以測試,如果這是通過向fragment1佈局添加具有相同ID的textview爲true。

+0

我已經添加了一個帶有相同ID的textview到fragment1,它現在運行,但它不提取數據,視圖是空的 –

+0

你從哪裏開始你的片段? AppCompatActivity會看到它嗎? @AuroraBrignola – JeD

+0

我不認爲片段與此有關,但我可能是錯的。我從主要活動開始。 –

0

您指的是您的新Activity和您的Fragment中的相同ListView R.id.listView_shopping_lists,因此您的活動佈局文件中絕不會將shopping_list_details作爲目標,因此TextView爲空。

+0

真實的故事,我已經解決了這個問題,現在它沒有遇到任何錯誤,它打開新的活動,但空白,沒有數據在列表視圖 –

+0

如果活動啓動時沒有顯示數據,我會說你已經縮小問題給活動和適配器。我會使用日誌打印輸出或斷點來查看服務返回的數據(如果有),然後檢查它是否在適配器內正確設置。 – Mark

+0

@AuroraBrignola如果你已經在問題中修改了一些代碼,你能否對你的帖子做一個新的編輯來解釋新問題? –