Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - android.content.res.Resources$NotFoundException: Resource ID #0x0

I'm trying to write an Android app that would browse the files on a phone. When I run the app on the emulator, I get the following errors:

02-18 22:14:17.839: E/AndroidRuntime(896): FATAL EXCEPTION: main
02-18 22:14:17.839: E/AndroidRuntime(896):     android.content.res.Resources$NotFoundException: Resource ID #0x0
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.content.res.Resources.getValue(Resources.java:1018)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.content.res.Resources.getDrawable(Resources.java:663)
02-18 22:14:17.839: E/AndroidRuntime(896):  at com.example.App.FileArrayAdapter.getView(FileArrayAdapter.java:56)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.widget.AbsListView.obtainView(AbsListView.java:2033)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.widget.ListView.makeAndAddView(ListView.java:1772)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.widget.ListView.fillDown(ListView.java:672)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.widget.ListView.fillFromTop(ListView.java:732)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.widget.ListView.layoutChildren(ListView.java:1625)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.widget.AbsListView.onLayout(AbsListView.java:1863)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.view.View.layout(View.java:11278)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.view.View.layout(View.java:11278)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.view.View.layout(View.java:11278)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.view.View.layout(View.java:11278)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.os.Looper.loop(Looper.java:137)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.app.ActivityThread.main(ActivityThread.java:4424)
02-18 22:14:17.839: E/AndroidRuntime(896):  at java.lang.reflect.Method.invokeNative(Native Method)
02-18 22:14:17.839: E/AndroidRuntime(896):  at java.lang.reflect.Method.invoke(Method.java:511)
02-18 22:14:17.839: E/AndroidRuntime(896):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-18 22:14:17.839: E/AndroidRuntime(896):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-18 22:14:17.839: E/AndroidRuntime(896):  at dalvik.system.NativeStart.main(Native Method)

Here's the java code for FileArrayAdapter.java:

import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FileArrayAdapter extends ArrayAdapter<Item>{

private Context c;
private int id;
private List<Item> items;

public FileArrayAdapter(Context context, int textViewResourceId, List<Item> objects)
{
    super(context, textViewResourceId, objects);
    c = context;
    id = textViewResourceId;
    items = objects;
}

public Item getItem(int i)
{
    return items.get(i);
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    View v = convertView;

    if(v == null)
    {
        LayoutInflater vi = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(id, null);
    }

    final Item i = items.get(position);

    if(i != null)
    {
        TextView t1 = (TextView)v.findViewById(R.id.TextView01);
        TextView t2 = (TextView)v.findViewById(R.id.TextView02);
        TextView t3 = (TextView)v.findViewById(R.id.TextViewDate);

        ImageView imageCity = (ImageView) v.findViewById(R.id.fd_Icon1);
        String uri = "drawable/" + i.getImage();

        int imageResource = c.getResources().getIdentifier(uri, null, c.getPackageName());
        Drawable image = c.getResources().getDrawable(imageResource);
        imageCity.setImageDrawable(image);

        if(t1 != null)
            t1.setText(i.getName());

        if(t2 != null)
            t2.setText(i.getData());

        if(t3 != null)
            t3.setText(i.getDate());
    }

    return v;
}

}

UPDATE Here's the Item.java code:

public class Item implements Comparable<Item>{

private String name, data, date, path, image;

public Item(String n, String d, String dt, String p, String i)
{
    name = n;
    data = d;
    date = dt;
    path = p;
    image = i;
}

public String getName()
{
    return name;
}

public String getData()
{
        return data;
}

public String getDate()
{
        return date;
}

public String getPath()
{
        return path;
}

public String getImage() 
{
        return image;
}

@Override
public int compareTo(Item another) {
    // TODO Auto-generated method stub
    if(this.name != null)
        return this.name.toLowerCase().compareTo(another.getName().toLowerCase());
    else
        throw new IllegalArgumentException();
}

}
like image 921
user2201650 Avatar asked Feb 19 '15 03:02

user2201650


2 Answers

Don't worry . . .

I got the same problem before .Use my way to get drawable

 Drawable image = c.getResources().getDrawable(R.drawable.name_of_image_in_drawableFolder);

example :

Drawable image = c.getResources().getDrawable(R.drawable.ic_launcher);

This will definetely solove your problem.

But, If you dont have images in drawable folder There might be two posibilities

- Getting Drawable from Phone(SDCARD)
- Getting Drawable from url

In First Case

imageCity.setImageBitmap(BitmapFactory.decodeFile(Environment.getExternalStorage‌​Directory()+"image.png"));

In Second case

imageCity.setImageDrawable(drawableFromUrl("url")); 
    public Drawable drawableFromUrl(String url) throws IOException {
        Bitmap x;
        HttpURLConnection connection = (HttpURLConnection) new URL(url)
                .openConnection();
        connection.connect();
        InputStream input = connection.getInputStream();
        x = BitmapFactory.decodeStream(input);
        return new BitmapDrawable(x);
    }

Reference :Android Drawable Images from URL

If any problem persists you can contact me [email protected] .

like image 149
Gaganpreet Singh Avatar answered Nov 15 '22 19:11

Gaganpreet Singh


This error also appear when showing an AlertDialog in Android when it uses applicationContext.

val builder = AlertDialog.Builder(applicationContext)

the solution is to use this:

val builder = AlertDialog.Builder(this)
like image 31
hertzon Avatar answered Nov 15 '22 19:11

hertzon