我正在制作一个带有网格视图的应用程序,以显示存储在我的项目可绘制文件夹中的图像缩略图。
但是当我在模拟器上运行应用程序时,它不显示任何缩略图。
屏幕出现 1 秒(空白),然后关闭并显示“不幸的是主页已停止”。该怎么办?”
主页.java
public class Home extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
GridView gridview = (GridView) findViewById(R.id.gridView1);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(Home.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
}
ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView1(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.images, R.drawable.home,
R.drawable.ic_launcher,R.drawable.sync,
R.drawable.images, R.drawable.home,
R.drawable.ic_launcher,R.drawable.sync
};
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
return null;
}
}
详细的logcat:
08-13 01:45:12.189: D/dalvikvm(818): GC_FOR_ALLOC 释放 39K, 6% 释放 2702K/2860K, 暂停 142ms, 总共 326ms 08-13 01:45:12.290: I/dalvikvm-heap(818):将堆(碎片情况)增加到 3.358MB,分配 635812 字节 08-13 01:45:12.699:D/dalvikvm(818):GC_FOR_ALLOC 释放 3K,5% 空闲 3319K/3484K,暂停 410ms,总计 410ms 08-13 01 :45:13.591:D / AndroidRuntime(818):关闭VM 08-13 01:45:13.699:W / dalvikvm(818):threadid = 1:线程退出未捕获的异常(组= 0x414c4700)08-13 01: 45:13.959:E/AndroidRuntime(818):致命异常:主要 08-13 01:45:13.959:E/AndroidRuntime(818):java.lang.NullPointerException 08-13 01:45:13.959:E/AndroidRuntime(818 ): 在 android.widget.AbsListView.obtainView(AbsListView.java:2179) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 android.widget.GridView.onMeasure(GridView.java:1040) 08- 13 01:45:13.959:E/AndroidRuntime(818): 在 android.view.View.measure(View.java:15848) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java :5008) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) 08-13 01:45:13.959: E/AndroidRuntime(818):在 android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 android.widget.LinearLayout.onMeasure(LinearLayout.java:590) 08-13 01 :45:13.959: E/AndroidRuntime(818): 在 android.view.View.measure(View.java:15848) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 android.view.ViewGroup。 measureChildWithMargins(ViewGroup.java:5008) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 android.view.View.measure(View.java:15848) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout .java:728) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477) 08-13 01:45:13.959: E/AndroidRuntime(818 ): 在 android.view.View.measure(View.java:15848) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008) 08- 13 01:45:13.959: E/AndroidRuntime(818): 在 android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 android.view。 View.measure(View.java:15848) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728) 08-13 01:45:13.959:E/AndroidRuntime(818): 在 android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 android.view.View.measure(View.java :15848) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008) 08-13 01:45:13.959: E/AndroidRuntime(818):在 android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 android.view.View.measure(View.java:15848) 08-13 01 :45:13.959: E/AndroidRuntime(818): 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 com.android.internal。 widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 android.view.View.measure(View.java:15848) 08-13 01:45: 13.959:E/AndroidRuntime(818): 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 android.widget.FrameLayout.onMeasure(FrameLayout.java :310) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189) 08-13 01:45:13.959 : E/AndroidRuntime(818): 在 android.view.View.measure(View.java:15848) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 android.view.ViewRootImpl.performMeasure(ViewRootImpl. java:1905) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104) 08-13 01:45:13.959: E/AndroidRuntime(818) : 在 android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 android.view.ViewRootImpl。doTraversal(ViewRootImpl.java:1004) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 android.view.Choreographer.doCallbacks(Choreographer .java:562) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 android.view.Choreographer.doFrame(Choreographer.java:532) 08-13 01:45:13.959: E/AndroidRuntime(818 ): 在 android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 android.os.Handler.handleCallback(Handler.java:730) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 android.os.Handler.dispatchMessage(Handler.java:92) 08-13 01:45:13.959: E/AndroidRuntime(818):在 android.os.Looper.loop(Looper.java:137) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 android.app.ActivityThread.main(ActivityThread.java:5103) 08-13 01 :45:13.959: E/AndroidRuntime(818): at java.lang.reflect.Method.invokeNative(Native Method) 08-13 01:45:13.959: E/AndroidRuntime(818): at java.lang.reflect.Method .invoke(Method.java:525) 08-13 01:45:13.959: E/AndroidRuntime(818): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 08-13 01 :45:13.959: E/AndroidRuntime(818): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 dalvik。 system.NativeStart.main(本机方法)E/AndroidRuntime(818): 在 java.lang.reflect.Method.invokeNative(Native Method) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 java.lang.reflect.Method.invoke(Method. java:525) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 dalvik.system.NativeStart.main (本机方法)E/AndroidRuntime(818): 在 java.lang.reflect.Method.invokeNative(Native Method) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 java.lang.reflect.Method.invoke(Method. java:525) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 08-13 01:45:13.959: E/AndroidRuntime(818): 在 dalvik.system.NativeStart.main (本机方法)ZygoteInit.main(ZygoteInit.java:553) 08-13 01:45:13.959: E/AndroidRuntime(818): at dalvik.system.NativeStart.main(Native Method)ZygoteInit.main(ZygoteInit.java:553) 08-13 01:45:13.959: E/AndroidRuntime(818): at dalvik.system.NativeStart.main(Native Method)