将肖像旋转到横向时获得强制关闭,我尝试了android:configChanges =“keyboard | keyboardHidden”并在纵向模式下工作?

时间:2013-02-05 12:20:38

标签: android android-intent android-emulator

AM从纵向旋转到横向时,是否在模拟器上关闭力?我已经尝试添加清单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)

这是我的代码:

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();
}

1 个答案:

答案 0 :(得分:2)

您的ImageView home视图似乎为NULL,为什么当您尝试设置clickListener时崩溃:

home.setOnClickListener(this);

如果只有当您处于横向模式时才会发生,我认为这是因为您使用特定的横向方向布局(目录res/layout-land)并且此布局没有{带有ImageView标识符的{1}}。

编辑:

还有一件事,您不需要在清单中添加R.id.home。这不是你的问题的原因,并且不鼓励Android开发期望某些特定的情况。

In the official documentation

  

当你必须避免时,这种技术应该被视为最后的手段   由于配置更改而重新启动,不建议大多数人使用   应用