5

Using a device for testing - Whenever I lock the screen (or sleeps after 1 min idle), the app crashes and the logcat shows a NullPointerException always on an imageView.setOnClickListener in the onResume() method.

Here's the small part of the code where I get the exception :

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.critical_items_list);

    // instantiate member variables

    mExtras = getIntent().getExtras();
    mItems_P1 = mExtras.getParcelableArrayList(EXTRA_ITEMS_P1);
    mItems_P2 = mExtras.getParcelableArrayList(EXTRA_ITEMS_P2);
    mItems_Engine = mExtras.getParcelableArrayList(EXTRA_ITEMS_ENGINE); 
}

protected void onResume() {
    super.onResume();

    ImageView imageAddToMultiple = (ImageView) findViewById(R.id.image_add_to_multiple);

    //This is where the I get the NullPointer
    imageAddToMultiple.setOnClickListener(listenerAddToMultipleItems); 

    refreshLists();
    ListView listP1 = (ListView) findViewById(R.id.list_p1);
    ListView listP2 = (ListView) findViewById(R.id.list_p2);
    ListView listEngine = (ListView) findViewById(R.id.list_engine);

    listP1.setAdapter(new MyAdapter(mList_P1));
    listP2.setAdapter(new MyAdapter(mList_P2));
    listEngine.setAdapter(new MyAdapter(mList_Engine));
}

The imageView itself lies in a RelativeLayout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="@dimen/standard_bar_height"
    android:background="@color/green"
    android:baselineAligned="false" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:text="@string/plant_1"
            android:textColor="@android:color/black" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/plant_2"
            android:textColor="@android:color/black" />
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/RelativeLayout1"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1" >

        <ImageView
            android:id="@+id/image_add_to_multiple"
            android:layout_width="@dimen/standard_bar_height"
            android:layout_height="@dimen/standard_bar_height"
            android:layout_alignParentRight="true"
            android:layout_centerHorizontal="true"
            android:contentDescription="@string/description_add_to_multiple_items"
            android:src="@android:drawable/ic_menu_add" />

        <TextView
            android:id="@+id/TextView1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:text="@string/engine"
            android:textColor="@android:color/black" />
    </RelativeLayout>
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <ListView
            android:id="@+id/list_p1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
        </ListView>

        <View
            android:layout_width="2dp"
            android:layout_height="fill_parent"
            android:background="@color/green" />

        <ListView
            android:id="@+id/list_p2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
        </ListView>

        <View
            android:layout_width="2dp"
            android:layout_height="fill_parent"
            android:background="@color/green" />

        <ListView
            android:id="@+id/list_engine"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
        </ListView>
    </LinearLayout>
</LinearLayout>

I have no clue why I'm getting this NullPointer.

FATAL EXCEPTION: main java.lang.RuntimeException: Unable to resume activity {nmw.nss/nmw.nss.CriticalList}: java.lang.NullPointerException at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2820) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2859) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2242) at android.app.ActivityThread.access$600(ActivityThread.java:139) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:4974) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at nmw.nss.CriticalList.onResume(CriticalList.java:93)

at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1236) at android.app.Activity.performResume(Activity.java:4620) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2804) ... 12 more

Thanks.

EDIT : I've added the complete activity(sort of), the xml, and the logcat. Do I need to override any of Activity methods, perhaps the onStop or onPause to make this work?

Chilledrat
  • 2,593
  • 3
  • 28
  • 38
Arjun
  • 101
  • 1
  • 5
  • but where you have declared listview ? – MAC Jun 16 '12 at 07:54
  • @gtumca-MAC listview is of no concern, that is just part of my activity. To confirm it, I removed the imageView and its listener code entirely, and it was working perfectly. So definitely findViewById is returning null – Arjun Jun 16 '12 at 12:11

2 Answers2

4

Finally found it! My app is basically designed to run in landscape mode, and in the manifest I've added ------- android:screenOrientation="landscape".

Well, here's the problem : it doesn't matter in what mode you lock the device, findViewById() always searches the XML in the portrait mode, and in my case, which did not have the ImageView at all. Hence the NullPointerException.

Arjun
  • 101
  • 1
  • 5
  • same prblm here for my app can you tell me what will be the solution for this – Manju Jun 18 '12 at 12:39
  • 4
    Since I never used the portrait mode, it didn't really matter what the layout XML contained, I simply copied all the code from the landscape from the XML, to the portrait – Arjun Jun 22 '12 at 07:34
  • That's the problem, yes. Here's how to fix it: http://stackoverflow.com/a/29494115/1760313 – Tom Apr 27 '15 at 12:13
0

I assume that you have not called setContentView() before, thus Imageview imageAddToMultiple is null and when you try to set the listener, it crashes.

Hope it helps!

Dimitris Makris
  • 5,183
  • 2
  • 34
  • 54