6

我正在为具有自定义列表的 ListView 编写自定义适配器。

显然,我必须编写getView函数。所以这是我的代码:

/*1*/ public View getView(int position, View convertView, ViewGroup parent) {
/*2*/    View vi = convertView;
/*3*/    if (convertView == null)
/*4*/        vi = inflater.inflate(R.layout.result_list_item, null);
/*5*/
/*6*/    TextView title = (TextView) vi.findViewById(R.id.tvTitle);
/*7*/    TextView subtitle = (TextView) vi.findViewById(R.id.tvSubTitle);
/*8*/    ImageView image = (ImageView) vi.findViewById(R.id.imageView1);
/*9*/
/*10*/   title.setText(data.get(position).getDescription().toString());
/*11*/   subtitle.setText(data.get(position).getDate().toString());
/*12*/   String img = "http://someLink/" + data.get(position).getId() + "a_t.jpg";
/*13*/   imageLoader.DisplayImage(img, image);
/*14*/   return vi;
/*15*/ }

但是,我注意到在第 6 行,程序因 NullPointer 异常而崩溃。

这是我的崩溃报告:

04-18 09:33:32.745: E/AndroidRuntime(2209): FATAL EXCEPTION: main
04-18 09:33:32.745: E/AndroidRuntime(2209): java.lang.NullPointerException
04-18 09:33:32.745: E/AndroidRuntime(2209):     at com.package.mojo.NoPicAdapter.getView(NoPicAdapter.java:46)
04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.widget.AbsListView.obtainView(AbsListView.java:1554)
04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1264)
04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.widget.ListView.onMeasure(ListView.java:1175)
04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.view.View.measure(View.java:8366)
04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.view.View.measure(View.java:8366)
04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.view.View.measure(View.java:8366)
04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.view.View.measure(View.java:8366)
04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.view.View.measure(View.java:8366)
04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.view.ViewRoot.performTraversals(ViewRoot.java:847)
04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1868)
04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.os.Looper.loop(Looper.java:123)
04-18 09:33:32.745: E/AndroidRuntime(2209):     at android.app.ActivityThread.main(ActivityThread.java:3691)
04-18 09:33:32.745: E/AndroidRuntime(2209):     at java.lang.reflect.Method.invokeNative(Native Method)
04-18 09:33:32.745: E/AndroidRuntime(2209):     at java.lang.reflect.Method.invoke(Method.java:507)
04-18 09:33:32.745: E/AndroidRuntime(2209):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
04-18 09:33:32.745: E/AndroidRuntime(2209):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
04-18 09:33:32.745: E/AndroidRuntime(2209):     at dalvik.system.NativeStart.main(Native Method)

当然,布局和项目本身存在......

有什么想法吗?

更新:当我在调试时查看变量时,我发现它vi没有被夸大(vi == null),为什么?

4

3 回答 3

13

改变这个:

vi = inflater.inflate(R.layout.result_list_item, null);

对此:

vi = inflater.inflate(R.layout.result_list_item, parent, false);

你的适配器应该是这样的:

public class NoPicAdapter extends ArrayAdapter<NewAndCalendar> {

    private ArrayList<NewAndCalendar> data;
    private Activity mActivity;
    private LayoutInflater inflater = null;

    public NoPicAdapter(Activity a, ArrayList<NewAndCalendar> d) {
        super(a, R.layout.no_pic_list_item, d);
        mActivity = a;
        data = d;
        inflater = (LayoutInflater) mActivity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    @Override
    public View getView(int position, @Nullable View convertView, ViewGroup parent) {
        View vi = convertView;
        if (vi == null)
            vi = inflater.inflate(R.layout.no_pic_list_item, parent, false);
        TextView title = (TextView) vi.findViewById(R.id.noPicTitle);
        TextView subtitle = (TextView) vi.findViewById(R.id.noPicSubtitle);

        title.setText(data.get(position).getmTitle());
        subtitle.setText(data.get(position).getmPubDate());

        return vi;
    }

}
于 2012-04-18T06:53:49.633 回答
2

尝试这个

vi = LayoutInflater.from(mActivity).inflate(R.layout.result_list_item, null);

如果 mActivity 是从创建此适配器对象的活动传递的活动上下文。

于 2012-04-18T07:05:01.553 回答
1

你在初始化充气机吗?如果不初始化它。

inflater = getLayoutInflater();

或者

 inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
于 2012-04-18T06:56:43.900 回答