1

我正在使用一个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)

请朋友们帮帮我。

4

6 回答 6

3

您在 getView 开始时将 viewHolder 设置为 null。如果 convertView 为空,则初始化它。但是如果convertView不为null,你就直接使用了。如果 convertView 不为空,请不要忘记 viewHolder 为空。

在 else 块的开头添加以下代码:

holder = (ViewHolder) convertView.getTag();

于 2013-03-13T06:39:02.767 回答
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));
    }
}
于 2013-03-13T06:53:39.273 回答
1

context是 null 因为你没有在构造函数中初始化它,

尝试像这样初始化它

 public CustomListViewAdapter(Context context, int resource,
                List<Result> results) {
            super(context, resource, results);
           this.context=context;

        }

而且你的观众是空的

尝试初始化您的视图持有者

 holder = new viewHolder();
于 2013-03-12T12:58:28.643 回答
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
        }
    }
于 2013-03-12T13:03:51.157 回答
1

@Mahaveer 你能做到吗

@Override
public int getCount() {
    return <Size of list items>;
}

希望这能解决您的问题。

于 2013-03-12T17:49:17.560 回答
0

您正在将您的设置ViewHoldernull,但从不初始化它。

所以让你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);
        }...
于 2013-03-12T12:59:25.513 回答