2012-07-09 32 views
0

我通過一個標準的方案創建一個網格:GridView設置與自定義BaseAdapter -derivate:安卓:網格滾動奇怪的崩潰

class EventGridAdapter extends BaseAdapter { 

    private GregorianCalendar gregorianCalendar; 
    Context context; 
    GridView gridView; 

    public EventGridAdapter(Context context, GridView gridView) { 
     this.context = context; 
     this.gridView = gridView; 
    } 


    public int getCount() { 
     return 31; 
    } 

    public Object getItem(int position) { 
     return null; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

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

     CalendarItem cell = null; 
     if (convertView == null) { 
      cell = new CalendarItem(context); 
      int viewHeight = gridView.getHeight(); 
      int spacing = 1; 
      int height = viewHeight/5 - spacing; 
      cell.setMinimumHeight(height); 
     } else { 
      cell = (CalendarItem) convertView; 
     } 
     return cell; 
    } 
} 

關於這一行:

cell = new CalendarItem(context) 

通過現在CalendarItem是純的View,例如一切都被註釋掉了,所以沒有引起死機有...

和標準GridView輸出:

public class CalendarActivity extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.calendar); 

    GridView g = (GridView) findViewById(R.id.gridCalendar); 
    g.setAdapter(new EventGridAdapter(this, g));   
} 
} 

佈局:

<?xml version="1.0" encoding="utf-8"?> 
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/gridCalendar" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:gravity="center" 
android:horizontalSpacing="1dp" 
android:verticalSpacing="1dp" 
android:numColumns="7" 
android:padding="10dp" 
android:stretchMode="columnWidth" 
/> 

什麼問題

當我在ICS 4.0.3的仿真器上運行這個代碼時,網格顯示得很完美,但是當我嘗試滾動它時 - 程序崩潰Ë例外:

D/AndroidRuntime(1087): Shutting down VM W/dalvikvm(1087): threadid=1: thread exiting with uncaught exception (group=0x409c01f8) E/AndroidRuntime(1087): FATAL EXCEPTION: main E/AndroidRuntime(1087): java.lang.IllegalArgumentException: bitmap size exceeds 32bits E/AndroidRuntime(1087): at android.graphics.Bitmap.nativeCreate(Native Method) E/AndroidRuntime(1087): at android.graphics.Bitmap.createBitmap(Bitmap.java:605) E/AndroidRuntime(1087): at android.graphics.Bitmap.createBitmap(Bitmap.java:585) E/AndroidRuntime(1087): at android.view.View.buildDrawingCache(View.java:10626) E/AndroidRuntime(1087): at android.view.View.getDrawingCache(View.java:10476) E/AndroidRuntime(1087): at android.view.ViewGroup.drawChild(ViewGroup.java:2743) E/AndroidRuntime(1087): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489) E/AndroidRuntime(1087): at android.widget.AbsListView.dispatchDraw(AbsListView.java:2092) E/AndroidRuntime(1087): at android.view.View.draw(View.java:11083) E/AndroidRuntime(1087): at android.widget.AbsListView.draw(AbsListView.java:3398) E/AndroidRuntime(1087): at android.view.ViewGroup.drawChild(ViewGroup.java:2887) E/AndroidRuntime(1087): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489) E/AndroidRuntime(1087): at android.view.ViewGroup.drawChild(ViewGroup.java:2885) E/AndroidRuntime(1087): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489) E/AndroidRuntime(1087): at android.view.ViewGroup.drawChild(ViewGroup.java:2885) E/AndroidRuntime(1087): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489) E/AndroidRuntime(1087): at android.view.View.draw(View.java:10981) E/AndroidRuntime(1087): at android.widget.FrameLayout.draw(FrameLayout.java:450) E/AndroidRuntime(1087): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2126) E/AndroidRuntime(1087): at android.view.ViewRootImpl.draw(ViewRootImpl.java:2026) E/AndroidRuntime(1087): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1634) E/AndroidRuntime(1087): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442) E/AndroidRuntime(1087): at android.os.Handler.dispatchMessage(Handler.java:99) E/AndroidRuntime(1087): at android.os.Looper.loop(Looper.java:137) E/AndroidRuntime(1087): at android.app.ActivityThread.main(ActivityThread.java:4424) E/AndroidRuntime(1087): at java.lang.reflect.Method.invokeNative(Native Method) E/AndroidRuntime(1087): at java.lang.reflect.Method.invoke(Method.java:511) E/AndroidRuntime(1087): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) E/AndroidRuntime(1087): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) E/AndroidRuntime(1087): at dalvik.system.NativeStart.main(Native Method)

當我在2.3.3運行仿真器相同的代碼 - 一切都很好!

經過一番研究,我發現了這個問題(的「源」 - 在getView方法EventGridAdapter再看一下:

 int viewHeight = gridView.getHeight(); 
     int spacing = 1; 
     int height = viewHeight/5 - spacing; 
     cell.setMinimumHeight(height); 

問題行是:

cell.setMinimumHeight(height); 

當我改變它到:

cell.setMinimumHeight(10); 

...崩潰消失

但我不知道我做錯了什麼?一切看起來在邏輯上是正確的:我需要5行。 此外,使height下不起作用(當所有的行都在視圖區,如沒有滾動需要):

int height = viewHeight/7 - spacing; 

你的意見?

回答

2

問題是聽到...

gridView.getHeight() 

爲什麼呢? 因爲你不會有在GridView @它剛剛創建

,你可以在任何onWindowfocuschanged或使用viewTreeobserver

像下面

ViewTreeObserver vto1 = gridView.getViewTreeObserver(); 
     vto1.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 

      public void onGlobalLayout() { 
       // write hear for height 

       Log.i(TAG, "from VTO" + gridView.getHeight()); 
       ViewTreeObserver obs = gridView.getViewTreeObserver(); 
       obs.removeGlobalOnLayoutListener(this); 
      } 
     }); 
+0

我不要了一個使用此方法時的高度創建網格時無法獲得高度! 'getView'稍後調用! 我試過你的方法。它返回相同的高度。當然,崩潰,仍然出現... – Exterminator13 2012-07-09 09:49:55