简体   繁体   English

将人像旋转到风景时,我要关闭力量,我尝试了android:configChanges =“ keyboard | keyboardHidden”并以人像模式工作吗?

[英]Getting Force Close when rotating portrait to landscape and I tried android:configChanges=“keyboard|keyboardHidden” and working in portrait mode?

AM getting force close on emulator while rotating from portrait to Landscape? 从纵向旋转到横向时,AM是否在仿真器上强制关闭? I already tried adding in manifest android:configChanges="keyboard|keyboardHidden" 我已经尝试添加清单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();
}

It seems that your ImageView home view is NULL that why when you try to set your clickListener it crashes: 看来您的ImageView home视图为NULL,所以当您尝试设置clickListener时会崩溃:

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. 如果仅当您在横向模式下发生时,我认为这是因为您使用的是横向特定布局(目录res/layout-land ),并且此布局没有带有R.id.home标识符的ImageView

Edit: 编辑:

One more thing, you do not need to add android:configChanges="keyboard|keyboardHidden" to your manifest. 还有一件事,您无需在android:configChanges="keyboard|keyboardHidden"添加android:configChanges="keyboard|keyboardHidden" It's not the cause of your problem and it's discouraged for Android development expect for some really specific case. 这不是问题的根源,对于某些特定情况下的Android开发,建议不要这样做。

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. 当您必须避免由于配置更改而导致重启时,应将这种技术视为万不得已的方法,建议不要将其用于大多数应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在横向模式下强制关闭,但在纵向中不强制关闭 - Force Close in Landscape Mode, but not in Portrait Android:当我使用 android:configChanges=“orientation|keyboardHidden”> 时,横向模式的视图发生了变化 - Android: View of landscape mode changes when I use android:configChanges=“orientation|keyboardHidden”> 数据丢失并在横向和纵向模式下强制关闭android - data lose and force close in landscape and portrait mode android android:configChanges =“screenSize”参数在横向模式下采用纵向高度 - android:configChanges=“screenSize” parameter is taking portrait height in landscape mode 当键盘打开时,纵向模式被误解为横向 - Portrait mode gets misinterpreted as landscape when keyboard is on 在Android中以编程方式使用人像和风景模式 - Working with portrait and landscape mode programmatically in android Android 中的横向模式更改为纵向 - Landscape Mode change to portrait in Android 纵向模式下的Android Landscape VideoView - Android Landscape VideoView in portrait mode 适用于Android应用程序的横向和纵向模式 - Landscape and portrait mode for android application 平板电脑Android中的横向和纵向模式 - Landscape and portrait mode in tablet Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM