2014-06-21 46 views
0

林製作UI喜歡的圖片: Sketch充氣2個佈局上的GridView項= CRASH :(

  1. 所以,當我點擊它,改變它的內容放到一個小菜單一個gridview項

  2. 什麼是處理這個正確的方式我的解決辦法2點佈局,膨脹的項目上點擊, 與刷新:adapter.notifyDataSetChanged();

  3. 它運作良好unti你向下滾動(直到物品出現在視線之外)然後崩潰。

  4. 這是我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; 
    

    }

不管我嘗試它總是崩潰。 我在做這個對嗎?

UPDATE 這裏是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) 
+0

您可以發佈與崩潰的logcat的輸出? – michaelcarrano

+0

我知道了! ILL嘗試將它添加到我的信息 – user2551102

回答

0

您正在膨脹的兩個視圖,並且僅使用一個視圖持有者。有可能是情況,當行不爲空,但holder.txtTitle爲空。代碼 Instade:

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); 
} 
+0

立即嘗試:)生病即將更新 – user2551102

+0

Kornel,它不起作用。然而 - 新的事情正在發生 - 1.我點擊一個項目。 2.按鈕正在顯示。 3)我從可見區域滾出。 4)當我回到可見區域時,它會崩潰。 5)另外,當你點擊第一個項目IMMEDIATLEY CRASH。可能會出現問題嗎? – user2551102