简体   繁体   中英

Locking orientation to portrait force close in my phone

I want my app to lock into portrait mode. For this I used this code :

<activity android:name="MyActivity"
                  android:label="@string/app_name" android:screenOrientation="portrait"
                  android:configChanges="orientation|keyboardHidden"
                  >

and in MyActivity class :

public void onConfigurationChanged(Configuration newConfig)
    {
        super.onConfigurationChanged(newConfig);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

This code work correctly in emulator, but when I install and run in my phone, the app is force closed. How can I solve this problem?

Yo already use in manifest then there is no need to use by pragmatically. So remove the onConfigurationChanged method from your code.

I am using

android:screenOrientation="portrait" 

in the manifest file, no other code in Java side and is working.

I see two problems here:

  • Remove android:configChanges="orientation|keyboardHidden" from manifest
  • Remove onConfigurationChanged from java

    and it should work just fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM