在android中的片段中的列表适配器中获取Null Pointer Exception

时间:2016-12-30 12:24:56

标签: android listview android-fragments nullpointerexception

我是Android的新手并在片段中处理自定义列表视图,我使用它是一个运行良好的活动,但是当我在片段中使用它时会说“空指针”,任何人都可以请帮助我明白了吗?

* ErroLog **

java.lang.NullPointerException
    at abc.test.com.abc.adapter.ListingAdapter.getView(ListingAdapter.java:50)
    at android.widget.AbsListView.obtainView(AbsListView.java:2758)
    at android.widget.ListView.makeAndAddView(ListView.java:1811)
    at android.widget.ListView.fillDown(ListView.java:697)
    at android.widget.ListView.fillFromTop(ListView.java:763)
    at android.widget.ListView.layoutChildren(ListView.java:1641)
    at android.widget.AbsListView.onLayout(AbsListView.java:2579)
    at android.view.View.layout(View.java:15630)
    at android.view.ViewGroup.layout(ViewGroup.java:4884)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
    at android.view.View.layout(View.java:15630)
    at android.view.ViewGroup.layout(ViewGroup.java:4884)
    at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1695)
    at android.view.View.layout(View.java:15630)
    at android.view.ViewGroup.layout(ViewGroup.java:4884)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
    at android.view.View.layout(View.java:15630)
    at android.view.ViewGroup.layout(ViewGroup.java:4884)
    at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:459)
    at android.view.View.layout(View.java:15630)
    at android.view.ViewGroup.layout(ViewGroup.java:4884)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
    at android.view.View.layout(View.java:15630)
    at android.view.ViewGroup.layout(ViewGroup.java:4884)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2350)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2063)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1251)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6557)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:813)
    at android.view.Choreographer.doCallbacks(Choreographer.java:613)
    at android.view.Choreographer.doFrame(Choreographer.java:583)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:799)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:146)
    at android.app.ActivityThread.main(ActivityThread.java:5653)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
    at dalvik.system.NativeStart.main(Native Method)

fragment.java

public class AllFragment extends Fragment {
    ListingAdapter adapter;
    String[] web = {
            "Pavel City Fire Brigade",
            "Lonavala fire Station",
            "Amravati Fire Base",
            "Chakan City Fire Brigade",
            "Talegaon Fire Base",
            "Thane Fire brigade",
            "Khar Fire Base",
            "Virar Fire Station",
            "Bhakala Fire Base",
    };
    int[] imageId = {
            R.drawable.img_emergency,
            R.drawable.img_emergency,
            R.drawable.img_emergency,
            R.drawable.img_emergency,
            R.drawable.img_emergency,
            R.drawable.img_emergency,
            R.drawable.img_emergency,
            R.drawable.img_emergency,
            R.drawable.img_emergency,
    };
    Context context = getActivity();
    ListView lv_listing;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_all, container, false);
        lv_listing = (ListView)rootView.findViewById(R.id.lv_listing);

        adapter = new ListingAdapter(context, web, imageId);
        lv_listing.setAdapter(adapter);

        return rootView;
    }
}

listAdapter.java

public class ListingAdapter extends BaseAdapter {
    private Context mContext;
    private final String[] web;

    public ListingAdapter(Context c, String[] web, int[] Imageid) {
        mContext = c;
        this.web = web;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return web.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View grid;
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {

            grid = new View(mContext);
            grid = inflater.inflate(R.layout.raw_tab, null);
            TextView tv_cat = (TextView) grid.findViewById(R.id.tv_cat);
            ImageView iv_pic = (ImageView)grid.findViewById(R.id.iv_pic);
            TextView tv_features = (TextView)grid.findViewById(R.id.tv_features);
            TextView tv_location = (TextView)grid.findViewById(R.id.tv_location);
            TextView tv_rating = (TextView)grid.findViewById(R.id.tv_rating);
            RatingBar rt_bar = (RatingBar)grid.findViewById(R.id.rt_fvrt);
            TextView tv_votes = (TextView)grid.findViewById(R.id.tv_votes);
            ImageView iv_call = (ImageView)grid.findViewById(R.id.iv_call);
            ImageView iv_share = (ImageView)grid.findViewById(R.id.iv_share);

            tv_cat.setText(web[position]);
        } else {
            grid = (View) convertView;
        }

        return grid;
    }
}

raw_tab

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_category"
    android:padding="5dp">


    <ImageView
        android:id="@+id/iv_pic"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentLeft="true"
        android:layout_margin="10dp"
        android:scaleType="fitXY"
        android:src="@drawable/img_entertainment" />

    <TextView
        android:id="@+id/tv_cat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_toLeftOf="@+id/iv_call"
        android:layout_toRightOf="@+id/iv_pic"
        android:text="Super mall,Panvel"
        android:textColor="@color/black"
        android:textSize="20dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tv_features"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_cat"
        android:layout_marginTop="2dp"
        android:layout_toRightOf="@+id/iv_pic"
        android:maxLines="2"
        android:text="Featurs:ICU,Heart Care,Cardiac,Emergency,Orthopedic"
        android:textColor="@color/grey_dark"
        android:textSize="13dp" />

    <ImageView
        android:id="@+id/iv_loca"
        android:layout_width="15dp"
        android:layout_height="15dp"
        android:layout_below="@+id/tv_features"
        android:layout_marginLeft="-2dp"
        android:layout_marginTop="2dp"
        android:layout_toRightOf="@+id/iv_pic"
        android:background="@drawable/ic_location" />

    <TextView
        android:id="@+id/tv_location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_features"
        android:layout_marginTop="2dp"
        android:layout_toRightOf="@+id/iv_loca"
        android:maxLines="2"
        android:text="Navi Mumbai"
        android:textColor="@color/grey_dark"
        android:textSize="13dp" />

    <RelativeLayout
        android:id="@+id/rl_bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_location"
        android:layout_marginLeft="-10dp"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@+id/iv_loca">

        <TextView
            android:id="@+id/tv_rating"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/bg_green"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:text="3.0"
            android:textColor="#fff"
            android:textSize="14dp" />

        <RatingBar
            android:id="@+id/rt_fvrt"
            style="@android:style/Widget.Holo.Light.RatingBar.Small"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_toRightOf="@+id/tv_rating"
            android:numStars="5" />

        <TextView
            android:id="@+id/tv_votes"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_toRightOf="@+id/rt_fvrt"
            android:text="2 Votes"
            android:textColor="@color/grey_dark"
            android:textSize="14dp" />

    </RelativeLayout>

    <ImageView
        android:id="@+id/iv_call"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/ic_call_green" />

    <ImageView
        android:id="@+id/iv_share"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/iv_call"
        android:layout_marginRight="10dp"
        android:layout_marginTop="20dp"
        android:background="@drawable/bg_et"
        android:padding="5dp"
        android:src="@drawable/ic_shr" />
</RelativeLayout>

fragment_all.xml

<?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:id="@+id/lv_listing"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>

</LinearLayout>

请帮帮我。

1 个答案:

答案 0 :(得分:2)

AllFragment更改

adapter = new ListingAdapter(context, web, imageId);

为:

adapter = new ListingAdapter(getActivity(), web, imageId);

并在ListingAdapter更改

public ListingAdapter(context c, String[] web, int[] Imageid) {
        mContext = c;
        this.web = web;
    }

public ListingAdapter(Activity c, String[] web, int[] Imageid) {
        mContext = c;
        this.web = web;
    }

并更改

private Context mContext;

private Activity mContext;

并更改

LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

为:

LayoutInflater inflater = (LayoutInflater) mContext.getLayoutInflater();