在Gridview项目上膨胀2个布局= CRASH :(

时间:2014-06-21 00:39:13

标签: android android-gridview android-adapter

我在照片中制作了一个ui: Sketch

  1. 因此,当我点击gridView项目时,它会将其内容更改为小菜单。

  2. 接近这个的正确方法是什么?我的解决方案是2个布局,点击后膨胀项目, 并使用:adapter.notifyDataSetChanged();

  3. 进行刷新
  4. 一直运行良好,直到你向下滚动(直到项目出现在视线之外)然后崩溃。

  5. 这是我的getView()代码:

    public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    RecordHolder holder = null;
    
    
    Item item = data.get(position);
    LayoutInflater inflater = ((Activity) context).getLayoutInflater();
    
    if (row == null) {
    
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new RecordHolder();
        holder.txtTitle = (TextView) row.findViewById(R.id.item_text);
        holder.imageItem = (ImageView) row.findViewById(R.id.item_image);   
        row.setTag(holder);
    }
    else {
        holder = (RecordHolder) row.getTag();
    }
    
    if(item.showMenu == true && item.menuShown == false)
    {
        item.menuShown = true;
        row = inflater.inflate(layoutResourceId_menu, parent, false);
        holder = new RecordHolder();
        holder.button1 = (Button) row.findViewById(R.id.button1);
        holder.button2 = (Button) row.findViewById(R.id.button2);
        row.setTag(holder);
    }
    
    if(item.showMenu == true && item.menuShown == true)
    {
        holder.button1.setText("asdsad");
        holder.button2.setText("asdasdd");
    }
    
    if(item.showMenu == false && item.menuShown == true)
    {
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new RecordHolder();
        holder.txtTitle = (TextView) row.findViewById(R.id.item_text);
        holder.imageItem = (ImageView) row.findViewById(R.id.item_image);
        row.setTag(holder);
        holder.txtTitle.setText(item.getTitle());
        holder.imageItem.setImageBitmap(item.getImage());
    }
    if(item.showMenu == false && item.menuShown == false)
    {
        holder.txtTitle.setText(item.getTitle());
        holder.imageItem.setImageBitmap(item.getImage());
    }
    return row;
    

    }

  6. 无论我尝试了什么,它都会一直崩溃。 我这样做对吗?

    更新 这是logcat:

    06-21 17:37:39.735    3308-3308/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.menu_grid_view_page, PID: 3308
    java.lang.NullPointerException
            at com.example.menu_grid_view_page.CustomGridViewAdapter.getView(CustomGridViewAdapter.java:91)
            at android.widget.AbsListView.obtainView(AbsListView.java:2240)
            at android.widget.GridView.makeAndAddView(GridView.java:1345)
            at android.widget.GridView.makeRow(GridView.java:345)
            at android.widget.GridView.fillDown(GridView.java:287)
            at android.widget.GridView.fillGap(GridView.java:247)
            at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5136)
            at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4247)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
            at android.view.Choreographer.doCallbacks(Choreographer.java:574)
            at android.view.Choreographer.doFrame(Choreographer.java:543)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5081)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)
    

1 个答案:

答案 0 :(得分:0)

您正在充气两个视图并仅使用一个视图持有者。可能存在这样的情况:当row不为null但holder.txtTitle为null。 代码内容:

if (row == null) {
..
} else {
..
}

尝试使用:

boolean inflated = false;
if (row != null) {
    holder = (RecordHolder) row.getTag();
    if (holder.imageItem != null) {
        inflated = true;
    }
}

if(!inflated) {
    row = inflater.inflate(layoutResourceId, parent, false);
    holder = new RecordHolder();
    holder.txtTitle = (TextView) row.findViewById(R.id.item_text);
    holder.imageItem = (ImageView) row.findViewById(R.id.item_image);   
    row.setTag(holder);
}