将CustomAdapter实现为Fragment ListView

时间:2016-11-15 03:47:18

标签: android listview android-fragments android-arrayadapter custom-adapter

我尝试在Fragment和ListView上实现CustomAdapter,但它在androidListView.setAdapter(androidListAdapter);

中显示错误NullPointerException

以前我尝试过没有片段的CustomAdapter,这是成功的

这是我的片段代码:

public class LaporanFragment extends Fragment {
private ArrayAdapter<String> mLaporanAdapter;
String androidListViewStrings[] = {"Android ListView Example", "Android Custom ListView Example", "Custom ListView Example",
        "Android List Adapter", "Custom Adapter ListView", "ListView Tutorial",
        "ListView with Image and Text", "Custom ListView Text and Image", "ListView Custom Tutorial"};

Integer image_id[] = {R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
        R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
        R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher};

public LaporanFragment(){
}

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_laporan, container, false);
    LaporanAdapter androidListAdapter = new LaporanAdapter(getActivity(), image_id, androidListViewStrings);
    ListView androidListView = (ListView) rootView.findViewById(R.id.list_view_laporan);
    androidListView.setAdapter(androidListAdapter); **//Error nullPointerExeption on this line**

    return rootView;
}}

这是我的自定义适配器代码:

public class LaporanAdapter extends ArrayAdapter {
String[] androidListViewStrings;
Integer[] imagesId;
Context context;

public LaporanAdapter(Activity context, Integer[] imagesId, String[] textListView) {
    super(context, R.layout.list_item_laporan, textListView);
    this.androidListViewStrings = textListView;
    this.imagesId = imagesId;
    this.context = context;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    LayoutInflater layoutInflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View viewRow = layoutInflater.inflate(R.layout.list_item_laporan, null,
            true);
    TextView mtextView = (TextView) viewRow.findViewById(R.id.list_item_laporan_textview);
    ImageView mimageView = (ImageView) viewRow.findViewById(R.id.image_view);
    mtextView.setText(androidListViewStrings[i]);
    mimageView.setImageResource(imagesId[i]);
    return viewRow;
}}

更新:

Logcat输出:

FATAL EXCEPTION: main
          Process: id.co.gloftech.pelaporandishub, PID: 4859
          java.lang.RuntimeException: Unable to start activity ComponentInfo{id.co.gloftech.pelaporandishub/id.co.gloftech.pelaporandishub.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
              at android.app.ActivityThread.access$800(ActivityThread.java:151)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
              at android.os.Handler.dispatchMessage(Handler.java:102)
              at android.os.Looper.loop(Looper.java:135)
              at android.app.ActivityThread.main(ActivityThread.java:5254)
              at java.lang.reflect.Method.invoke(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:372)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
           Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
              at id.co.gloftech.pelaporandishub.LaporanFragment.onCreateView(LaporanFragment.java:85)
              at android.support.v4.app.Fragment.performCreateView(Fragment.java:2087)
              at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113)
              at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295)
              at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
              at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1682)
              at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
              at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607)
              at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:181)
              at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1236)
              at android.app.Activity.performStart(Activity.java:6006)
              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
              at android.app.ActivityThread.access$800(ActivityThread.java:151) 
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
              at android.os.Handler.dispatchMessage(Handler.java:102) 
              at android.os.Looper.loop(Looper.java:135) 
              at android.app.ActivityThread.main(ActivityThread.java:5254) 
              at java.lang.reflect.Method.invoke(Native Method) 
              at java.lang.reflect.Method.invoke(Method.java:372)

还有fragment_laporan.xml

<FrameLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity$LaporanFragment">

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

1 个答案:

答案 0 :(得分:0)

您的ListView ID错误。因此rootView.findViewById(R.id.list_view_laporan)将返回null并导致NPE。

更改,

ListView androidListView = (ListView) rootView.findViewById(R.id.list_view_laporan);

要,

ListView androidListView = (ListView) rootView.findViewById(R.id.list_view_forecast);