问题描述
- ListView 的问题,按照书上例子,仿写了一个,结果出问题了,请大家看看到底哪里出错了啊
-
先是有两个布局文件,一个是关于ListView控件的,另一个是我自己定的线性布局,实现的是一个字符旁边先是一个图样。
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="com.IWIN.listview.MainActivity" ><ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"<ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"
android:layout_marginLeft="10dp"/>
然后定义了一个了类,定义字符和图样
package com.IWIN.listview;public class Fruit {
private String name;
private int imageId;
public Fruit(String name,int imageId){
this.name=name;
this.imageId=imageId;
}
public int getImageId(){
return imageId;
}
public String getName(){
return name;
}}
紧接着又定义了一个适配器
package com.IWIN.listview;import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;import com.IWIN.listview.Fruit;
import com.IWIN.listview.R;public class FruitAdapter extends ArrayAdapter{
private ImageView image; private TextView textView; int resourceId; public FruitAdapter(Context context, int textViewResourceId, List<Fruit> objects) { super(context, textViewResourceId, objects); resourceId=textViewResourceId; } public View getView(int position,View convertView,ViewGroup parent){ Fruit fruit=getItem(position);//找到在屏幕内的fruit类 View view=LayoutInflater.from(getContext()).inflate(resourceId,null);//cetContentView是Activity里面的类 image=(ImageView)view.findViewById(R.id.image); //LayoutInflater.from().inflate(常量ID,ViewGroup)可以在非Activity里面使用。 textView=(TextView)view.findViewById(R.id.textView); image.setImageResource(fruit.getImageId()); textView.setText(fruit.getName()); return view; }
}
再然后就是ManiActivity
private ListView listView;protected void onCreate(Bundle savedInstanceState) { nitFruit(); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FruitAdapter adapter=new FruitAdapter(MainActivity.this,android.R.layout.simple_list_item_1,list); listView=(ListView)findViewById(R.id.listView); listView.setAdapter(adapter); } public void nitFruit(){ Fruit read=new Fruit("read",R.drawable.ic_launcher); list.add(read); Fruit write=new Fruit("write",R.drawable.ic_launcher); list.add(write); Fruit white=new Fruit("white",R.drawable.ic_launcher); list.add(white); Fruit blue=new Fruit("blue",R.drawable.ic_launcher); list.add(blue); Fruit sea=new Fruit("sea",R.drawable.ic_launcher); list.add(sea); Fruit like=new Fruit("like",R.drawable.ic_launcher); list.add(like); Fruit love=new Fruit("love",R.drawable.ic_launcher); list.add(love); Fruit student=new Fruit("student",R.drawable.ic_launcher); list.add(student); Fruit teacher=new Fruit("teacher",R.drawable.ic_launcher); list.add(teacher); Fruit classmates=new Fruit("classmates",R.drawable.ic_launcher); list.add(classmates); Fruit classroom=new Fruit("classroom",R.drawable.ic_launcher); list.add(classroom); Fruit hello=new Fruit("hello",R.drawable.ic_launcher); list.add(hello); }
}
编译后LogCat显示的错误,我也没看出问题
09-11 02:45:16.776: W/dalvikvm(844): threadid=1: thread exiting with uncaught exception (group=0x409bf1f8)
09-11 02:45:16.796: E/AndroidRuntime(844): FATAL EXCEPTION: main
09-11 02:45:16.796: E/AndroidRuntime(844): java.lang.NullPointerException
09-11 02:45:16.796: E/AndroidRuntime(844): at com.IWIN.listview.FruitAdapter.getView(FruitAdapter.java:34)
09-11 02:45:16.796: E/AndroidRuntime(844): at android.widget.AbsListView.obtainView(AbsListView.java:2033)
09-11 02:45:16.796: E/AndroidRuntime(844): at android.widget.ListView.makeAndAddView(ListView.java:1772)
09-11 02:45:16.796: E/AndroidRuntime(844): at android.widget.ListView.fillDown(ListView.java:672)
09-11 02:45:16.796: E/AndroidRuntime(844): at android.widget.ListView.fillFromTop(ListView.java:732)
09-11 02:45:16.796: E/AndroidRuntime(844): at android.widget.ListView.layoutChildren(ListView.java:1625)
09-11 02:45:16.796: E/AndroidRuntime(844): at android.widget.AbsListView.onLayout(AbsListView.java:1863)
09-11 02:45:16.796: E/AndroidRuntime(844): at android.view.View.layout(View.java:11278)
09-11 02:45:16.796: E/AndroidRuntime(844): at android.view.ViewGroup.layout(ViewGroup.java:4224)
09-11 02:45:16.796: E/AndroidRuntime(844): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
。。。。。。
解决方案
你传入的resourecid不对,要传你自己写的包含一个textview和imageview的那个布局的id。你传的是系统的android.R.layout.simple_list_item_1,这玩意儿就一个textview,而且id应该是text。没有imageview。所以是在image.setImageResource(fruit.getImageId());这里空指针。因为image没找到
解决方案二:
空指针报错,View view=LayoutInflater.from(getContext()).inflate(resourceId,null)终得resourecid没有指定吧
解决方案三:
com.IWIN.listview.FruitAdapter.getView(FruitAdapter.java:34)报错了。
FruitAdapter adapter=new FruitAdapter(MainActivity.this,android.R.layout.simple_list_item_1,list);//在MainActivity里面的第二个参数不对,你要传递你自己的layout.
解决方案四:
convertView没赋值?