Resources $ NotFoundException:AlertDialog中的资源ID#0x0 [英] Resources$NotFoundException: Resource ID #0x0 in AlertDialog

查看:145
本文介绍了Resources $ NotFoundException:AlertDialog中的资源ID#0x0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RecyclerView,并且在它的适配器中,我创建了一个与OnLongClickListener类似的东西,为了避免混淆,我将其称为OnEntryLongClickListener.

I have a RecyclerView, and in its adapter, I have created something similar to an OnLongClickListener, which I am calling an OnEntryLongClickListener to avoid confusion.

我正在使用AlertDialog来显示一个对话框,其中包含用于不同操作的列表项.但是,出现以下错误:

I am using an AlertDialog to display a dialog with list items for different actions. However, I am getting the following error:

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$2.onEntryLongClick(ThisActivity.java:135)  
    at com.mycompany.myapp.adapter.RVAdapter$RVViewHolder.onLongClick(RVAdapter.java:41)   
    at android.view.View.performLongClick(View.java:5236)  

以下是我正在使用的相关代码:

Below is the relevant code I am using:

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:

As well as the XML I am using for the array:

<string-array name="quickActions">
    <item>Add to Favourites</item>
    <item>More information</item>
</string-array>

我不确定这是否重要,但是我正在从android.support.v7.app.AlertDialog(从v7支持库)导入AlertDialog.

I'm not sure if it matters, but I am importing the AlertDialog from android.support.v7.app.AlertDialog (from the v7 Support Library).

我该如何解决这个问题?

How can I solve this problem?

推荐答案

AlertDialog.Builder实例中的getBaseContext()更改为当前Activity实例.例如:

Change getBaseContext() in the AlertDialog.Builder instantiation to the current Activity instance. For example:

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

AlertDialog需要某些资源,这些资源的值由与其使用的Context关联的主题和样式提供. getBaseContext()返回的Context没有这些附件,但是Activity有.实际上,每当UI组件需要Context时(例如Dialog s,View s,Adapter s等),当前的Activity通常就是您要使用的.

An AlertDialog requires certain resources whose values are provided by the themes and styles attached to the Context it uses. The Context returned by getBaseContext() doesn't have those attached, but the Activity does. Indeed, whenever a Context is needed for a UI component - e.g., Dialogs, Views, Adapters, etc. - the current Activity is usually what you want to use.

这篇关于Resources $ NotFoundException:AlertDialog中的资源ID#0x0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