java.lang.IllegalStateException:只有全屏不透明活动可以请求方向 [英] java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation
问题描述
我在 Android 8.0 Oreo java.lang.IllegalStateException 中从通讯录检索联系人时遇到问题:只有全屏不透明活动可以请求方向
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
我正在尝试从电话通讯簿中获取我的活动中的联系人,它非常适合棒棒糖、棉花糖、牛轧糖等,但它会给我这样的奥利奥错误,请帮助我.我的代码在下面.
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.
演示代码:-
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");
}
}
错误日志:-
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
推荐答案
当您的目标 sdk 为 28 时,问题似乎发生了.因此,在尝试了许多选项后,这终于奏效了.
The problem seems to be happening when your target sdk is 28. So after trying out many options finally this worked.
<activity
android:name=".activities.FilterActivity"
android:theme="@style/Transparent"
android:windowSoftInputMode="stateHidden|adjustResize" />
风格:-
<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>
注意:api 28 需要 parent="Theme.AppCompat.Light.NoActionBar".以前在 api 26 上有其他东西.运行良好,但在 28 时开始出现问题.希望它可以帮助这里的人.对于某些仅通过设置 <item name="android:windowIsTranslucent">false</item>和 <item name="android:windowIsFloating">false</item>
工作.可能取决于您实施解决方案的方式.在我的情况下,它通过将它们设置为 true 来工作.
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.
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.
这篇关于java.lang.IllegalStateException:只有全屏不透明活动可以请求方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!