2

articleList.setAdapter(bindingData); 引发致命异常 (Resources$NotFoundException)。

我正在尝试从片段中填充 ListView。如果它位于 ListView 位于 Activity 上的单独项目中,则相同的代码可以工作。我所做的唯一更改是将以下构造函数添加到 BinderData 类:

    public BinderData(LayoutInflater i, List<HashMap<String, String>> map) {
    this.contentDataCollection = map;
    inflater = i;
}

片段:

public static class CalendarFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.calendar_fragment, container, false);

        final ListView articleList = (ListView)view.findViewById(R.id.content_list);
        final List<HashMap<String, String>> contentDataCollection = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map = null;

        map = new HashMap<String, String>();
        map.put("type", "Article");
        map.put("title", "Gesund kochen");
        map.put("text", "Lorem ipsum");
        map.put("image", "img_10061216.jpg");
        contentDataCollection.add(map);

        map = new HashMap<String, String>();
        map.put("type", "Rezept");
        map.put("title", "Pasta mit Radicchio");
        map.put("text", "300g Pasta");
        map.put("image", "img_10061216.jpg");
        contentDataCollection.add(map);

        BinderData bindingData = new BinderData(inflater, contentDataCollection);
        articleList.setAdapter(bindingData);

        return view;
    }
}

BinderData.java

package com.bayikoo;

import java.util.HashMap;
import java.util.List;

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

public class BinderData extends BaseAdapter {

// Content Attributes
static final String KEY_ID = "id";
static final String KEY_TYPE = "type";
static final String KEY_WEEK = "week";
static final String KEY_TITLE = "title";
static final String KEY_IMAGE = "image";
static final String KEY_TEXT = "text";

LayoutInflater inflater;
ImageView list_image;
List<HashMap<String, String>> contentDataCollection;
ViewHolder holder;

public BinderData() {
    // TODO Auto-generated constructor stub
}

public BinderData(Activity act, List<HashMap<String, String>> map) {
    this.contentDataCollection = map;
    inflater = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public BinderData(LayoutInflater i, List<HashMap<String, String>> map) {
    this.contentDataCollection = map;
    inflater = i;
}

public int getCount() {
    // TODO Auto-generated method stub
    // return idlist.size();
    return contentDataCollection.size();
}

public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

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

    View vi = convertView;
    if (convertView == null) {

        vi = inflater.inflate(R.layout.list_row, null);
        holder = new ViewHolder();

        holder.title = (TextView) vi.findViewById(R.id.title);
        //holder.type = (TextView) vi.findViewById(R.id.type);
        holder.list_image = (ImageView) vi.findViewById(R.id.list_image);

        vi.setTag(holder);
    } else {
        holder = (ViewHolder) vi.getTag();
    }
    holder.title.setText(contentDataCollection.get(position).get(KEY_TITLE));
    //holder.type.setText(contentDataCollection.get(position).get(KEY_TYPE));

    String uri = "drawable/" + contentDataCollection.get(position).get(KEY_IMAGE);
    // Setting an image
    int imageResource = vi.getContext().getApplicationContext().getResources().getIdentifier(uri,null,vi.getContext().getApplicationContext().getPackageName());
    Drawable image = vi.getContext().getResources().getDrawable(imageResource);
    holder.list_image.setImageDrawable(image);

    return vi;
}

/*
 * 
 * */
static class ViewHolder {
    TextView title;
    TextView type;
    ImageView list_image;
}

}

日志猫:

10-20 14:39:30.990: W/dalvikvm(3087): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
10-20 14:39:31.100: E/AndroidRuntime(3087): FATAL EXCEPTION: main
10-20 14:39:31.100: E/AndroidRuntime(3087): android.content.res.Resources$NotFoundException: Resource ID #0x0
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.content.res.Resources.getValue(Resources.java:1014)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.content.res.Resources.getDrawable(Resources.java:659)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at com.bayikoo.BinderData.getView(BinderData.java:83)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.widget.AbsListView.obtainView(AbsListView.java:2159)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.widget.ListView.onMeasure(ListView.java:1158)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.view.View.measure(View.java:15518)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.widget.RelativeLayout.measureChild(RelativeLayout.java:666)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.view.View.measure(View.java:15518)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.view.View.measure(View.java:15518)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1447)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.view.View.measure(View.java:15518)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.view.View.measure(View.java:15518)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.view.View.measure(View.java:15518)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.view.View.measure(View.java:15518)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.view.Choreographer.doFrame(Choreographer.java:532)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.os.Handler.handleCallback(Handler.java:725)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.os.Looper.loop(Looper.java:137)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at android.app.ActivityThread.main(ActivityThread.java:5041)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at java.lang.reflect.Method.invokeNative(Native Method)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at java.lang.reflect.Method.invoke(Method.java:511)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-20 14:39:31.100: E/AndroidRuntime(3087):     at dalvik.system.NativeStart.main(Native Method)
10-20 14:39:31.321: D/dalvikvm(3087): GC_CONCURRENT freed 95K, 8% free 3239K/3484K, paused 5ms+68ms, total 184ms
10-20 14:39:32.810: I/Process(3087): Sending signal. PID: 3087 SIG: 9

有人可以帮忙吗?

谢谢!

4

2 回答 2

0

getDrawablethrows Resources$NotFoundException,如果您提供的 id 不存在。

String uri = "drawable/" + contentDataCollection.get(position).get(KEY_IMAGE);
int imageResource = vi.getContext().getApplicationContext().getResources().getIdentifier(uri,null,vi.getContext().getApplicationContext().getPackageName());

在这里,您正在寻找可绘制对象的 id,但如果您要查找的可绘制对象不存在,getIdentifier 可以返回 0。试试这种方式:

String drawableName = "the_name_of_drwable"; resId = getContext().getResources().getIdentifier(drawableName, "drawable", getContext().getPackageName())

于 2013-10-20T14:57:49.037 回答
0

只需将vi = inflater.inflate(R.layout.list_row, null);您的 getView 方法的这一行更改为:vi = inflater.inflate(R.layout.list_row, parent, false);。这是因为您必须指定一个父级,并且不要让他们附加根它。

于 2013-10-20T15:06:07.863 回答