我正在尝试编写一款能够浏览手机上文件的Android应用。当我在模拟器上运行应用程序时,出现以下错误:
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;
}
}
UPDATE 以下是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();
}
}
答案 0 :(得分:6)
此代码导致问题:
String uri = "drawable/" + i.getImage();
int imageResource = c.getResources().getIdentifier(uri, null, c.getPackageName());
这里至少有两个问题:
getIdentifier
的电话应该通过&#39; drawable&#39;作为资源类型所以,试试这段代码:
String uri = i.getImage();
int imageResource = c.getResources().getIdentifier(uri, "drawable", c.getPackageName());
注意:您没有发布Item
课程,或getImage
发布的内容。如果你仍然得到异常(因为imageResource为0),那是因为你没有一个名为与i.getImage()返回的名称完全相同的可绘制资源,所以你也需要检查它。
答案 1 :(得分:4)
之前我遇到了同样的问题。用我的方式获得可绘制的
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.getExternalStorageDirectory()+"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。
答案 2 :(得分:-1)
使用AlertDialog
在Android中显示applicationContext
时也会出现此错误。
val builder = AlertDialog.Builder(applicationContext)
解决方案是使用this
:
val builder = AlertDialog.Builder(this)