2014-12-29 166 views
1

我有一個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中顯示錯誤。

+0

初始化您theatreList爲theatreList =新名單();在你的片段類中,並在其中放置一些對象 –

回答

2

因爲您的theatreList爲null,您將得到NPE。你不會在任何地方初始化它。

+0

Ohh god。謝謝bro.i忘了這麼做。非常感謝。 –

+0

兄弟我有疑問... –

+0

你的回答很好。但展示如何解決它的方式? –

2

看到這一行你的logcat

Caused by: java.lang.NullPointerException 

你還沒有初始化theatreList

tlAdapter = new TheatreListAdapter(getActivity().getBaseContext(),theatreList); 
+0

謝謝。它的工作。 –

+0

對不起兄弟,degrigorash先回答。所以他應得的。但我會從你的朋友帳戶中投票給你。 –

+0

嗯,謝謝:)兄弟 –