我有一個ListView
和一個自定義Adapter
,但是當我將該適配器設置爲我的Listview
它顯示這樣的錯誤。自定義適配器顯示RuntimeException - ListView
的logcat:
12-29 16:43:43.740: E/AndroidRuntime(27363): FATAL EXCEPTION: main 12-29 16:43:43.740: E/AndroidRuntime(27363): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rh.bookmany/com.rh.bookmany.MFragmentContainer}: java.lang.NullPointerException 12-29 16:43:43.740: E/AndroidRuntime(27363): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121) 12-29 16:43:43.740: E/AndroidRuntime(27363): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2146) 12-29 16:43:43.740: E/AndroidRuntime(27363): at android.app.ActivityThread.access$700(ActivityThread.java:140) 12-29 16:43:43.740: E/AndroidRuntime(27363): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238) 12-29 16:43:43.740: E/AndroidRuntime(27363): at android.os.Handler.dispatchMessage(Handler.java:99) 12-29 16:43:43.740: E/AndroidRuntime(27363): at android.os.Looper.loop(Looper.java:137) 12-29 16:43:43.740: E/AndroidRuntime(27363): at android.app.ActivityThread.main(ActivityThread.java:4944) 12-29 16:43:43.740: E/AndroidRuntime(27363): at java.lang.reflect.Method.invokeNative(Native Method) 12-29 16:43:43.740: E/AndroidRuntime(27363): at java.lang.reflect.Method.invoke(Method.java:511) 12-29 16:43:43.740: E/AndroidRuntime(27363): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038) 12-29 16:43:43.740: E/AndroidRuntime(27363): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 12-29 16:43:43.740: E/AndroidRuntime(27363): at dalvik.system.NativeStart.main(Native Method) 12-29 16:43:43.740: E/AndroidRuntime(27363): Caused by: java.lang.NullPointerException 12-29 16:43:43.740: E/AndroidRuntime(27363): at com.rh.bookmany.navigationadapter.TheatreListAdapter.getCount(TheatreListAdapter.java:40) 12-29 16:43:43.740: E/AndroidRuntime(27363): at android.widget.ListView.setAdapter(ListView.java:466) 12-29 16:43:43.740: E/AndroidRuntime(27363): at com.rh.bookmany.ShowTimeFragment.onCreateView(ShowTimeFragment.java:43) 12-29 16:43:43.740: E/AndroidRuntime(27363): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829) 12-29 16:43:43.740: E/AndroidRuntime(27363): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035) 12-29 16:43:43.740: E/AndroidRuntime(27363): at android.app.BackStackRecord.run(BackStackRecord.java:635) 12-29 16:43:43.740: E/AndroidRuntime(27363): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1399) 12-29 16:43:43.740: E/AndroidRuntime(27363): at android.app.Activity.performStart(Activity.java:5197) 12-29 16:43:43.740: E/AndroidRuntime(27363): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2094) 12-29 16:43:43.740: E/AndroidRuntime(27363): ... 11 more
這裏是我的Fragment
:
public class ShowTimeFragment extends Fragment {
List<Theatre> theatreList;
ListView lvTheatre;
TheatreListAdapter tlAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflating View
View v = inflater.inflate(R.layout.theatre_showtime, container, false);
lvTheatre = (ListView) v.findViewById(R.id.lvTheatre);
tlAdapter = new TheatreListAdapter(getActivity().getBaseContext(),theatreList);
lvTheatre.setAdapter(tlAdapter);
return v;
}
}
這裏是我Adapter
:
public class TheatreListAdapter extends BaseAdapter {
Context c;
List<Theatre> theatreList;
LayoutInflater lInflater;
public TheatreListAdapter(Context c,List<Theatre> theatreList)
{
Log.d("X","Constructor called");
try {
this.c = c;
this.theatreList = theatreList;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("X",e.getMessage());
}
Log.d("X","Constuctor Passed");
}
@Override
public int getCount() {
return theatreList.size();
}
@Override
public Object getItem(int position) {
return theatreList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d("X","getView Called");
try {
//Checking inflater already inflated
if(lInflater==null)
{
lInflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
//Checking convertView already instantiated
if(convertView==null)
{
convertView = lInflater.inflate(R.layout.theatre_fragment_row, null);
}
//Inst. UI elements
TextView tvTheatre = (TextView) convertView.findViewById(R.id.tvTheatre);
TextView tvShowTimes = (TextView) convertView.findViewById(R.id.tvShowTimes);
//Inst. Theatre for row
Theatre mTheatre = theatreList.get(position);
//Generating showTimes
String showTimes = "";
for(String showTime:mTheatre.getShowTimes())
{
showTimes+=showTime;
}
//Setting collected information to the UI elements
tvTheatre.setText(mTheatre.getTheatre());
tvShowTimes.setText(showTimes);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("X",e.getMessage());
}
return convertView;
}
}
當過我刪除setAdapter
方法,它工作正常,這意味着沒有錯誤顯示。 但是當我設置適配器時,它在我的logcat中顯示錯誤。
初始化您theatreList爲theatreList =新名單();在你的片段類中,並在其中放置一些對象 –