我正在使用一个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)
请朋友们帮帮我。