0

I'm setting an basic ArrayAdapter. When I rotate the screen a few times, then I get a nullpointer exception.

public void testListView(){        
    my_array_list.add("hello world 1");
    my_array_list.add("hello world 2");
    my_array_list.add("hello world 3");
 //next line does the problem
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, my_array_list);
    mListView.setAdapter(arrayAdapter);
}

this is my onCreateView()

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.developer_test_side, container, false);
    ...
    my_array_list = new ArrayList<>();
    mListView = (ListView) v.findViewById(R.id.my_listview);
    testListView();
}

here is the logcat output:

 04-27 20:10:45.985  11813-11813/com.rivler.example E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.rivler.example, PID: 11813
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
        at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
        at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:153)
        at com.example.rivler.DeveloperTestSide.testListView(DeveloperTestSide.java:116)
        at com.example.rivler.DeveloperTestSide$6.run(DeveloperTestSide.java:149)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5312)
        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:901)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

what could be the problem?

mkersche17
  • 85
  • 10
  • getActivity returns null, because your method is called in a thread without consideration of the fragment being attached or not. – njzk2 Apr 27 '15 at 19:26
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – JAAD Nov 12 '16 at 12:03

3 Answers3

0

It sounds like the view is being destroyed and the code is still trying to access the now non-existent component (My guess is that getActivity() is returning null). Screen orientation changes will cause your view to be destroyed.

Try moving the logic into the onResume instead.

user2120910
  • 394
  • 1
  • 4
  • 14
0

The activity holding your fragment is recreated every time the screen is rotated. The Fragment itself gets recreated itself and attached to the Activity again.

Calling getActivity() returns NULL if the Fragment is not yet attached or the Activity itself isn't recreated (a.k.a we haven't reached the onActivityCreated() lifecycle method). This in tern causes the NullPointerException in the ArrayAdapter.

To solve your problem - just save your context in onAttach() method and use it. Other option is to always wrap calls to getActivity() with isAdded() like this:

if (isAdded()) {
    // code that needs getActivity()
}
Vesko
  • 3,662
  • 2
  • 20
  • 29
0

If you do not need or want screen rotation on this activity then you can add the following to your manifest.

<activity
        android:name=".MainActivity"
        android:screenOrientation="portrait">
</activity>

This will not allow the rotation in the first place so your fragment will never be destroyed. You can of course tell it to be landscape instead if you want.

thurst0n
  • 155
  • 1
  • 1
  • 12