Search code examples
javaandroidtablet

setOnClickListener error exclusively on Tablets (10")?


i just finished my app, and i'm ready to publish it. Now, i decided to make my app compatible with 10" tablets, and when i open my first activity (on the emulator with Nexus10) it displays everything correctly (even if it has to change orientation from portrait to landscape), but when i start my second activity, it crashes. On the LogCat, i see that the problem is an ImageButton, normally working on any other device.

Activity.java:

    ImageButton bulb = (ImageButton) findViewById(R.id.bulb1);
        bulb.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //ecc... In-app purchase

Activity.xml:

<ImageButton 
        android:id="@+id/bulb1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/lightbulb"
        android:layout_centerHorizontal="true"
        android:layout_alignTop="@+id/textView1"
        android:background="@drawable/lightbulb"
        />

and Logcat:

05-27 14:18:22.949: E/AndroidRuntime(965): FATAL EXCEPTION: main
05-27 14:18:22.949: E/AndroidRuntime(965): Process: com.lifeapps.appquiz, PID: 965
05-27 14:18:22.949: E/AndroidRuntime(965): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lifeapps.appquiz/com.lifeapps.appquiz.Levels}: java.lang.NullPointerException
05-27 14:18:22.949: E/AndroidRuntime(965):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-27 14:18:22.949: E/AndroidRuntime(965):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-27 14:18:22.949: E/AndroidRuntime(965):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-27 14:18:22.949: E/AndroidRuntime(965):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-27 14:18:22.949: E/AndroidRuntime(965):  at android.os.Handler.dispatchMessage(Handler.java:102)
05-27 14:18:22.949: E/AndroidRuntime(965):  at android.os.Looper.loop(Looper.java:136)
05-27 14:18:22.949: E/AndroidRuntime(965):  at android.app.ActivityThread.main(ActivityThread.java:5017)
05-27 14:18:22.949: E/AndroidRuntime(965):  at java.lang.reflect.Method.invokeNative(Native Method)
05-27 14:18:22.949: E/AndroidRuntime(965):  at java.lang.reflect.Method.invoke(Method.java:515)
05-27 14:18:22.949: E/AndroidRuntime(965):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-27 14:18:22.949: E/AndroidRuntime(965):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-27 14:18:22.949: E/AndroidRuntime(965):  at dalvik.system.NativeStart.main(Native Method)
05-27 14:18:22.949: E/AndroidRuntime(965): Caused by: java.lang.NullPointerException
05-27 14:18:22.949: E/AndroidRuntime(965):  at com.lifeapps.appquiz.Levels.onCreate(Levels.java:133)
05-27 14:18:22.949: E/AndroidRuntime(965):  at android.app.Activity.performCreate(Activity.java:5231)
05-27 14:18:22.949: E/AndroidRuntime(965):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-27 14:18:22.949: E/AndroidRuntime(965):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-27 14:18:22.949: E/AndroidRuntime(965):  ... 11 more

Any help? I repeat, that's ONLY happening on 10" tablets.


Solution

  • Is there a chance you have a different Activity.xml file in a layout-w820dp directory that is overriding the default Activity.xml in the default layout directory? If so, make sure that other layout file also includes the ImageButton with the correct id.