我正在通过标准方案创建网格:GridView
使用自定义BaseAdapter
衍生设置:
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;
你的建议?