2

I'm getting a NullPointerException at this line:

ImageButton sb = (ImageButton) findViewById(R.id.imageButton2);
sb.setOnClickListener(new OnClickListener() { //error on this line

    public void onClick(View v) {
        Intent intent = new Intent(AlarmClock.this,SettingsActivity.class);
        startActivity(intent);
    }
});

I have checked the setContentView() and it refers to the correct layout file which has imageButton2 as follows:

<ImageView
    android:id="@+id/imageButton1"
    android:layout_width="0dip"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:layout_weight="0.275"
    android:scaleType="fitCenter"
    android:src="@drawable/bell_icon" />

Here's the crash report:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.moosa.alarmclock/com.moosa.alarmclock.AlarmClock}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3689)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
    at com.moosa.alarmclock.AlarmClock.updateLayout(AlarmClock.java:260)
    at com.moosa.alarmclock.AlarmClock.onCreate(AlarmClock.java:220)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
    ... 11 more

The biggest problem is: I don't find the error on my GingerBread Phone or my ICS emulator. It seems this problem has occurred mainly for Galaxy S3's who have sent the crash report for the published report.

Please help me out as this is my first published app.

Mohammad Shabaz Moosa
  • 1,515
  • 1
  • 13
  • 20
  • 5
    Do you have multiple layout files to go with different screens? If so, the S3 may be loading a layout files that has a typo in the ID, or be without that ID completely. – Raghav Sood Mar 14 '13 at 09:18
  • That was probably the issue. There was a copy of the same layout file in res/layout-land folder without the new element Thnaks a ton – Mohammad Shabaz Moosa Mar 14 '13 at 09:26

2 Answers2

2

If you have multiple layout files to go with different screen sizes or orientations, the S3 may be loading one of those files in which your ImageButton has a different ID, or is missing altogether.

This would result in a null value and cause your crash.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
-1

Please put if befor it

ImageButton sb = (ImageButton)findViewById(R.id.imageButton2); if(sb!=null)

sb.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) {
        Intent intent = new Intent(AlarmClock.this,SettingsActivity.class);
        startActivity(intent);

    }
});

else it will crash on runtime.

  • This doesn't really answer the question as to why it is crashing. It just prevents a crash, and also kills whatever functionality that button offered. – Raghav Sood Mar 14 '13 at 09:32