Frage

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.

War es hilfreich?

Lösung

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.

Andere Tipps

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top