Resources$NotFoundException:AlertDialog 中的资源 ID #0x0
Resources$NotFoundException: Resource ID #0x0 in AlertDialog
我有一个 RecyclerView
,在它的适配器中,我创建了一个类似于 OnLongClickListener
的东西,我称之为 OnEntryLongClickListener
以避免混淆。
我正在使用 AlertDialog
来显示一个对话框,其中包含用于不同操作的列表项。但是,我收到以下错误:
E/AndroidRuntime: android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.Resources.getValue(Resources.java:2345)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:3910)
at android.content.res.Resources.getLayout(Resources.java:2161)
at android.view.LayoutInflater.inflate(LayoutInflater.java:413)
at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
at android.support.v7.app.AlertController$AlertParams.createListView(AlertController.java:734)
at android.support.v7.app.AlertController$AlertParams.apply(AlertController.java:711)
at android.support.v7.app.AlertDialog$Builder.create(AlertDialog.java:883)
at com.mycompany.myapp.ThisActivity.onEntryLongClick(ThisActivity.java:135)
at com.mycompany.myapp.adapter.RVAdapter$RVViewHolder.onLongClick(RVAdapter.java:41)
at android.view.View.performLongClick(View.java:5236)
下面是我使用的相关代码:
adapter.setOnEntryLongClickListener(new RVAdapter.OnEntryLongClickListener() {
@Override
public void onEntryLongClick(View view, int position) {
final MiniEntry thisEntry = entryList.get(position);
AlertDialog.Builder builder = new AlertDialog.Builder(getBaseContext());
builder.setTitle(thisEntry.getEntryName()););
builder.setItems(R.array.quickActions, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Other code here
}
});
AlertDialog alert = builder.create(); // The error log points to this line
alert.show();
}
});
mRecyclerView.setAdapter(adapter);
以及我用于数组的 XML:
<string-array name="quickActions">
<item>Add to Favourites</item>
<item>More information</item>
</string-array>
我不确定它是否重要,但我正在从 android.support.v7.app.AlertDialog
(来自 v7 支持库)导入 AlertDialog
。
我该如何解决这个问题?
将 AlertDialog.Builder
实例化中的 getBaseContext()
更改为当前 Activity
实例。例如:
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
AlertDialog
需要某些资源,其值由附加到它使用的 Context
的主题和样式提供。 getBaseContext()
返回的 Context
没有附加那些,但 Activity
有。实际上,每当 UI 组件需要 Context
- 例如,Dialog
s、View
s、Adapter
s 等 - 当前 Activity
通常是您要使用的。
尝试为您的对话框添加一个扩展 Theme.AppCompat.Light.Dialog.Alert
的样式
<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert" />
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.MyDialogTheme);
这对我有用。
问候
如果你像我一样只使用 Material 没有支持库的设计组件,你可以使用这个构造函数。
// in app build.gradle file
// make sure you have the MDC depencency.
// https://material.io/develop/android/docs/getting-started
implementation "com.google.android.material:material:${materialcomponents_version}"
// MainActivity.kt
// creating the builder
val dialogBuilder = MaterialAlertDialogBuilder(this)
//... setting up dialog
// creating dialog successfully
val alert: AlertDialog = dialogBuilder.create()
我有一个 RecyclerView
,在它的适配器中,我创建了一个类似于 OnLongClickListener
的东西,我称之为 OnEntryLongClickListener
以避免混淆。
我正在使用 AlertDialog
来显示一个对话框,其中包含用于不同操作的列表项。但是,我收到以下错误:
E/AndroidRuntime: android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.Resources.getValue(Resources.java:2345)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:3910)
at android.content.res.Resources.getLayout(Resources.java:2161)
at android.view.LayoutInflater.inflate(LayoutInflater.java:413)
at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
at android.support.v7.app.AlertController$AlertParams.createListView(AlertController.java:734)
at android.support.v7.app.AlertController$AlertParams.apply(AlertController.java:711)
at android.support.v7.app.AlertDialog$Builder.create(AlertDialog.java:883)
at com.mycompany.myapp.ThisActivity.onEntryLongClick(ThisActivity.java:135)
at com.mycompany.myapp.adapter.RVAdapter$RVViewHolder.onLongClick(RVAdapter.java:41)
at android.view.View.performLongClick(View.java:5236)
下面是我使用的相关代码:
adapter.setOnEntryLongClickListener(new RVAdapter.OnEntryLongClickListener() {
@Override
public void onEntryLongClick(View view, int position) {
final MiniEntry thisEntry = entryList.get(position);
AlertDialog.Builder builder = new AlertDialog.Builder(getBaseContext());
builder.setTitle(thisEntry.getEntryName()););
builder.setItems(R.array.quickActions, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Other code here
}
});
AlertDialog alert = builder.create(); // The error log points to this line
alert.show();
}
});
mRecyclerView.setAdapter(adapter);
以及我用于数组的 XML:
<string-array name="quickActions">
<item>Add to Favourites</item>
<item>More information</item>
</string-array>
我不确定它是否重要,但我正在从 android.support.v7.app.AlertDialog
(来自 v7 支持库)导入 AlertDialog
。
我该如何解决这个问题?
将 AlertDialog.Builder
实例化中的 getBaseContext()
更改为当前 Activity
实例。例如:
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
AlertDialog
需要某些资源,其值由附加到它使用的 Context
的主题和样式提供。 getBaseContext()
返回的 Context
没有附加那些,但 Activity
有。实际上,每当 UI 组件需要 Context
- 例如,Dialog
s、View
s、Adapter
s 等 - 当前 Activity
通常是您要使用的。
尝试为您的对话框添加一个扩展 Theme.AppCompat.Light.Dialog.Alert
的样式<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert" />
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.MyDialogTheme);
这对我有用。
问候
如果你像我一样只使用 Material 没有支持库的设计组件,你可以使用这个构造函数。
// in app build.gradle file
// make sure you have the MDC depencency.
// https://material.io/develop/android/docs/getting-started
implementation "com.google.android.material:material:${materialcomponents_version}"
// MainActivity.kt
// creating the builder
val dialogBuilder = MaterialAlertDialogBuilder(this)
//... setting up dialog
// creating dialog successfully
val alert: AlertDialog = dialogBuilder.create()