Resources$NotFoundException: AlertDialog のリソース ID #0x0 質問する

Resources$NotFoundException: AlertDialog のリソース ID #0x0 質問する

私は を持っており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$2.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>

重要かどうかはわかりませんが、(v7 サポート ライブラリから)AlertDialogをインポートしています。android.support.v7.app.AlertDialog

この問題をどうすれば解決できますか?

ベストアンサー1

getBaseContext()インスタンス化をAlertDialog.Builder現在のインスタンスに変更しますActivity。例:

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

には、それが使用するAlertDialogにアタッチされたテーマやスタイルによって値が提供される特定のリソースが必要です。によって返される にはこれらのリソースはアタッチされていませんが、 にはアタッチされています。実際、 がUI コンポーネント (たとえば、、、など) に必要な場合は常に、通常は現在の を使用します。ContextContextgetBaseContext()ActivityContextDialogViewAdapterActivity

おすすめ記事