0

I am getting this error when calling a method that in turn calls AlertDialog

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tylerhosting.hoot.wj2.beta, PID: 6424
android.content.res.Resources$NotFoundException: Resource ID #0x0
    at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:204)
    at android.content.res.Resources.loadXmlResourceParser(Resources.java:2133)
    at android.content.res.Resources.getLayout(Resources.java:1142)
    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:989)
    at android.support.v7.app.AlertController$AlertParams.apply(AlertController.java:965)
    at android.support.v7.app.AlertDialog$Builder.create(AlertDialog.java:982)
    at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:1006)
    at com.tylerhosting.hoot.hoot.FileDialog.createFileDialog(FileDialog.java:87)
    at com.tylerhosting.hoot.hoot.FileDialog.showDialog(FileDialog.java:118)
    at com.tylerhosting.hoot.hoot.SearchActivity.selectFile(SearchActivity.java:3954)

It is a mystery because I use the same code in other activities in the same app with no problem. Also mysterious is that it works on some devices,emulators, i.e. it works on API 23 device and emulator, and an API 28 emulator, but gives me an error on API 27.

public void selectFile() {
    if (!databaseAccess.permission(this))
        return;
    File mPath = new File(Environment.getExternalStorageDirectory() +  "//Documents//" );
    FileDialog fileDialog = new FileDialog(this, mPath, "txt");
    // only supports one file extension

    fileDialog.addFileListener(new FileDialog.FileSelectedListener() {
        public void fileSelected(File file) {
            String full = file.getAbsolutePath();
            importfile.setText(full);
        }
    });
    fileDialog.showDialog();
}

The fileDialog method that I call was one I got from https://stackoverflow.com/a/13986063/4106573

and apparently occurs when attempting to show the dialog using

    dialog = builder.show();
    return dialog;

I've seen this before and it had to do with invalid context, so I suspect it is not recognizing the Activity.

I thought it might be because it
'implements PopupMenu.OnMenuItemClickListener' but modifying it doesn't change anything.

Following is the theme and supporting styles.

<style name="DarkTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="colorPrimary">@color/blackBackground</item>
    <item name="colorPrimaryDark">@color/blackBackground</item>
    <item name="colorAccent">@color/yellow</item>
    <item name="android:textColor">@color/whiteText</item>
    <item name="android:actionMenuTextColor">@color/whiteText</item>

    <item name="android:windowBackground">@color/blackBackground</item>
    <item name="background">@color/blackBackground</item>
    <!--        <item name="android:background">@color/blackBackground</item> -->

    <item name="buttonStyle">@style/darkButtonStyle</item>
    <item name="spinnerStyle">@style/darkSpinnerStyle</item>
    <item name="android:listViewStyle">@style/darkListView</item>
    <item name="popupMenuStyle">@style/darkContextMenu</item>
    <item name="alertDialogStyle">@style/darkAlertDialog</item>

    <item name="actionBarStyle">@style/darkActionBar</item>
    <!--To change the text styling of options menu items</item>-->
    <item name="android:itemTextAppearance">@style/darkActionBar.MenuTextStyle</item>
    <!--To change the background of options menu-->
    <item name="android:itemBackground">@color/blackBackground</item>
    <item name="android:fastScrollTrackDrawable">@drawable/scrolltrack</item>

    <!-- if using android.widget.PopupMenu -->
    <item name="android:popupMenuStyle">@style/darkPopupMenu</item>
    <!-- if using android.support.v7.widget.PopupMenu
    <item name="popupMenuStyle">@style/darkPopupMenu</item>-->
</style>

<style name="darkButtonStyle" parent="@android:style/Widget.Button">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:textColor">@color/whiteText</item>
    <item name="android:textSize">16sp</item>
    <item name="android:padding">10dp</item>
    <item name="android:textStyle">bold</item>
    <item name="android:background">@color/darkBackground</item>
    <item name="android:gravity">center</item>
    <item name="android:textAllCaps">true</item>
