我使用一个listview
来显示地点的详细信息。为此,我创建了一个CustomAdapter
,但我不知道为什么它给了我Null Pointer Exception
,请帮我解决这个问题。
这是我的xml文件。
activity_main.xml中
<LinearLayout 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"
tools:context=".MainActivity" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</LinearLayout>
list_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" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/img_hotel"
android:layout_width="100dp"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/txt_hotelName"
android:layout_width="fill_parent"
android:layout_height="20dp" />
<TextView
android:id="@+id/txt_hotelAddress"
android:layout_width="fill_parent"
android:layout_height="20dp" />
<RatingBar
android:id="@+id/ratingBar"
style="?android:attr/ratingBarStyleIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:rating="2.0"
android:stepSize="0.5" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
这是我更新的自定义适配器
public class CustomListViewAdapter extends BaseAdapter {
Context context;
public CustomListViewAdapter(Context context, int resource,
List<Result> results) {
// super(context, resource, results);
this.context = context;
Log.v("Result Data in Adapter", results.toString());
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = null;
Result result = (Result) getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
// LayoutInflater mInflater = (LayoutInflater)
// context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.imgView = (ImageView) convertView
.findViewById(R.id.img_hotel);
holder.txtName = (TextView) convertView
.findViewById(R.id.txt_hotelName);
holder.txtAddress = (TextView) convertView
.findViewById(R.id.txt_hotelAddress);
holder.rate = (RatingBar) convertView
.findViewById(R.id.ratingBar);
convertView.setTag(holder);
} else {
/*
* holder = (ViewHolder) convertView.getTag();
*
* String url = result.icon; Log.e("URL is ", url); if (url !=
* null) { Bitmap bitmap = fetchBitmapFromCache(url); if (bitmap
* == null) { new
* BitmapDownloaderTask(holder.imgView).execute(url); } else {
* holder.imgView.setImageBitmap(bitmap); } } else {
* holder.imgView.setImageBitmap(null); }
*/
holder.imgView.setImageDrawable(getResources().getDrawable(
R.drawable.ic_launcher));
holder.txtName.setText(result.name);
holder.txtAddress.setText(result.formatted_address);
holder.rate.setRating(result.rating);
}
return convertView; // super.getView(position, convertView, parent);
}
private class ViewHolder {
ImageView imgView;
TextView txtName;
TextView txtAddress;
RatingBar rate;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return results.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return results.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return results.indexOf(getItem(position));
}
}
这里我已将适配器设置为listView
list.setAdapter(new CustomListViewAdapter(MainActivity.this,
R.layout.list_item, results));
这是我最新的代码,但它仍然给我空指针异常。
这是我的堆栈。
03-13 11:13:20.355: E/AndroidRuntime(413): FATAL EXCEPTION: main
03-13 11:13:20.355: E/AndroidRuntime(413): java.lang.NullPointerException
03-13 11:13:20.355: E/AndroidRuntime(413): at com.th.hotelreservationsystem.MainActivity$CustomListViewAdapter.getView(MainActivity.java:183)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.widget.AbsListView.obtainView(AbsListView.java:1294)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.widget.ListView.onMeasure(ListView.java:1109)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.view.View.measure(View.java:8171)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:696)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.widget.LinearLayout.onMeasure(LinearLayout.java:306)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.view.View.measure(View.java:8171)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.view.View.measure(View.java:8171)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.view.View.measure(View.java:8171)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.view.View.measure(View.java:8171)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.os.Handler.dispatchMessage(Handler.java:99)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.os.Looper.loop(Looper.java:123)
03-13 11:13:20.355: E/AndroidRuntime(413): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-13 11:13:20.355: E/AndroidRuntime(413): at java.lang.reflect.Method.invokeNative(Native Method)
03-13 11:13:20.355: E/AndroidRuntime(413): at java.lang.reflect.Method.invoke(Method.java:521)
03-13 11:13:20.355: E/AndroidRuntime(413): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-13 11:13:20.355: E/AndroidRuntime(413): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-13 11:13:20.355: E/AndroidRuntime(413): at dalvik.system.NativeStart.main(Native Method)
请帮帮我朋友。
答案 0 :(得分:3)
您在getView的开头将viewHolder设置为null。 如果convertView为null,则初始化它。但是如果convertView不为null,则直接使用它。如果convertView不为null,请不要忘记viewHolder为null。
Add following code in the starting of else block:
holder = (ViewHolder) convertView.getTag();
答案 1 :(得分:2)
谢谢你的帮助。
我得到了解决方案,这是我带有注释的更新代码。
3我的代码错误。 对那些帮助我的人有所帮助
public class CustomListViewAdapter extends BaseAdapter {
Context context;
public CustomListViewAdapter(Context context, int resource,
List<Result> results) {
super(); // 1) here i was trying to call parameterised Constructor which
// was wrong.
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = null;
Result result = (Result) getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder(); // 2) i had not instantiated View holder
// here i did.
holder.imgView = (ImageView) convertView
.findViewById(R.id.img_hotel);
holder.txtName = (TextView) convertView
.findViewById(R.id.txt_hotelName);
holder.txtAddress = (TextView) convertView
.findViewById(R.id.txt_hotelAddress);
holder.rate = (RatingBar) convertView
.findViewById(R.id.ratingBar);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();// 3) here by mistake i
//had commited holde with other unwanted code. here removed it.
holder.imgView.setImageDrawable(getResources().getDrawable(
R.drawable.ic_launcher));
holder.txtName.setText(result.name);
holder.txtAddress.setText(result.formatted_address);
holder.rate.setRating(result.rating);
}
return convertView; // super.getView(position, convertView, parent);
}
private class ViewHolder {
ImageView imgView;
TextView txtName;
TextView txtAddress;
RatingBar rate;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return results.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return results.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return results.indexOf(getItem(position));
}
}
答案 2 :(得分:1)
您的context
为空,因为您尚未在构造函数中初始化它,
尝试像这样初始化它
public CustomListViewAdapter(Context context, int resource,
List<Result> results) {
super(context, resource, results);
this.context=context;
}
此外,您的观看者为空
尝试初始化您的观看者
holder = new viewHolder();
答案 3 :(得分:1)
您应该在getView方法中返回convertView而不是调用:
return super.getView(position, convertView, parent); // replace this line by return convertView;
编辑: 所以完整的答案是这样的:
public class CustomListViewAdapter extends ArrayAdapter<Result>{
Context context;
public CustomListViewAdapter(Context context, int resource,
List<Result> results) {
super(context, resource, results);
this.context = context; // 1
Log.v("Result Data in Adapter", results.toString());
// TODO Auto-generated constructor stub
}
private class ViewHolder {
ImageView imgView;
TextView txtName;
TextView txtAddress;
RatingBar rate;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = null;
Result result = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null ){
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder(); //2
// holder.imgView = (ImageView)convertView.findViewById(R.id.img_hotel);
holder.txtName = (TextView) convertView.findViewById(R.id.txt_hotelName);
holder.txtAddress = (TextView) convertView.findViewById(R.id.txt_hotelAddress);
holder.rate = (RatingBar)convertView.findViewById(R.id.ratingBar);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
//holder.imgView.setImageResource();
holder.txtName.setText(result.name);
holder.txtAddress.setText(result.formatted_address);
holder.rate.setRating(result.rating);
}
return convertView; //3
}
}
答案 4 :(得分:1)
@Mahaveer 你可以做到吗
@Override
public int getCount() {
return <Size of list items>;
}
希望这可以解决您的问题。
答案 5 :(得分:0)
您将ViewHolder
设置为null
,但从未对其进行初始化。
所以让你的getView()
像这样:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = null;
Result result = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null ){
holder = new viewHolder(); //initialize it
convertView = mInflater.inflate(R.layout.list_item, null);
....
convertView.setTag(holder);
}...