自定义适配器显示RuntimeException - ListView

时间:2014-12-29 11:22:58

标签: java android listview

我有一个ListView和一个自定义Adapter,但当我将该适配器设置为我的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中显示错误。

2 个答案:

答案 0 :(得分:2)

您获得NPE,因为您的theatreList为null。你不能在任何地方初始化它。

答案 1 :(得分:2)

在logcat中查看此行

Caused by: java.lang.NullPointerException

您尚未初始化 theatreList

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