我无法让Jetpack / AndroidX首选项与MultiSelectListPreference
一起使用。我已将代码缩减为最简单的示例项目:https://github.com/quittle/a11y-ally/tree/simple-multi-list 。
由于某些原因,使用AndroidX类时,具有单个首选项片段和一个空MultiSelectListPreference的最基本的Activity崩溃,但不适用于Android SDK级别28中已弃用的原始Android API。
>我发现了https://code.videolan.org/videolan/vlc-android/commit/1464d1a6ab882eec78ab3a749e8f06e52bbd7404,这似乎表明这可能是AppCompat库的问题,但是当改用AndroidX库时并不能解决我的设置。
使用偏好设置的最小代码段
public class VanillaMainActivity extends Activity {
public static class CustomPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
getFragmentManager()
.beginTransaction()
.replace(R.id.preferences_holder, new CustomPreferenceFragment())
.commit();
}
}
public class AndroidXMainActivity extends FragmentActivity {
public static class CustomPreferenceFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) {
addPreferencesFromResource(R.xml.preferences);
}
}
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.preferences_holder, new CustomPreferenceFragment())
.commit();
}
}
使用AndroidX库时获得的堆栈跟踪如下。
2018-11-05 20:32:22.318 18080-18080/com.example E/com.example: Invalid ID 0x00000000.
2018-11-05 20:32:22.320 18080-18080/com.example E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example, PID: 18080
android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:216)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2155)
at android.content.res.Resources.getLayout(Resources.java:1155)
at android.view.LayoutInflater.inflate(LayoutInflater.java:421)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AlertController$AlertParams.createListView(AlertController.java:989)
at androidx.appcompat.app.AlertController$AlertParams.apply(AlertController.java:965)
at androidx.appcompat.app.AlertDialog$Builder.create(AlertDialog.java:982)
at androidx.preference.PreferenceDialogFragmentCompat.onCreateDialog(PreferenceDialogFragmentCompat.java:158)
at androidx.fragment.app.DialogFragment.onGetLayoutInflater(DialogFragment.java:330)
at androidx.fragment.app.Fragment.performGetLayoutInflater(Fragment.java:1308)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at androidx.fragment.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
答案 0 :(得分:0)
在提交错误报告后,我被告知失败的原因是我在活动中没有类似Theme.AppCompat
的App Compat主题,因此缺少资源异常。将其添加到活动中后,它将按预期工作。