54

I have a textview, when it is clicked, I am populating a listView inside a dialog. This code used to work fine, but today it is throwing exception.

this is my code:

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

The logcat error shows this:

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)

Cant figure it out the problem. Any help please?

Bluemarble
  • 1,925
  • 4
  • 20
  • 35
  • is that DailyQuotesWidgetConfigureActivity.java:182? – Blackbelt Mar 17 '15 at 09:10
  • Hi, the line 182 is this: ListView listView = new ListView(context); listView.setAdapter(new ArrayAdapter(context, android.R.layout.simple_list_item_1, new String[] {"Default", "Serif", "Monospace"})); – Bluemarble Mar 17 '15 at 09:13
  • @BuggyCoder: try it as `dialog.setTitle(v.getContext().getResources().getString(R.string.txt_settings_QuotefontName))` instead of `dialog.setTitle(R.string.txt_settings_QuotefontName)` – ρяσѕρєя K Mar 17 '15 at 09:39
  • 1
    I had this issue when upgrading to use `compile 'com.android.support:appcompat-v7:23.4.0'` if I stay with `v7:22.2.0'` it works a charm – tread May 14 '16 at 15:15

12 Answers12

197

I encountered this problem in the recent app I made. In my case, the problem was I put an image in the folder called drawable-v21, which is not available in older android API.

The solution is to put your drawable in drawable-...dpi folders too.

starball
  • 20,030
  • 7
  • 43
  • 238
Dũng Trần Trung
  • 6,198
  • 3
  • 24
  • 20
  • 4
    Thanks!! a very simple solution to a big problem. – Pedro Varela May 05 '16 at 19:05
  • 1
    @codingpuss. My min SDK is Android 5.0. I don't need to do the things you mentioned, right? But i am still getting crashes on some devices running Android 5.1 – zulkarnain shah Jul 17 '18 at 09:43
  • 1
    did you put the graphic file in all drawable folders? – Dũng Trần Trung Jul 17 '18 at 12:11
  • Downgrading from 8 to 5.1.1, this is what I needed - thanks! – Rob Oct 30 '18 at 08:16
  • Yup, I had the same issue with a device of Android 7 . And for Later version's it was working fine. I placed the drawable in all dpi folders (hdpi, mdpi, xhdpi, xxhdpi, xxxhdpi) and it worked for me – i.am.jabi May 04 '20 at 07:11
  • Nice answer but I should add this additional information. To access drawble-v24 for instance you should change android studio files view to Project level so you can find drawable-v24 in res folder. then move all drawbles from it to drawble folder – Reza Oct 08 '20 at 09:48
  • Why does Android Studio do such a dangerous thing all by itself?!! – doctorram Jun 02 '22 at 21:26
57

In Android Studio change Project hierarchy to Project Files.

Then go to the res folder, you will see multiple drawable folders. Copy the images to appropriate folder(drawable) or based on Api level.

In my case image was present in drawable-24 folder therefore it was not available on api<24 devices.

Screenshot.

Salman
  • 781
  • 6
  • 9
  • 5
    If I could buy you a beer, I'd buy you a case. I've spent hours on this and your fix was exactly the issue. Thank you. – DJFriar May 16 '19 at 06:09
  • 3
    This should be marked as the correct answer to the question. Thanks. – Alvaro May 26 '19 at 09:24
  • 1
    Thanks. I was scratching my head trying to figure out why my app works great on my regular test device (LG G3, running api 28) but not on api 23 of my phone, which is, as you pointed out, less than 24. The actual error I was given was Error inflating class "androidx.appcompat.widget.Toolbar", which made it look like androix is not supported. – ShayR Jul 14 '19 at 02:32
14

On the Mac in finder, I just simply moved all the files in the folder ../drawable/drawable-24 to /drawable and everything worked both on older android versions and Oreo. Also when you copy and paste the images into Android Studio make sure to paste them into drawable not drawable//drawable-24 which may be the default.

10

I figured it out, it was not an issue with the code, but the theme. I recently changed the theme from android:theme="@style/AppTheme"to android:theme="@style/Theme.AppCompat.NoActionBar" after that the problem started. I reverted back the old AppTheme thene it started working again. It seems that the NoActionBar theme was not compatible for dialog boxes.

Bluemarble
  • 1,925
  • 4
  • 20
  • 35
2

I ran into this error in a different situation, and it turned out that I'd accidentally set a drawable to R.id.something instead of R.drawable.something!

Sam
  • 40,644
  • 36
  • 176
  • 219
  • Thank you for this, I had set a drawable in my XML as `@id/something` and this was the cause of this crash! – Lucas P. Feb 28 '18 at 15:23
  • @Sam. My min SDK is Android 5.0. I don't need to do the things you mentioned, right? But i am still getting crashes on some devices running Android 5.1 – zulkarnain shah Jul 17 '18 at 09:42
  • @zulkarnainshah, as far as I remember, I had accidentally tried to use an id as a drawable, so the error message (`Resource is not a Drawable`) was pretty accurate in my case. This probably applies regardless of the Android version since ids are not drawables. – Sam Jul 17 '18 at 22:05
2

Make sure you followed the above solutions, try this if non-of-them worked for you.

In my case, the problem was due to drawable icons in only hdpi image resolution. Change these icons to the folder, which contains xhdpi, mdpi, xxhdpi and xxxhdpi resolution also.

And that solved my problem.

PS:

I convert icons to different image resolution by installing plugin "Android drawable importer".

meyasir
  • 268
  • 3
  • 12
0

Maybe your view background resource not in drawable dir, in color. view background not support resource in color.

view background like this

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_selected="true">
            <shape>
                <solid android:color="#ffffffff"/>
            </shape>
        </item>
        <item android:state_selected="false">
            <shape>
                <solid android:color="#00000000"/>
            </shape>
        </item>
    </selector>
lingyfh
  • 1,363
  • 18
  • 23
0

You can get it working by making sure that your drawable.xml not included (v24)

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Abdelaziz refaat
  • 157
  • 1
  • 12
0
<ImageView
...
            android:src="@drawable/realtconsult" />

If you will write it from a view then this will works.

Iefimenko Ievgen
  • 379
  • 1
  • 5
  • 12
0

There are more than one folder included in drawable folders(not visible), if you get these kind of error then please copy paste the file on which error occured to all the directories or atleast copy paste to night and normal drwable folder

enter image description here

0

When you put an image into the drawable folder, by default they are -v24 named at the end.

If you are not sure, just drag and drop the image again into the drawable folder. Make sure the name is not -v24 at the end.

Solution for old APIs

J A S K I E R
  • 1,976
  • 3
  • 24
  • 42
-1

move all your ic_xyz.xml folders to drawable (v24) ie select your folder which is having this issue and move it to or select---> refactor--->move file-->(change the path) to Drawable(v24).