java.lang.IllegalStateException:只有全屏不透明活动才能请求方向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48072438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation
提问by Shubham Sejpal
I am facing the problem while retrieving the contacts from the contact book in Android 8.0 Oreo java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation
我在从 Android 8.0 Oreo java.lang.IllegalStateException 的通讯录中检索联系人时遇到问题:只有全屏不透明活动可以请求方向
I am trying to get the contact in my activity from the phone contact book and it works perfect for Lollipop, Marshmallow, Nougat, etc but it will gives me the error for Oreo like this please help me. My code is here below.
我正在尝试从电话通讯簿中获取我的活动中的联系人,它非常适合棒棒糖、棉花糖、牛轧糖等,但它会给我这样的奥利奥错误,请帮助我。我的代码在下面。
Demo Code :-
演示代码:-
private void loadContacts() {
contactAsync = new ContactLoaderAsync();
contactAsync.execute();
}
private class ContactLoaderAsync extends AsyncTask<Void, Void, Void> {
private Cursor numCursor;
@Override
protected void onPreExecute() {
super.onPreExecute();
Uri numContacts = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] numProjection = new String[]{ContactsContract.CommonDataKinds.Phone.CONTACT_ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.TYPE};
if (android.os.Build.VERSION.SDK_INT < 11) {
numCursor = InviteByContactActivity.this.managedQuery(numContacts, numProjection, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE NOCASE ASC");
} else {
CursorLoader cursorLoader = new CursorLoader(InviteByContactActivity.this, numContacts, numProjection, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE NOCASE ASC");
numCursor = cursorLoader.loadInBackground();
}
}
@Override
protected Void doInBackground(Void... params) {
if (numCursor.moveToFirst()) {
try {
final int contactIdIndex = numCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID);
final int displayNameIndex = numCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
final int numberIndex = numCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
final int typeIndex = numCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
String displayName, number, type;
do {
displayName = numCursor.getString(displayNameIndex);
number = numCursor.getString(numberIndex);
type = getContactTypeString(numCursor.getString(typeIndex), true);
final ContactModel contact = new ContactModel(displayName, type, number);
phoneNumber = number.replaceAll(" ", "").replaceAll("\(", "").replaceAll("\)", "").replaceAll("-", "");
if (phoneNumber != null || displayName != null) {
contacts.add(phoneNumber);
contactsName.add(displayName);
contactsChecked.add(false);
filterdNames.add(phoneNumber);
filterdContactNames.add(displayName);
filterdCheckedNames.add(false);
}
} while (numCursor.moveToNext());
} finally {
numCursor.close();
}
}
Collections.sort(contacts, new Comparator<String>() {
@Override
public int compare(String lhs, String rhs) {
return lhs.compareToIgnoreCase(rhs);
}
});
InviteByContactActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
mContactAdapter.notifyDataSetChanged();
}
});
return null;
}
}
private String getContactTypeString(String typeNum, boolean isPhone) {
String type = PHONE_TYPES.get(typeNum);
if (type == null)
return "other";
return type;
}
static HashMap<String, String> PHONE_TYPES = new HashMap<String, String>();
static {
PHONE_TYPES.put(ContactsContract.CommonDataKinds.Phone.TYPE_HOME + "", "home");
PHONE_TYPES.put(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE + "", "mobile");
PHONE_TYPES.put(ContactsContract.CommonDataKinds.Phone.TYPE_WORK + "", "work");
}
}
Error Log:-
错误日志:-
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example, PID: 6573
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.Activity.InviteByContactActivity}: java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation
Caused by: java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation
采纳答案by Debasish Ghosh
The problem seems to be happening when your target sdk is 28. So after trying out many options finally this worked.
当您的目标 sdk 为 28 时,问题似乎发生了。因此,在尝试了许多选项后,这终于奏效了。
<activity
android:name=".activities.FilterActivity"
android:theme="@style/Transparent"
android:windowSoftInputMode="stateHidden|adjustResize" />
style:-
风格:-
<style name="Transparent" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
Note:parent="Theme.AppCompat.Light.NoActionBar" is needed for api 28. Previously had something else at api 26. Was working great but started to give problem at 28.
Hope it helps someone out here.
EDIT: For some only by setting <item name="android:windowIsTranslucent">false</item> and <item name="android:windowIsFloating">false</item>
worked.May be depends upon the way you implement the solution works.In my case it worked by setting them to true.
注意:api 28 需要 parent="Theme.AppCompat.Light.NoActionBar"。以前在 api 26 上有其他东西。工作很好,但在 28 时开始出现问题。希望它可以帮助这里的人。编辑:对于某些只能通过设置<item name="android:windowIsTranslucent">false</item> and <item name="android:windowIsFloating">false</item>
工作。可能取决于您实施解决方案的方式。在我的情况下,它通过将它们设置为true来工作。
回答by Ragesh Ramesh
In Android O and later this error happens when you set
在 Android O 和更高版本中,当您设置时会发生此错误
android:screenOrientation="portrait"
in Manifest.
在清单中。
Remove that line and use
删除该行并使用
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
in your activity.
在你的活动中。
This will fix your issue.
这将解决您的问题。
回答by Steven Sun
Google throws this exception on Activity's onCreate
method after v27, their meaning is : if an Activity is translucent or floating, its orientation should be relied on parent(background) Activity, can't make decision on itself.
谷歌onCreate
在 v27 之后在 Activity 的方法上抛出了这个异常,它们的意思是:如果一个 Activity 是半透明或浮动的,它的方向应该依赖于父(背景)Activity,不能自行决定。
Even if you remove android:screenOrientation="portrait"
from the floating or translucent Activity but fix orientation on its parent(background) Activity, it is still fixed by the parent, I have tested already.
即使您android:screenOrientation="portrait"
从浮动或半透明 Activity 中删除但在其父(背景)Activity 上固定方向,它仍然由父级固定,我已经测试过。
One special situation : if you make translucent on a launcher Activity, it has't parent(background), so always rotate with device. Want to fix it, you have to take another way to replace <item name="android:windowIsTranslucent">true</item>
style.
一种特殊情况:如果您将启动器 Activity 设为半透明,则它没有父级(背景),因此始终随设备旋转。想要修复它,你必须采取另一种方式来替换<item name="android:windowIsTranslucent">true</item>
样式。
回答by Seven
I do not know if this is a bug from Google or an intended behavior but I (at least momentarily) solved it by changing compileSdkVersion and targetSdkVersion back to 26 in Gradle...
我不知道这是 Google 的错误还是预期的行为,但我(至少暂时)通过在 Gradle 中将 compileSdkVersion 和 targetSdkVersion 改回 26 解决了这个问题...
回答by Gardax
If you haven't resolved your problem just register the ad activity in the manifest like this:
如果您还没有解决问题,只需在清单中注册广告活动,如下所示:
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
tools:replace="android:theme"
/>
You also need to update to the latest version of the sdk.
您还需要更新到最新版本的 sdk。
回答by Balflear
Probably you showing Activity looking like Dialog(non-fullscreen), so remove screenOrientationfrom Manifestor from code. This will fix the issue.
可能您显示的 Activity 看起来像 Dialog(非全屏),因此请从清单或代码中删除screenOrientation。这将解决问题。
回答by hannes ach
I can't agree to most rated answer, because
我不能同意评分最高的答案,因为
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
causes an error
导致错误
java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation
java.lang.IllegalStateException:只有全屏不透明活动才能请求方向
but this makes it works for me
但这对我有用
<style name="TranslucentTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
and use it for your Activity, when you extends from
并将其用于您的活动,当您从
InterstitialActivity extends AppCompatActivity
in AndroidManifest.xml
在 AndroidManifest.xml 中
<activity
android:name=".InterstitialActivity"
...
android:screenOrientation="portrait"
android:theme="@style/TranslucentTheme" />
回答by Regis_AG
The only solution that really works :
唯一真正有效的解决方案:
Change:
改变:
<item name="android:windowIsTranslucent">true</item>
to:
到:
<item name="android:windowIsTranslucent">false</item>
in styles.xml
在styles.xml中
But this might induce a problem with your splashscreen (white screen at startup)... In this case, add the following line to your styles.xml:
但这可能会导致您的启动画面出现问题(启动时出现白屏)...在这种情况下,请将以下行添加到您的 style.xml 中:
<item name="android:windowDisablePreview">true</item>
just below the windowIsTranslucent line.
就在 windowIsTranslucent 线下方。
Last chance if the previous tips do not work : target SDK 26 instead o 27.
如果之前的提示不起作用,最后的机会是:将 SDK 26 改为 o 27。
回答by gellyke
If you use a fullscreen transparent activity, there is no need to specify the orientation lock on the activity. It will take the configuration settings of the parent activity. So if the parent activity has in the manifest:
如果使用全屏透明 Activity,则无需在 Activity 上指定方向锁定。它将采用父活动的配置设置。因此,如果父活动在清单中:
android:screenOrientation="portrait"
机器人:屏幕方向=“肖像”
your translucent activity will have the same orientation lock: portrait.
您的半透明活动将具有相同的方向锁定:纵向。
回答by Bingerz
Many people have given a fix, so I'll talk about the source of the problem.
很多人都给出了修复,所以我来谈谈问题的根源。
According to the exception log:
根据异常日志:
Caused by: java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation
at android.app.Activity.onCreate(Activity.java:1081)
at android.support.v4.app.SupportActivity.onCreate(SupportActivity.java:66)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:297)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:84)
at com.nut.blehunter.ui.DialogContainerActivity.onCreate(DialogContainerActivity.java:43)
at android.app.Activity.performCreate(Activity.java:7372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3147)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302)?
at android.app.ActivityThread.-wrap12(Unknown Source:0)?
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891)?
at android.os.Handler.dispatchMessage(Handler.java:108)?
at android.os.Looper.loop(Looper.java:166)
The code that triggered the exception in Activity.java
Activity.java 中触发异常的代码
//Need to pay attention mActivityInfo.isFixedOrientation() and ActivityInfo.isTranslucentOrFloating(ta)
if (getApplicationInfo().targetSdkVersion >= O_MR1 && mActivityInfo.isFixedOrientation()) {
final TypedArray ta = obtainStyledAttributes(com.android.internal.R.styleable.Window);
final boolean isTranslucentOrFloating = ActivityInfo.isTranslucentOrFloating(ta);
ta.recycle();
//Exception occurred
if (isTranslucentOrFloating) {
throw new IllegalStateException(
"Only fullscreen opaque activities can request orientation");
}
}
mActivityInfo.isFixedOrientation():
mActivityInfo.isFixedOrientation():
/**
* Returns true if the activity's orientation is fixed.
* @hide
*/
public boolean isFixedOrientation() {
return isFixedOrientationLandscape() || isFixedOrientationPortrait()
|| screenOrientation == SCREEN_ORIENTATION_LOCKED;
}
/**
* Returns true if the activity's orientation is fixed to portrait.
* @hide
*/
boolean isFixedOrientationPortrait() {
return isFixedOrientationPortrait(screenOrientation);
}
/**
* Returns true if the activity's orientation is fixed to portrait.
* @hide
*/
public static boolean isFixedOrientationPortrait(@ScreenOrientation int orientation) {
return orientation == SCREEN_ORIENTATION_PORTRAIT
|| orientation == SCREEN_ORIENTATION_SENSOR_PORTRAIT
|| orientation == SCREEN_ORIENTATION_REVERSE_PORTRAIT
|| orientation == SCREEN_ORIENTATION_USER_PORTRAIT;
}
/**
* Determines whether the {@link Activity} is considered translucent or floating.
* @hide
*/
public static boolean isTranslucentOrFloating(TypedArray attributes) {
final boolean isTranslucent = attributes.getBoolean(com.android.internal.R.styleable.Window_windowIsTranslucent, false);
final boolean isSwipeToDismiss = !attributes.hasValue(com.android.internal.R.styleable.Window_windowIsTranslucent)
&& attributes.getBoolean(com.android.internal.R.styleable.Window_windowSwipeToDismiss, false);
final boolean isFloating = attributes.getBoolean(com.android.internal.R.styleable.Window_windowIsFloating, false);
return isFloating || isTranslucent || isSwipeToDismiss;
}
According to the above code analysis, when TargetSdkVersion>=27, when using SCREEN_ORIENTATION_LANDSCAPE, SCREEN_ORIENTATION_PORTRAIT, and other related attributes, the use of windowIsTranslucent, windowIsFloating, and windowSwipeToDismiss topic attributes will trigger an exception.
根据以上代码分析,当TargetSdkVersion>=27时,使用SCREEN_ORIENTATION_LANDSCAPE、SCREEN_ORIENTATION_PORTRAIT等相关属性时,使用windowIsTranslucent、windowIsFloating、windowSwipeToDismiss主题属性会触发异常。
After the problem is found, you can change the TargetSdkVersion or remove the related attributes of the theme according to your needs.
发现问题后,您可以根据需要更改 TargetSdkVersion 或删除主题的相关属性。
回答by Radesh
In android Oreo (API 26) you can not change orientation for Activity that have below line in style
在 android Oreo (API 26) 中,您不能更改样式为下方的 Activity 的方向
<item name="android:windowIsTranslucent">true</item>
You have several way to solving this :
你有几种方法来解决这个问题:
1)You can simply remove above line (or turn it to false) and your app works fine.
1)您可以简单地删除上面的行(或将其变为false)并且您的应用程序运行良好。
2)Oryou can first removebelow line from manifest for that activity
2)或者您可以先从该活动的清单中删除以下行
android:screenOrientation="portrait"
Then you must addthis line to your activity (in onCreate())
然后您必须将此行添加到您的活动中(在 onCreate() 中)
//android O fix bug orientation
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
3)You can create new styles.xml
in values-v26
folder and add this to your style.xml
. (Thanks to AbdelHadycomment)
3)您可以styles.xml
在values-v26
文件夹中创建新的并将其添加到您的style.xml
. (感谢AbdelHady评论)
<item name="android:windowIsTranslucent">false</item>