NullPointerException:在Fragment Class中调用setAdapter

时间:2015-10-22 09:36:18

标签: android listview nullreferenceexception android-adapter

我尝试使用片段类创建RelativeLayout,但是当我在populateListView函数中调用ListView setAdapter方法时,我有一个java.lang.NullPointerException,我无法找到我的错误码。谢谢!

My Fragment Class

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","01234242143",1,R.drawable.productt));
    product.add(new Product("Prod_2","012313123112",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;


    }
}
}

这是我的xml文件,其中包含allproductinfridge布局:

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

2 个答案:

答案 0 :(得分:0)

您必须给视图充气,然后找到您的元素:

typedef void (^HandlerBlock)(void);

@interface MyClass : NSObject
@property (strong, nonatomic) HandlerBlock tapHandler;
@end

答案 1 :(得分:0)

验证你的onCreateView()方法。你正在调用populateListView(),然后通过执行

返回一个全新的膨胀布局
html, body {
height: 100%;
padding:0
margin:0;
}

.header {
 background: #4a90e2;
 height: 100px;
 position: fixed;
 width:100%;

 }


.left {

position: fixed;
height: 100%;
top: 100px;
background: #F44336;
width: 20%;

}

.content {

position: relative;
height: 100%;
top: 100px;
left: 20%;
background: #555;
width: 80%;
color:#fff;
min-height: 100%;
}

.bottom {
position: absolute;
bottom: 0px;
background: yellow;

}

你应该做的是在onCreateView()中。 首先膨胀视图并将其保存在变量中。设置必要的适配器/侦听器,然后返回视图。

设置按钮侦听器的示例 - Button Listener for button in fragment in android

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

视图首先要膨胀。然后在膨胀的视图中找到必要的组件(按钮或列表视图)并设置监听器/适配器,然后返回视图。