</style>

<style name="darkSpinnerStyle" parent="Widget.AppCompat.Spinner">
    <item name="android:background">@color/blackBackground</item>
    <item name="background">@color/blackBackground</item>
    <item name="android:popupBackground">@color/blackBackground</item>

    <item name="android:paddingTop">0dp</item>
    <item name="android:layout_marginStart">1dp</item>
    <item name="android:layout_marginLeft">1dp</item>
    <item name="android:layout_width">0dp</item>
    <item name="android:layout_height">0dp</item>
</style>

<style name="darkListView" parent="Widget.AppCompat.ListView">
    <item name="android:background">@color/blackBackground</item>
    <item name="android:scrollbarAlwaysDrawVerticalTrack">true</item>
    <item name="android:fastScrollAlwaysVisible">true</item>
</style>

<style name="darkContextMenu" parent="Widget.AppCompat.PopupMenu">
    <item name="android:textColor">@color/whiteText</item>
    <item name="android:background">@color/blackBackground</item>
    <item name="android:popupBackground">@color/blackBackground</item>
</style>

<style name="darkAlertDialog" parent="Theme.AppCompat.Dialog.Alert">
    <item name="background">@color/blackBackground</item>
    <item name="android:background">@color/blackBackground</item>
    <item name="android:textColor">@color/whiteText</item>
    <item name="android:titleTextColor">@color/whiteText</item>
    <item name="android:colorAccent">#0000FF</item>
</style>

<style name="darkProgressBar" parent="@android:style/Widget.ProgressBar">
    <item name="background">@color/blackBackground</item>
    <item name="android:textColor">@color/whiteText</item>
    <item name="android:titleTextColor">@color/whiteText</item>
    <item name="android:colorAccent">#0000FF</item>
</style>

<style name="darkPopupMenu" parent="Widget.AppCompat.PopupMenu">
    <item name="android:popupBackground">@color/whiteBackground</item>
    <item name="android:background">@color/whiteBackground</item>
    <item name="android:textColor">@color/blackText</item>
</style>

<style name="darkActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="background">@color/blackBackground</item>
    <item name="titleTextStyle">@style/darkActionBarTitle</item>
</style>

<style name="darkActionBarTitle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="background">@color/blackBackground</item>
    <item name="android:textColor">@color/whiteText</item>
</style>

<style name="darkActionBar.MenuTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@color/whiteText</item>
    <item name="android:titleTextColor">@color/whiteText</item>
</style>
Dana Bell
  • 69
  • 6
  • Seems more likely to be a theme issue. Which theme are you using on the `Activity` with the problem? – Mike M. Sep 17 '20 at 22:02
  • Aha! I was using a custom dark theme. After going back to regular theme it works. How would I fix it to use the custom theme? – Dana Bell Sep 18 '20 at 17:35
  • I'm guessing that the custom theme is not an `AppCompat` theme, yeah? If so, then you just need to use an `AppCompat` theme for the `parent`, instead of whatever you have currently. If that's not it, please post your custom theme. – Mike M. Sep 18 '20 at 17:39
  • I was hoping. It wasn't based on AppCompat so I changed so that it was. Same error. Within the theme, though, the Alert Dialog was based on Theme.AppCompat.Dialog.Alert so it should've worked. I'll post the theme below. – Dana Bell Sep 18 '20 at 21:23

1 Answers1

2

I found a remedy. In the FileDialog class I changed the CreateFileDialog method to specify the regular theme when creating the builder.

Instead of

    AlertDialog.Builder builder = new AlertDialog.Builder(activity);

I use

    AlertDialog.Builder builder = new AlertDialog.Builder(activity, R.style.LightTheme);

Apparently there is a problem with the Dark Theme, but I want it to be light anyway so I'm good.

Elsewhere in the app I noticed that I used

    final AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.darkAlertDialog);

to specify the dark version. I hope that helps others.

Dana Bell
  • 69
  • 6