我应该在哪里查看我的应用代码来解决抛出此异常的问题?我无法解释它:
04-10 13:51:21.301: E/AndroidRuntime(1161): FATAL EXCEPTION: main
04-10 13:51:21.301: E/AndroidRuntime(1161): Process: com.solinpromex.vivegrancanaria, PID: 1161
04-10 13:51:21.301: E/AndroidRuntime(1161): android.content.res.Resources$NotFoundException: Resource ID #0x0
04-10 13:51:21.301: E/AndroidRuntime(1161): at android.content.res.Resources.getValue(Resources.java:1123)
04-10 13:51:21.301: E/AndroidRuntime(1161): at android.content.res.Resources.getDrawable(Resources.java:698)
04-10 13:51:21.301: E/AndroidRuntime(1161): at com.solinpromex.vivegrancanaria.Ofertas_ApplicationAdapter.getView(Ofertas_ApplicationAdapter.java:49)
04-10 13:51:21.301: E/AndroidRuntime(1161): at android.widget.AbsListView.obtainView(AbsListView.java:2263)
04-10 13:51:21.301: E/AndroidRuntime(1161): at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
04-10 13:51:21.301: E/AndroidRuntime(1161): at android.widget.ListView.onMeasure(ListView.java:1175)
04-10 13:51:21.301: E/AndroidRuntime(1161): at android.view.View.measure(View.java:16497)
04-10 13:51:21.301: E/AndroidRuntime(1161): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
04-10 13:51:21.301: E/AndroidRuntime(1161): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
04-10 13:51:21.301: E/AndroidRuntime(1161): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052)
04-10 13:51:21.301: E/AndroidRuntime(1161): at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
04-10 13:51:21.301: E/AndroidRuntime(1161): at android.view.View.measure(View.java:16497)
04-10 13:51:21.301: E/AndroidRuntime(1161): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
04-10 13:51:21.301: E/AndroidRuntime(1161): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-10 13:51:21.301: E/AndroidRuntime(1161): at android.view.View.measure(View.java:16497)
04-10 13:51:21.301: E/AndroidRuntime(1161): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
04-10 13:51:21.301: E/AndroidRuntime(1161): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
04-10 13:51:21.301: E/AndroidRuntime(1161): at android.view.View.measure(View.java:16497)
04-10 13:51:21.301: E/AndroidRuntime(1161): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
04-10 13:51:21.301: E/AndroidRuntime(1161): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-10 13:51:21.301: E/AndroidRuntime(1161): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
04-10 13:51:21.301: E/AndroidRuntime(1161): at android.view.View.measure(View.java:16497)
04-10 13:51:21.301: E/AndroidRuntime(1161): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
04-10 13:51:21.301: E/AndroidRuntime(1161): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
04-10 13:51:21.301: E/AndroidRuntime(1161): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
04-10 13:51:21.301: E/AndroidRuntime(1161): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
04-10 13:51:21.301: E/AndroidRuntime(1161): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
04-10 13:51:21.301: E/AndroidRuntime(1161): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
04-10 13:51:21.301: E/AndroidRuntime(1161): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
04-10 13:51:21.301: E/AndroidRuntime(1161): at android.view.Choreographer.doFrame(Choreographer.java:544)
04-10 13:51:21.301: E/AndroidRuntime(1161): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
04-10 13:51:21.301: E/AndroidRuntime(1161): at android.os.Handler.handleCallback(Handler.java:733)
04-10 13:51:21.301: E/AndroidRuntime(1161): at android.os.Handler.dispatchMessage(Handler.java:95)
04-10 13:51:21.301: E/AndroidRuntime(1161): at android.os.Looper.loop(Looper.java:136)
04-10 13:51:21.301: E/AndroidRuntime(1161): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-10 13:51:21.301: E/AndroidRuntime(1161): at java.lang.reflect.Method.invokeNative(Native Method)
04-10 13:51:21.301: E/AndroidRuntime(1161): at java.lang.reflect.Method.invoke(Method.java:515)
04-10 13:51:21.301: E/AndroidRuntime(1161): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-10 13:51:21.301: E/AndroidRuntime(1161): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-10 13:51:21.301: E/AndroidRuntime(1161): at dalvik.system.NativeStart.main(Native Method)
04-10 13:51:21.351: W/ActivityManager(380): Force finishing activity com.solinpromex.vivegrancanaria/.Ofertas_MainActivity
在此处编辑,包括更多代码
import java.text.NumberFormat;
import java.util.List;
import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class Ofertas_ApplicationAdapter extends ArrayAdapter<Ofertas_Application>{
private List<Ofertas_Application> items;
public Ofertas_ApplicationAdapter(Context context, List<Ofertas_Application> items) {
super(context, R.layout.ofertas_app_custom_list, items);
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null) {
LayoutInflater li = LayoutInflater.from(getContext());
v = li.inflate(R.layout.ofertas_app_custom_list, null);
}
Ofertas_Application app = items.get(position);
if(app != null) {
ImageView icon = (ImageView)v.findViewById(R.id.appIcon);
TextView titleText = (TextView)v.findViewById(R.id.titleTxt);
LinearLayout ratingCntr = (LinearLayout)v.findViewById(R.id.ratingCntr);
TextView dlText = (TextView)v.findViewById(R.id.dlTxt);
if(icon != null) {
Resources res = getContext().getResources();
String sIcon = "com.sj.jsondemo:drawable/" + app.getIcon();
icon.setImageDrawable(res.getDrawable(res.getIdentifier(sIcon, null, null)));
}
if(titleText != null) titleText.setText(app.getTitle());
if(dlText != null) {
NumberFormat nf = NumberFormat.getNumberInstance();
dlText.setText(nf.format(app.getTotalDl())+" dl");
}
if(ratingCntr != null && ratingCntr.getChildCount() == 0) {
/*
* max rating: 5
*/
for(int i=1; i<=5; i++) {
ImageView iv = new ImageView(getContext());
if(i <= app.getRating()) {
iv.setImageDrawable(getContext().
getResources().getDrawable(R.drawable.start_checked));
}
else {
iv.setImageDrawable(getContext().
getResources().getDrawable(R.drawable.start_unchecked));
}
ratingCntr.addView(iv);
}
}
}
return v;
}
}
答案 0 :(得分:1)
您的错误发生在此行代码
上icon.setImageDrawable(res.getDrawable(res.getIdentifier(sIcon, null, null)));
因为Resource.getIdentifier()是documented,并出现以下错误响应:
如果未找到此类资源,则返回0。 (0不是有效的资源ID。)
尝试调用ID为0的getDrawable()确实是你的失败。
显然,由于
的结果,您没有任何名称由sIcon包含的资源String sIcon = "com.sj.jsondemo:drawable/" + app.getIcon();
在分配和尝试使用它之间简单地记录sIcon可能是值得的,看看这个名字是否合理 - 也许你正在不正确地构建它。另一种可能性是你的某些情况可能根本没有图标,你可以用它来代替默认或“?”的图片。什么的。