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

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

我正在尝试编写一个 Android 应用程序来浏览 phone 上的文件。当我 运行 模拟器上的应用程序时,出现以下错误:

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)

这是 FileArrayAdapter.java 的 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;
}

}

更新 这是 Item.java 代码:

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

}

此代码导致了问题:

String uri = "drawable/" + i.getImage();
int imageResource = c.getResources().getIdentifier(uri, null, c.getPackageName());

这里至少有两个问题:

  1. uri 不应有 'drawable' 前缀
  2. getIdentifier的调用应该传递'drawable'作为资源类型

所以,试试这个代码:

String uri = i.getImage();
int imageResource = c.getResources().getIdentifier(uri, "drawable", c.getPackageName());

注意:您没有post Item class,或者getImage 做了什么。如果您仍然遇到异常(因为 imageResource 为 0),那是因为您没有以与 i.getImage() returns 完全相同的名称命名的可绘制资源,因此您需要也检查一下。

别担心。 . .

我之前遇到过同样的问题,用我的方法获取drawable

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

示例:

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

这绝对可以解决您的问题。

但是, 如果 drawable 文件夹中没有图像可能有两种可能性

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

第一种情况

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

第二种情况

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

参考:Android Drawable Images from URL

如果问题仍然存在,您可以联系我megaganpreet0@gmail.com

在使用 applicationContext 时在 Android 中显示 AlertDialog 时也会出现此错误。

val builder = AlertDialog.Builder(applicationContext)

解决方法是使用this:

val builder = AlertDialog.Builder(this)