我正在尝试创建一个具有Imageview的网格视图。我试图扩展BaseAdapter但我遇到了问题,如果有人能指出我正在做的错误,我会很高兴。
logcat的:
07-10 19:29:00.881: D/AndroidRuntime(1678): Shutting down VM
07-10 19:29:00.881: W/dalvikvm(1678): threadid=1: thread exiting with uncaught exception (group=0xb3a47b90)
07-10 19:29:00.901: E/AndroidRuntime(1678): FATAL EXCEPTION: main
07-10 19:29:00.901: E/AndroidRuntime(1678): Process: com.shrek.try2, PID: 1678
07-10 19:29:00.901: E/AndroidRuntime(1678): java.lang.NullPointerException
07-10 19:29:00.901: E/AndroidRuntime(1678): at com.shaastra.try2.MainActivity$lAdapter.getView(MainActivity.java:80)
07-10 19:29:00.901: E/AndroidRuntime(1678): at android.widget.AbsListView.obtainView(AbsListView.java:2263)
07-10 19:29:00.901: E/AndroidRuntime(1678): at android.widget.GridView.onMeasure(GridView.java:1044)
07-10 19:29:00.901: E/AndroidRuntime(1678): at android.view.View.measure(View.java:16458)
07-10 19:29:00.901: E/AndroidRuntime(1678): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
07-10 19:29:00.901: E/AndroidRuntime(1678): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
07-10 19:29:00.901: E/AndroidRuntime(1678): at android.view.View.measure(View.java:16458)
07-10 19:29:00.901: E/AndroidRuntime(1678): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-10 19:29:00.901: E/AndroidRuntime(1678): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-10 19:29:00.901: E/AndroidRuntime(1678): at android.view.View.measure(View.java:16458)
07-10 19:29:00.901: E/AndroidRuntime(1678): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-10 19:29:00.901: E/AndroidRuntime(1678): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
07-10 19:29:00.901: E/AndroidRuntime(1678): at android.view.View.measure(View.java:16458)
07-10 19:29:00.901: E/AndroidRuntime(1678): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-10 19:29:00.901: E/AndroidRuntime(1678): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-10 19:29:00.901: E/AndroidRuntime(1678): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2289)
07-10 19:29:00.901: E/AndroidRuntime(1678): at android.view.View.measure(View.java:16458)
07-10 19:29:00.901: E/AndroidRuntime(1678): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1914)
07-10 19:29:00.901: E/AndroidRuntime(1678): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1111)
07-10 19:29:00.901: E/AndroidRuntime(1678): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1293)
07-10 19:29:00.901: E/AndroidRuntime(1678): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
07-10 19:29:00.901: E/AndroidRuntime(1678): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5582)
07-10 19:29:00.901: E/AndroidRuntime(1678): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
07-10 19:29:00.901: E/AndroidRuntime(1678): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
07-10 19:29:00.901: E/AndroidRuntime(1678): at android.view.Choreographer.doFrame(Choreographer.java:532)
07-10 19:29:00.901: E/AndroidRuntime(1678): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
07-10 19:29:00.901: E/AndroidRuntime(1678): at android.os.Handler.handleCallback(Handler.java:733)
07-10 19:29:00.901: E/AndroidRuntime(1678): at android.os.Handler.dispatchMessage(Handler.java:95)
07-10 19:29:00.901: E/AndroidRuntime(1678): at android.os.Looper.loop(Looper.java:137)
07-10 19:29:00.901: E/AndroidRuntime(1678): at android.app.ActivityThread.main(ActivityThread.java:4998)
07-10 19:29:00.901: E/AndroidRuntime(1678): at java.lang.reflect.Method.invokeNative(Native Method)
07-10 19:29:00.901: E/AndroidRuntime(1678): at java.lang.reflect.Method.invoke(Method.java:515)
07-10 19:29:00.901: E/AndroidRuntime(1678): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
07-10 19:29:00.901: E/AndroidRuntime(1678): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
07-10 19:29:00.901: E/AndroidRuntime(1678): at dalvik.system.NativeStart.main(Native Method)
它在第80行显示nullpointexception (mainactivity和customadapter在mainAcitivity.java中定义):
mainActivity.java:
package com.shrek.try2;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class MainActivity extends Activity{// implements OnItemClickListener {
GridView myGrid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myGrid = (GridView) findViewById(R.id.gridView);
myGrid.setAdapter(new lAdapter(this));
}
class lAdapter extends BaseAdapter
{
private final int[] lectimages={R.drawable.image_1,R.drawable.image_2,R.drawable.image_3,R.drawable.image_4,R.drawable.image_5,R.drawable.image_6,R.drawable.image_7,R.drawable.image_8,R.drawable.image_9};
Context c;
lAdapter(Context c)
{
this.c = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return lectimages.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int i, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View item;
LayoutInflater inflater=(LayoutInflater) c.getSystemService(c.LAYOUT_INFLATER_SERVICE);
item = inflater.inflate(R.layout.single_item, parent, false);
ImageView lect = (ImageView) findViewById(R.id.imageView_1);
lect.setImageResource(lectimages[i]);
return item;
}
}
}
这意味着getView方法中的行 lect 为null,因此findviewbybyid返回null但我已经采用了正确的id,图像已被添加到drawable并且精确布局(single_item.xml)已经膨胀:
single_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageView_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:contentDescription="imageViewDescription"/>
</RelativeLayout>
和activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<GridView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/gridView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:numColumns="auto_fit"
android:horizontalSpacing="0dp"
android:verticalSpacing="0dp"
android:columnWidth="120dp"
android:stretchMode="spacingWidthUniform"
/>
</RelativeLayout>
请帮助我,我已经坚持了 10小时这个问题。
答案 0 :(得分:2)
lect是null
,因为您使用的是findViewById(R.id.imageView_1)
,这会在imageView_1
Activity
中搜索Layout
,如果是null
则返回ImageView
未找到。
当您的ItemView
位于View.findViewById()
时,您必须使用此item.findViewById(R.id.imageView_1);
在itemView中找到它。
{{1}}