Search code examples
javaandroidbutterknife

ListView not being initialised


This is driving me up the wall right now. I have two Fragments with ListViews, both of which use ButterKnife. One of them works just fine, but the other won't.

Here's the working Fragment:

//IRRELEVANT CODE ABOVE
@BindView(R.id.exerciseListView1)
ListView exerciseListView;

/**
 * Mandatory empty constructor for the fragment manager to instantiate the
 * fragment (e.g. upon screen orientation changes).
 */
public ExerciseFragment() {
}

@SuppressWarnings("unused")
public static ExerciseFragment newInstance(int columnCount) {
    ExerciseFragment fragment = new ExerciseFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_COLUMN_COUNT, columnCount);
    fragment.setArguments(args);
    return fragment;
}

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

    if (getArguments() != null) {
        mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_exercise_list, container, false);
    unbinder = ButterKnife.bind(this, view);

    ListItem[] exercises = {
            new ListItem(" 1. Learning Names & Faces", "This exercise will teach you how to learn people's names."),
            new ListItem(" 2. Paying Attention", "This exercise will test how well you pay attention."),
            new ListItem(" 3. Memory Recall", "This exercise will help you improve your recall skills.")
    };

    mAdapter = new ArrayAdapter<ListItem>(view.getContext(), R.layout.mylist, exercises);
    exerciseListView.setAdapter(mAdapter);
    exerciseListView.setOnItemClickListener(new ListView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
            String item = ((TextView) view).getText().toString();
            Log.d("ExercisesActivity", item + " #" + Integer.toString(position) + " selected");
            //Switch statements won't take longs, thankfully there's the position variable
            switch (position) {
                case 0:
                    startActivity(new Intent(getContext(), LearningNamesActivity.class));
                    break;
                case 1:
                    startActivity(new Intent(getContext(), PayingAttentionActivity.class));
                    break;
                case 2:
                    startActivity(new Intent(getContext(), MemoryRecallActivity.class));
                    break;
            }
        }
    });

    return view;
}

And here's the one that won't work:

//IRRELEVANT CODE ABOVE
@BindView(R.id.informationListView)
ListView informationList;
/**
 * Mandatory empty constructor for the fragment manager to instantiate the
 * fragment (e.g. upon screen orientation changes).
 */
public InformationFragment() {
}

// TODO: Customize parameter initialization
@SuppressWarnings("unused")
public static InformationFragment newInstance(int columnCount) {
    InformationFragment fragment = new InformationFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_COLUMN_COUNT, columnCount);
    fragment.setArguments(args);
    return fragment;
}

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

    if (getArguments() != null) {
        mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_information_list, container, false);
    unbinder = ButterKnife.bind(getContext(), view);

    ListItem[] exercises = {
            new ListItem("1. Tell me about memory", ""),
            new ListItem("2. What can go wrong with my memory", ""),
            new ListItem("3. Helpful tips to improve my memory", "")
    };
    mAdapter = new ArrayAdapter<>(view.getContext(), R.layout.mylist, exercises);
informationList.setAdapter(mAdapter);
informationList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            switch (position) {
                case 0:
                    startActivity(new Intent(getContext(), LearnAboutMemoryActivity.class));
                    break;
                case 1:
                    startActivity(new Intent(getContext(), WhatCanGoWrongActivity.class));
                    break;
                case 2:
                    //TODO: Use HelpfulTipsActivity as an "all-purpose" activity for holding tip Fragments
                    startActivity(new Intent(getContext(), HelpfulTipsActivity.class));
                    break;
             }
        }
});

return view;

Here's the error log:

06-17 12:20:15.674 13182-13182/com.tommy.mastersproject E/AndroidRuntime: FATAL EXCEPTION: main                                                                         Process: com.tommy.mastersproject, PID: 13182
Theme: themes:{com.android.settings=overlay:com.cyngn.hexo, default=overlay:system, iconPack:system, fontPkg:system, com.android.systemui=overlay:system, com.android.systemui.navbar=overlay:system}
                                                                          java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tommy.mastersproject/com.tommy.mastersproject.activities.InformationActivity}: 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:2450)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.tommy.mastersproject.fragments.InformationFragment.onCreateView(InformationFragment.java:90)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:339)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:601)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1238)
at android.app.Activity.performStart(Activity.java:6268)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5461) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

Here's the Fragments' respective layout files: Working properly

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
    <ListView
        android:id="@+id/exerciseListView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</RelativeLayout>

Not working properly

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
    <ListView
        android:id="@+id/informationListView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

Was there something in the working Fragment that I forgot to put into the one which doesn't work?


Solution

  • I think the problem is in InformationFragment inside onCreateView():

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.fragment_information_list, container, false);
      unbinder = ButterKnife.bind(this, view);
    
      ListItem[] exercises = {
            new ListItem("1. Tell me about memory", ""),
            new ListItem("2. What can go wrong with my memory", ""),
            new ListItem("3. Helpful tips to improve my memory", "")
      };
      // rest of your code
    

    Problem:

    While binding view you used getContext().So your informationList ListView remains Null and you were getting NullPointerException Remove getContext() instead add this.