为什么在滑动抽屉上显示图像会给力关闭?

时间:2014-04-29 10:52:09

标签: android imageview android-drawable drawerlayout

我已生成Sliding Drawer,其中包含ImageViewTextView。我已经从SD卡生成了我的第一张图片。我想显示Drawable文件夹中的剩余部分。我已完成以下编码,但我收到了错误日志。请一步一步地告诉我我做错了什么。

Profile.class

    List<DrawerItem> dataList;
    dataList.add(new DrawerItem(c.get("username"),bmp));

    //dataList.add(new DrawerItem(c.get("username"),R.drawable.ic_action_good));
    dataList.add(new DrawerItem("contacts", R.drawable.ic_action_good));
    dataList.add(new DrawerItem("Tasks Sent by Me", R.drawable.ic_action_gamepad));
    dataList.add(new DrawerItem("My Tasks", R.drawable.ic_action_labels));
    dataList.add(new DrawerItem("Profile", R.drawable.ic_action_search));
    dataList.add(new DrawerItem("Settings", R.drawable.ic_action_cloud));

CustomDrawerAdapter.class

      @Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    DrawerItemHolder drawerHolder;
    View view = convertView;

    if (view == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        drawerHolder = new DrawerItemHolder();

        view = inflater.inflate(layoutResID, parent, false);
        drawerHolder.ItemName = (TextView) view
                .findViewById(R.id.drawer_itemName);
        drawerHolder.icon = (ImageView) view.findViewById(R.id.drawer_icon);
      drawerHolder.vieworange=(View)view.findViewById(R.id.vieworangelist);
      //drawerHolder.vieworange.setVisibility(view.VISIBLE);

        view.setTag(drawerHolder);

    } else {
        drawerHolder = (DrawerItemHolder) view.getTag();

    }

    DrawerItem dItem = (DrawerItem) this.drawerItemList.get(position);
    //Log.e("drawer position",""+dItem);

    drawerHolder.icon.setImageDrawable(view.getResources().getDrawable(
            dItem.getImgResID()));
    //drawerHolder.ItemName.
    drawerHolder.ItemName.setText(dItem.getItemName());
    drawerHolder.icon.setImageBitmap(dItem.bmp);


    return view;
}

错误日志

 04-29 10:38:43.546: E/Trace(4628): error opening trace file: No such file or directory (2)
 04-29 10:38:47.985: E/AndroidRuntime(4628): FATAL EXCEPTION: main
 04-29 10:38:47.985: E/AndroidRuntime(4628): android.content.res.Resources$NotFoundException: Resource ID #0x0
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at android.content.res.Resources.getValue(Resources.java:1014)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at android.content.res.Resources.getDrawable(Resources.java:659)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at com.example.taskmanager.CustomDrawerAdapter.getView(CustomDrawerAdapter.java:62)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at android.widget.AbsListView.obtainView(AbsListView.java:2159)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at android.widget.ListView.makeAndAddView(ListView.java:1831)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at android.widget.ListView.fillDown(ListView.java:674)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at android.widget.ListView.fillFromTop(ListView.java:735)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at android.widget.ListView.layoutChildren(ListView.java:1638)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at android.widget.AbsListView.onLayout(AbsListView.java:1994)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at android.view.View.layout(View.java:14008)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at android.view.ViewGroup.layout(ViewGroup.java:4373)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:714)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at android.view.View.layout(View.java:14008)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at android.view.ViewGroup.layout(ViewGroup.java:4373)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at android.view.View.layout(View.java:14008)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at android.view.ViewGroup.layout(ViewGroup.java:4373)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at  android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
  04-29 10:38:47.985: E/AndroidRuntime(4628):   at android.view.View.layout(View.java:14008)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at android.view.ViewGroup.layout(ViewGroup.java:4373)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at android.view.View.layout(View.java:14008)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at android.view.ViewGroup.layout(ViewGroup.java:4373)
  04-29 10:38:47.985: E/AndroidRuntime(4628):   at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
  04-29 10:38:47.985: E/AndroidRuntime(4628):   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
  04-29 10:38:47.985: E/AndroidRuntime(4628):   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at android.view.Choreographer.doFrame(Choreographer.java:532)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at android.os.Handler.handleCallback(Handler.java:725)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at android.os.Handler.dispatchMessage(Handler.java:92)
  04-29 10:38:47.985: E/AndroidRuntime(4628):   at android.os.Looper.loop(Looper.java:137)
   04-29 10:38:47.985: E/AndroidRuntime(4628):  at android.app.ActivityThread.main(ActivityThread.java:5041)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at java.lang.reflect.Method.invokeNative(Native Method)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at java.lang.reflect.Method.invoke(Method.java:511)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
 04-29 10:38:47.985: E/AndroidRuntime(4628):    at dalvik.system.NativeStart.main(Native Method)
 04-29 10:39:45.125: E/Trace(4681): error opening trace file: No such file or directory (2)

DrawerItem.class

   public class DrawerItem {

String ItemName;
int imgResID;
 Bitmap bmp;
 File f;

public DrawerItem(String itemName, int imgResID) {
    super();
    ItemName = itemName;
    this.imgResID = imgResID;
}

public DrawerItem(String itemName, Bitmap bmp) {
    // TODO Auto-generated constructor stub
    ItemName = itemName;
    File f = new File("/mnt/sdcard/test2.png");

    Bitmap bmps = BitmapFactory.decodeFile(f.getAbsolutePath());
    this.bmp = bmps;
}

/*public DrawerItem(String itemName2, File f) {
    // TODO Auto-generated constructor stub
    this.f=f;
}*/

public String getItemName() {
    return ItemName;
}
public void setItemName(String itemName) {
    ItemName = itemName;
}
public int getImgResID() {
    return imgResID;
}
public void setImgResID(int imgResID) {
    this.imgResID = imgResID;
}



 }

3 个答案:

答案 0 :(得分:1)

只是一个想法,为什么不尝试使用setImageResource而不是尝试自己获取drawable,使用框架方法!

drawerHolder.icon.setImageResource(dItem.getImgResID());

如果仍然无法工作,那么这很奇怪,总是值得清理项目以确保资源得到重建

答案 1 :(得分:1)

0资源ID已由其他答案解决:列表中的第一项没有资源ID。检查资源ID是!= 0还是仅使用setImageResource()

为什么你仍然没有看到任何东西是因为你在setImageDrawable()电话下面两行分配一个空图像位图:

drawerHolder.icon.setImageDrawable(view.getResources().getDrawable(
        dItem.getImgResID()));
//drawerHolder.ItemName.
drawerHolder.ItemName.setText(dItem.getItemName());
drawerHolder.icon.setImageBitmap(dItem.bmp);

删除drawerHolder.icon.setImageBitmap(dItem.bmp);。 (您可能希望仅对具有位图且没有资源ID的项目保留类似的内容。)

答案 2 :(得分:0)

我认为你的问题在于:

drawerHolder.icon.setImageDrawable(view.getResources().getDrawable(dItem.getImgResID()));

正如您的日志中所示,

  

dItem.getImgResID()

返回“0”,因此资源系统找不到您的Drawable资源,因此您的DrawerItem类没有返回正确的ID。

在其他情况下,问题可能是您从视图中获取资源:

view.getResources()

只需使用当前上下文更改该视图:

  

context.getResources()// context指向您当前的Context。

其他方式你可以让ImageView本身解析你的资源中的drawable:

  

drawerHolder.icon.setImageResource(dItem.getImgResID());