10

I'm getting an exception when trying to display a dialog box in Android. My AlertDialog is called from a FragmentActivity with the following code:

public static void displayShare(){
    // show share options
    CharSequence selections[] = new CharSequence[] {"Email", "SMS", "Tweet", "Phone Call", "Cancel"};
    final AlertDialog.Builder builder = new AlertDialog.Builder(CommonVariables.mContext);
    builder.setTitle("Share your location via...");
    builder.setItems(selections, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch(which){
                case 0: // Email
                   callEmailMethod();
                    break;
                case 1: // SMS
                    callSMSMethod();
                    break;
                case 2: // Tweet
                    callTwitterMethod();
                    break;
                case 3: // Phone Call
                    callNumberMethod();
                    break;
                case 4:
                    dialog.cancel();
                    break;
            }
        }
    });
    builder.show();
}

The following error is received at the line: builder.show();

    FATAL EXCEPTION: main
Process: com.au.ewn.melbwater, PID: 2839
android.content.res.Resources$NotFoundException: Resource ID #0x0
    at android.content.res.Resources.getValue(Resources.java:1351)
    at android.content.res.Resources.loadXmlResourceParser(Resources.java:2774)
    at android.content.res.Resources.getLayout(Resources.java:1165)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:421)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
    at android.support.v7.app.AlertController$AlertParams.createListView(AlertController.java:879)
    at android.support.v7.app.AlertController$AlertParams.apply(AlertController.java:856)
    at android.support.v7.app.AlertDialog$Builder.create(AlertDialog.java:899)
    at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:917)
    at com.au.ewn.activities.MainFragment.displayShare(MainFragment.java:1081)
    at com.au.ewn.activities.HelpMeScreen$2.onClick(HelpMeScreen.java:257)
    at android.view.View.performClick(View.java:5198)
    at android.view.View$PerformClick.run(View.java:21147)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

I have tried everything (except the correct solution, it seems). Any help is appreciated, thanks.

Note: CommonVariables.mContext is the context of the FragmentActivity and is not null: CommonVariables.mContext = FragmentAct.this;

Nickmccomb
  • 1,467
  • 3
  • 19
  • 34
  • 1
    http://stackoverflow.com/questions/33025719/resourcesnotfoundexception-resource-id-0x0-in-alertdialog – Bill Jul 13 '16 at 01:27
  • Are you sure `mContext` is current and not referring to some past instantiation of the Activity? – sleep Jul 13 '16 at 01:33
  • The accepted answer didn't help, i tried that an hour ago. The other comment did, there was no Style resource for the AlertDialog, which there used to be..The joys of working with multiple developers. Thanks for pointing me back to that! – Nickmccomb Jul 13 '16 at 01:34

2 Answers2

23

The problem was that my project was missing the style resource for the AlertDialog:

In styles.xml put this:

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

In your code where you create the Alert Dialog put this:

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

Thanks for @Fraranc in this post for the answer : Resources$NotFoundException: Resource ID #0x0 in AlertDialog

Community
  • 1
  • 1
Nickmccomb
  • 1,467
  • 3
  • 19
  • 34
2

Sorry if I late to the party. But I also experienced this issue and those 3 solutions I found in internet like :

  1. Use baseContext.
  2. Using when initiate dialog.
  3. Missing layout in specific drawable folder.

what helped me solved the issue is my alerdialog is located in fragment, and it solved by calling requiredContext() instead of baseContext or applicationContext when initiating alertdialog.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Bhimbim
  • 1,348
  • 2
  • 19
  • 15