导航抽屉,ResourceNotFoundException

时间:2013-08-02 18:47:50

标签: android navigation-drawer

我会自定义导航抽屉。我使用了一个我在另一个线程的stackoverflow上找到的例子。 代码是复制和粘贴,但在运行时,logcat中的应用程序获得异常:“ResourceNotFoundException”。 下面粘贴一块logcat

08-02 20:06:26.367: E/AndroidRuntime(6084): android.content.res.Resources$NotFoundException: String resource ID #0x0
08-02 20:06:26.367: E/AndroidRuntime(6084):     at android.content.res.Resources.getText(Resources.java:239)
08-02 20:06:26.367: E/AndroidRuntime(6084):     at android.widget.TextView.setText(TextView.java:3837)
08-02 20:06:26.367: E/AndroidRuntime(6084):     at it.ustation.DrawerAdapter.getView(DrawerAdapter.java:81)
08-02 20:06:26.367: E/AndroidRuntime(6084):     at android.widget.AbsListView.obtainView(AbsListView.java:2177)
....

我的适配器的代码是

public class DrawerAdapter extends ArrayAdapter<ListNavigationItemModel>{

public DrawerAdapter(Context context) {
    super(context, 0);
}

public void addHeader(int title) {
    add(new ListNavigationItemModel(title, -1, true));
}

public void addItem(int title, int icon) {
    add(new ListNavigationItemModel(title, icon, false));
}

public void addItem(ListNavigationItemModel itemModel) {
    add(itemModel);
}

@Override
public int getViewTypeCount() {
    return 2; 
}

@Override
public int getItemViewType(int position) {
    return getItem(position).isHeader ? 0 : 1;
}

@Override
public boolean isEnabled(int position) {
    return !getItem(position).isHeader;
}

public static class ViewHolder {
    public final TextView textHolder;
    public final ImageView imageHolder;

    public ViewHolder(TextView text1, ImageView image1) {
        this.textHolder = text1;
        this.imageHolder = image1;
    }
}

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

    ListNavigationItemModel item = getItem(position);
    ViewHolder holder = null;
    View view = convertView;

    if (view == null) {
        int layout = R.layout.list_item_drawer;
        if (item.isHeader)
            layout = R.layout.ns_menu_row_header;

        view = LayoutInflater.from(getContext()).inflate(layout, null);

        TextView text1 = (TextView) view.findViewById(R.id.nd_title);
        ImageView image1 = (ImageView) view.findViewById(R.id.nd_thumb_image);
        view.setTag(new ViewHolder(text1, image1));
    }

    if (holder == null && view != null) {
        Object tag = view.getTag();
        if (tag instanceof ViewHolder) {
            holder = (ViewHolder) tag;
        }
    }

    if (item != null && holder != null) {
        if (holder.textHolder != null)
            holder.textHolder.setText(item.title);

        if (holder.imageHolder != null) {
            if (item.iconRes > 0) {
                holder.imageHolder.setVisibility(View.VISIBLE);
                holder.imageHolder.setImageResource(item.iconRes);
            } else {
                holder.imageHolder.setVisibility(View.GONE);
            }
        }
    }

    return view;
}
}

,模型是

public class ListNavigationItemModel {

public int title;
public int iconRes;
public boolean isHeader;

public ListNavigationItemModel(int title, int iconRes, boolean header) {
    this.title = title;
    this.iconRes = iconRes;
    this.isHeader = header;
}

public ListNavigationItemModel(int title, int iconRes) {
    this(title, iconRes, false);
}
 }

和xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp" >

<ImageView
    android:id="@+id/nd_thumb_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="10dp"
    android:contentDescription="@string/thumbnail_desc"
    android:scaleType="fitCenter"
    android:src="@drawable/ic_save" />

<TextView
    android:id="@+id/nd_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/nd_thumb_image"
    android:text="@string/c_desc"
    android:textColor="#fff"
    android:textSize="16sp" />

这是在适配器上添加每个项目的代码。 mPlanetTitles是一个数组字符串。

for (String item : mPlanetTitles) {

        int id_menu_one = getResources().getIdentifier(item, "string", this.getPackageName());
        int id_menu_one_icons = getResources().getIdentifier(menuOneIcons[oneIcons], "drawable", this.getPackageName());
        ListNavigationItemModel mItem = new ListNavigationItemModel(id_menu_one, id_menu_one_icons);
        mAdapter.addItem(mItem);
        oneIcons++;
    }

2 个答案:

答案 0 :(得分:1)

根据您的评论item.title是一个整数

所以使用

    holder.textHolder.setText(String.valueOf(item.title));

出了什么问题?

    public final void setText (int resid)

setText查找资源ID,它是整数。哪个不存在。因此,您得到ResourceNotFoundException

相反,您应该将seText与CharSequence一起用作param

public final void setText (CharSequence text)

在API级别1中添加

设置TextView的字符串值。 TextView不接受类似HTML的格式,您可以使用XML资源文件中的文本字符串。要设置字符串样式,请将android.text.style。*对象附加到SpannableString,或者参阅可用资源类型文档以获取在XML资源文件中设置格式化文本的示例。

public static String valueOf(int i)

返回int参数的字符串表示形式。 表示正好是一个参数的Integer.toString方法返回的表示。

答案 1 :(得分:0)

看起来缺少一些字符串资源。确保@string/thumbnail_desc文件中存在@string/c_descstrings.xml