java.lang.NoSuchMethodError: package.call.getDrawable

2.6k views Asked by At

I am creating a small android app that has a list of hotels and restaurants and shows their details on click. During compilation time I have no errors, but on runtime I am having the error "java.lang.NoSuchMethodError: package.class.getDrawable" when it tries to load the image. Previously it showed the error "Call requires API level 21 (current min is 11)" and after I changed the min API level to 16, it disappeared from compilation time as an error, but remained in runtime. I tried to change the min API to 21 and it now works, but by this way I reduce a lot the range of compatible devices. The minimal API must be at least 16. I have also tried @SuppressLint("NewApi") but it gave the error anyway.

The details are stored in a .xml file so I can edit it in later times. For the image value in the xml i have stored the path of the local resource:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <hotel>
        <KEY_KEY_TITLE>Name of hotel</KEY_KEY_TITLE>
        <KEY_IMGLARGE>@drawable/hotel_image</KEY_IMGLARGE>
        <KEY_ADDR>street somewhere</KEY_ADDR>
        <KEY_TEL>0011223344</KEY_TEL>
        <KEY_EMAIL>[email protected]</KEY_EMAIL>
        <KEY_DESC>@string/hotel_desc</KEY_DESC>
        <KEY_CITY>CITY</KEY_CITY>
    </hotel>
</resources>

This is the code where I try to retrieve the image:

public View getView(int position, View convertView, ViewGroup parent) {

        View rowItem=convertView;
        if(rowItem==null){
            rowItem=getLayoutInflater().inflate(R.layout.row_layout, parent, false);
        }
        Hotel theHotel=hotels.get(position);//assign the object from the list
        ImageView img=(ImageView) rowItem.findViewById(R.id.item_Image);
        int drawableId;
        try{
            Class<drawable> res = R.drawable.class;
            Field field = res.getField(theHotel.getImage().substring(10, theHotel.getImage().length()));

            drawableId = field.getInt(null);
            img.setImageDrawable(getDrawable(drawableId));//error happens here in runtime
        }catch (Exception e) {
            Log.d("MyTag", "Failure to get drawable id.", e);
        }
        TextView title=(TextView) rowItem.findViewById(R.id.item_Title);
        title.setText(theHotel.getTheTitle()+" - "+theHotel.getCity());//set the title of the hotel and city

        return rowItem;
    }

Is there any workaround for this issue? I am new to android and the minimal thing I want to fix in this case is the loading dynamically of the image. If there are any better ways please tell me(and there is any code sample it is better :) )

Regards.

3

There are 3 answers

0
g_brahimaj On BEST ANSWER

I found a workaround:

In try-catch I have edited the following lines:

try{
                //Class<drawable> res = R.drawable.class;
                //Field field = res.getField(theHotel.getImage().substring(10, theHotel.getImage().length()));              
                //drawableId = field.getInt(null);

                drawableId=getResources().getIdentifier(theHotel.getImage().substring(1, theHotel.getImage().length()), null, getPackageName());
                Drawable theImage=getResources().getDrawable(drawableId);
                img.setImageDrawable(theImage);
            }

This solves the issue but I am afraid for performance issues. If you have any better solutions please insert it in the comments, I opened to any better one.

1
Ranjana Dangol On

Remove null from the getint.

drawableId = field.getInt();
img.setImageDrawable(getDrawable(drawableId));
0
Riot Goes Woof On

For me, calling it like this works:

image.setImageDrawable(getResources().getDrawable(R.drawable.your_drawable_here));