22

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.

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);

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>

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?

Pang
  • 9,564
  • 146
  • 81
  • 122
Farbod Salamat-Zadeh
  • 19,687
  • 20
  • 75
  • 125
  • 1
    Change `getBaseContext()` in the `AlertDialog.Builder` instantiation to the current Activity instance. For example, `new AlertDialog.Builder(ThisActivity.this)`. – Mike M. Oct 08 '15 at 21:03
  • @MikeM. This worked! Could you post this as an answer and *explain why it solved the problem* as well - thanks. – Farbod Salamat-Zadeh Oct 08 '15 at 21:05

3 Answers3

31

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

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

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.

Mike M.
  • 38,532
  • 8
  • 99
  • 95
26

Try putting an style for your Dialog that extends Theme.AppCompat.Light.Dialog.Alert

<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert" />

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.MyDialogTheme);

This works for me.

Greetings

Franrc
  • 286
  • 3
  • 5
0

If you, like me, is only using Material Design Components without support library, you can use this constructor.

// 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()
Akhha8
  • 418
  • 3
  • 10