资源$ NotFoundException:资源不是Drawable(颜色或路径)?

Bug*_*der 31 android

我有一个textview,当它被点击时,我在对话框中填充listView.这段代码以前工作正常,但今天却抛出了异常.

这是我的代码:

tvSelectedFont = (TextView)findViewById(R.id.lblQuoteSelectedFont);
    tvSelectedFont.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            ListView listView = new ListView(context);
            listView.setAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1,
                    new String[] {"Default", "Serif", "Monospace"}));
            final Dialog dialog = new Dialog(context);
            dialog.setContentView(listView);
            dialog.setTitle(R.string.txt_settings_QuotefontName);

            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String selectedTypeFace = ((TextView)view).getText().toString();
                    tvSelectedFont.setText(selectedTypeFace);
                    switch(selectedTypeFace)
                    {
                        case "Serif":
                            selectedQuoteTypeFace = Typeface.SERIF;
                            break;
                        case "Monospace":
                            selectedQuoteTypeFace = Typeface.MONOSPACE;
                            break;
                        default:
                            selectedQuoteTypeFace = Typeface.DEFAULT;
                            break;
                    }
                    tvQuoteTextSample.setTypeface(selectedQuoteTypeFace, selectedQuoteFontStyle);
                    dialog.dismiss();
                }
            });

            dialog.show();
        }
    });
Run Code Online (Sandbox Code Playgroud)

logcat错误显示如下:

Device driver API version: 29
User space API version: 29
03-17 14:33:24.701  23220-23220/com.example.manas.dailyquoteswidget E/? mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Tue Jul 22 19:59:34 KST 2014
03-17 14:33:27.926  23220-23220/com.example.manas.dailyquoteswidget E/AndroidRuntime? FATAL EXCEPTION: main
Process: com.example.manas.dailyquoteswidget, PID: 23220
android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x7f0100a7 a=3}
        at android.content.res.Resources.loadDrawable(Resources.java:3415)
        at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
        at android.widget.AbsListView.<init>(AbsListView.java:1089)
        at android.widget.ListView.<init>(ListView.java:152)
        at android.widget.ListView.<init>(ListView.java:148)
        at android.widget.ListView.<init>(ListView.java:144)
        at com.example.manas.dailyquoteswidget.DailyQuotesWidgetConfigureActivity$6.onClick(DailyQuotesWidgetConfigureActivity.java:182)
        at android.view.View.performClick(View.java:4640)
        at android.view.View$PerformClick.run(View.java:19425)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5593)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
        at dalvik.system.NativeStart.main(Native Method)
Run Code Online (Sandbox Code Playgroud)

无法弄清楚问题所在.有什么帮助吗?

cod*_*uss 136

我在最近的应用程序中遇到了这个问题.在我的情况下,问题是我把一个图像放在名为drawable-v21的文件夹中,这在旧的Android API中是不可用的.

解决方案是将drawable放在drawable -... dpi文件夹中.

希望有所帮助.

  • 谢谢!!解决一个大问题的一个非常简单的方法. (2认同)

Sal*_*man 30

在Android Studio中,将Project层次结构更改为Project Files.

然后转到res文件夹,您将看到多个可绘制文件夹.将图像复制到适当的文件夹(可绘制)或基于Api级别.

在我的情况下,图像存在于drawable-24文件夹中,因此它在api <24设备上不可用.

截图.

  • 如果我能给你买一瓶啤酒,我会给你买一箱。我在这上面花了几个小时,而你的修复正是问题所在。谢谢你。 (4认同)
  • 这应该被标记为问题的正确答案。谢谢。 (3认同)

小智 11

在 finder 中的 Mac 上,我只是简单地将文件夹 ../drawable/drawable-24 中的所有文件移动到 /drawable 并且所有内容都适用于较旧的 android 版本和 Oreo。此外,当您将图像复制并粘贴到 Android Studio 中时,请确保将它们粘贴到 drawable 而不是 drawable//drawable-24 这可能是默认设置。

  • 我将 /drawable-24 中的所有内容都移到了 /drawable。工作了! (3认同)

Bug*_*der 10

我想通了,这不是代码的问题,而是主题.我最近改变了主题android:theme="@style/AppTheme",android:theme="@style/Theme.AppCompat.NoActionBar"之后问题开始了.我恢复了旧的AppTheme thene它再次开始工作.似乎NoActionBar主题与对话框不兼容.