-1

AM getting force close on emulator while rotating from portrait to Landscape? I already tried adding in manifest android:configChanges="keyboard|keyboardHidden"

02-05 17:29:52.321: E/AndroidRuntime(1192): FATAL EXCEPTION: main
02-05 17:29:52.321: E/AndroidRuntime(1192): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kidzapp.sample/com.kidzapp.sample.alphabets}: java.lang.NullPointerException
02-05 17:29:52.321: E/AndroidRuntime(1192):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-05 17:29:52.321: E/AndroidRuntime(1192):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-05 17:29:52.321: E/AndroidRuntime(1192):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-05 17:29:52.321: E/AndroidRuntime(1192):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-05 17:29:52.321: E/AndroidRuntime(1192):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-05 17:29:52.321: E/AndroidRuntime(1192):     at android.os.Looper.loop(Looper.java:123)
02-05 17:29:52.321: E/AndroidRuntime(1192):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-05 17:29:52.321: E/AndroidRuntime(1192):     at java.lang.reflect.Method.invokeNative(Native Method)
02-05 17:29:52.321: E/AndroidRuntime(1192):     at java.lang.reflect.Method.invoke(Method.java:521)
02-05 17:29:52.321: E/AndroidRuntime(1192):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-05 17:29:52.321: E/AndroidRuntime(1192):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-05 17:29:52.321: E/AndroidRuntime(1192):     at dalvik.system.NativeStart.main(Native Method)
02-05 17:29:52.321: E/AndroidRuntime(1192): Caused by: java.lang.NullPointerException
02-05 17:29:52.321: E/AndroidRuntime(1192):     at com.kidzapp.sample.alphabets.onCreate(alphabets.java:68)
02-05 17:29:52.321: E/AndroidRuntime(1192):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-05 17:29:52.321: E/AndroidRuntime(1192):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

This is my code:

public class alphabets extends Activity implements OnClickListener{

    ImageButton home;
    @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.lower);

          home=(ImageButton)findViewById(R.id.home);

        home.setOnClickListener(this);
}

 public void onClick(View image) {
        // TODO Auto-generated method stub

switch (image.getId()) {
case R.id.home:

Intent i=new Intent(this, MainActivity.class);

startActivity(i);
finish();
break;

    }

}

@Override
protected void onStop() {
    // TODO Auto-generated method stub
    mp.stop();
    super.onStop();
}
5
  • Can you give the code at line 68 of your alphabets.java file?
    – tolgap
    Feb 5, 2013 at 12:23
  • I bet it's alphabets.java:68 And classnames should start with upper case letter! And maybe you want t oadd need to onConfigChanges "orientation" as well. Feb 5, 2013 at 12:24
  • where is 68 line number in alphabets.java class .plz edit relevant code with question Feb 5, 2013 at 12:25
  • have you tired Doctoror Drive's suggestion? also, when the device orientation is changed the onStop() method is called.what is mp which you have used within the onStop()? is it properly initialized? Feb 5, 2013 at 13:11
  • Thanks to @DoctororDrive & my problem is resolved Feb 6, 2013 at 7:06

1 Answer 1

2

It seems that your ImageView home view is NULL that why when you try to set your clickListener it crashes:

home.setOnClickListener(this);

If it only occurs when you are in landscape mode, I think it's because you are using a specific layout for landscape orientation (directory res/layout-land) and this layout does not have an ImageView with the R.id.home identifier.

Edit:

One more thing, you do not need to add android:configChanges="keyboard|keyboardHidden" to your manifest. It's not the cause of your problem and it's discouraged for Android development expect for some really specific case.

In the official documentation:

This technique should be considered a last resort when you must avoid restarts due to a configuration change and is not recommended for most applications.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.