2015-10-22 99 views
2

我想創建一個使用片段類的RelativeLayout,但是當我在populateListView函數中調用ListView setAdapter方法時,我有一個java.lang.NullPointerException,並且我找不到是什麼我的代碼錯了。謝謝!!NullPointerException:調用片段類中的setAdapter

我的片段類

public class ProductInFridge extends Fragment { 

private View rootView; 

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{ 


    rootView = inflater.inflate(R.layout.allproductinfridge, container, false); 

    populateListView(); 

    return rootView; 

} 

private void populateListView() { 

    ArrayAdapter<Product> adapter = new MyListAdapter(getActivity(),R.layout.item_view, populateList()); 
    ListView list = (ListView) rootView.findViewById(R.id.AllProdlistView); 
    list.setAdapter(adapter); 

} 

private List<Product> populateList(){ 

    List<Product> product = new ArrayList<Product>(); 


    product.add(new Product("Prod_1","",1,R.drawable.productt)); 
    product.add(new Product("Prod_2","",2,R.drawable.productt)); 
    product.add(new Product("Prod_3","24234234242",3,R.drawable.productt)); 

    return product; 
} 


private class MyListAdapter extends ArrayAdapter<Product> { 


    private List<Product> prod; 


    public MyListAdapter(Context context, int view, List<Product> passedProd) { 

     super(context, view , passedProd); 

     prod = passedProd; 

    } 

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

     View itemView; 




     itemView = getActivity().getLayoutInflater().inflate(R.layout.item_view,parent,false); 



     //Find the product 

     Product currentProd = prod.get(position); 



     //Fill the view 

     ImageView imageView = (ImageView)itemView.findViewById(R.id.imageView); 
     imageView.setImageResource(currentProd.getPhotoID()); 

     //Name 

     TextView nameTxt = (TextView)itemView.findViewById(R.id.item_txtName); 
     nameTxt.setText(currentProd.getName()); 

     //BarCode 

     TextView BarCodeTxt = (TextView)itemView.findViewById(R.id.item_txtBarCode); 
     BarCodeTxt.setText(currentProd.getBarCode()); 

     return itemView; 


    } 
} 
} 

這裏是allproductinfridge佈局我的XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#FFCC00" 
android:id="@+id/AllProd_layout"> 


<RelativeLayout 
    android:layout_width="200dp" 
    android:layout_height="match_parent" 
    android:background="#FFFFFFFF" 
    android:id="@+id/Category_list"> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/main_drawer" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     app:itemIconTint="@color/colorAccent" 
     app:itemTextColor="@color/colorTextSecondary" 
     app:menu="@menu/allproductmenu" /> 

</RelativeLayout> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/AllProductList" 
    android:layout_toRightOf="@+id/Category_list" 
    android:layout_toEndOf="@+id/Category_list"> 

    <ListView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/AllProdlistView" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" /> 

</RelativeLayout> 

而且item_view:

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

<ImageView 
    android:layout_width="120dp" 
    android:layout_height="120dp" 
    android:id="@+id/imageView" 
    android:src="@drawable/productt" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="Product Name" 
    android:id="@+id/item_txtName" 
    android:layout_marginLeft="45dp" 
    android:layout_marginStart="45dp" 
    android:layout_marginTop="30dp" 
    android:textSize="30sp" 
    android:layout_alignParentTop="true" 
    android:layout_toRightOf="@+id/imageView" 
    android:layout_toEndOf="@+id/imageView" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceSmall" 
    android:text="BarCode" 
    android:id="@+id/item_txtBarCode" 
    android:layout_below="@+id/item_txtName" 
    android:layout_alignLeft="@+id/item_txtName" 
    android:layout_alignStart="@+id/item_txtName" 
    android:layout_marginLeft="53dp" 
    android:layout_marginStart="53dp" /> 
</RelativeLayout> 

的Android的logcat :

01-02 01:20:21.410 3355-3355/slidenerd.vivz.navigationviewdemo E/AndroidRuntime: FATAL EXCEPTION: main 
01-02 01:20:21.410 3355-3355/slidenerd.vivz.navigationviewdemo E/AndroidRuntime: Process: slidenerd.vivz.navigationviewdemo, PID: 3355 
01-02 01:20:21.410 3355-3355/slidenerd.vivz.navigationviewdemo E/AndroidRuntime: java.lang.NullPointerException 
01-02 01:20:21.410 3355-3355/slidenerd.vivz.navigationviewdemo E/AndroidRuntime:  at slidenerd.vivz.navigationviewdemo.ProductInFridge.populateListView(ProductInFrid ge.java:44) 
01-02 01:20:21.410 3355-3355/slidenerd.vivz.navigationviewdemo E/AndroidRuntime:  at slidenerd.vivz.navigationviewdemo.ProductInFridge.onCreateView(ProductInFridge.java:34) 
01-02 01:20:21.410 3355-3355/slidenerd.vivz.navigationviewdemo E/AndroidRuntime:  at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789) 
+0

發佈您的logcat輸出.. –

回答

0

驗證onCreateView()方法。您呼叫populateListView()和比後你做

return inflater.inflate(R.layout.allproductinfridge, container, false); 

返回一個全新的充氣佈局你應該做的,而不是在你的onCreateView()。 首先膨脹視圖並將其保存在一個變量中。設置必要的適配器/偵聽器,然後返回視圖。

用於設置按鈕聽衆一個例子 - Button Listener for button in fragment in android

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     view = inflater.inflate(R.layout.smart_tv_controller_fragment, container, false); 
     upButton = (Button) view.findViewById(R.id.smart_tv_controller_framgment_up_button); 
     upButton.setOnClickListener(this); 
     return view; 
    } 

的視圖被第一膨脹。然後在充氣視圖內找到必要的組件(按鈕或列表視圖)並設置監聽器/適配器,然後返回視圖。

+0

非常感謝!它的工作! –

0

您必須虛增您的視圖,然後找到你的元素:

View rootView = inflater.inflate(R.layout.allproductinfridge, container, false); 
ListView list = (ListView) rootView.findById(R.id.AllProdlistView); 
//Now you can set your adapter 
+0

現在引發這個異常: ListView list =(ListView)rootView.findViewById(R.id.AllProdlistView); –

相關問題