-1

我是學嘗試落實片段和ListView CustomAdapter,但它顯示androidListView.setAdapter(androidListAdapter);實施CustomAdapter到碎片的ListView

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

UPDATE:

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> 

+0

顯示logcat的輸出和'fragment_laporan.xml'代碼。 –

+0

@KNeerajLal謝謝,我已更新我的問題 –

回答

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); 
+1

謝謝,它的工作原理,我得到了錯誤的ID –