0

我有一个 ListActivity 类,在其中,我使用 ArrayAdapter 来填充列表。问题是,当我尝试启动活动时,它会崩溃并显示以下错误消息:

08-18 18:09:27.443: E/AndroidRuntime(480): FATAL EXCEPTION: main
08-18 18:09:27.443: E/AndroidRuntime(480): java.lang.NullPointerException
08-18 18:09:27.443: E/AndroidRuntime(480):  at com.arox.havayollari.ListAirlines$MyListAdapter.getView(ListAirlines.java:114)
08-18 18:09:27.443: E/AndroidRuntime(480):  at android.widget.AbsListView.obtainView(AbsListView.java:1315)
08-18 18:09:27.443: E/AndroidRuntime(480):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
08-18 18:09:27.443: E/AndroidRuntime(480):  at android.widget.ListView.onMeasure(ListView.java:1109)
08-18 18:09:27.443: E/AndroidRuntime(480):  at android.view.View.measure(View.java:8171)
08-18 18:09:27.443: E/AndroidRuntime(480):  at android.widget.RelativeLayout.measureChild(RelativeLayout.java:563)
08-18 18:09:27.443: E/AndroidRuntime(480):  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:378)
08-18 18:09:27.443: E/AndroidRuntime(480):  at android.view.View.measure(View.java:8171)
08-18 18:09:27.443: E/AndroidRuntime(480):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
08-18 18:09:27.443: E/AndroidRuntime(480):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-18 18:09:27.443: E/AndroidRuntime(480):  at android.view.View.measure(View.java:8171)
08-18 18:09:27.443: E/AndroidRuntime(480):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
08-18 18:09:27.443: E/AndroidRuntime(480):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
08-18 18:09:27.443: E/AndroidRuntime(480):  at android.view.View.measure(View.java:8171)
08-18 18:09:27.443: E/AndroidRuntime(480):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
08-18 18:09:27.443: E/AndroidRuntime(480):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-18 18:09:27.443: E/AndroidRuntime(480):  at android.view.View.measure(View.java:8171)
08-18 18:09:27.443: E/AndroidRuntime(480):  at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
08-18 18:09:27.443: E/AndroidRuntime(480):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
08-18 18:09:27.443: E/AndroidRuntime(480):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-18 18:09:27.443: E/AndroidRuntime(480):  at android.os.Looper.loop(Looper.java:123)
08-18 18:09:27.443: E/AndroidRuntime(480):  at android.app.ActivityThread.main(ActivityThread.java:4627)
08-18 18:09:27.443: E/AndroidRuntime(480):  at java.lang.reflect.Method.invokeNative(Native Method)
08-18 18:09:27.443: E/AndroidRuntime(480):  at java.lang.reflect.Method.invoke(Method.java:521)
08-18 18:09:27.443: E/AndroidRuntime(480):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-18 18:09:27.443: E/AndroidRuntime(480):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-18 18:09:27.443: E/AndroidRuntime(480):  at dalvik.system.NativeStart.main(Native Method)

这是我的自定义数组适配器代码:

@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            View view = convertView;

            if (view == null) {
                LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                inflater.inflate(R.layout.airline_item, null);
            }

            //It crashes at this line
            ImageView img = (ImageView) view.findViewById(R.id.imgAirline);


            TextView tview = (TextView) view.findViewById(R.id.tvAirline);
            tview.setText(lines[position].toString());


            return view;
        }

还有我的airline_item.xml

<?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="wrap_content" >

    <ImageView
        android:id="@+id/imgAirline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/logo" />

    <TextView
        android:id="@+id/tvAirline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="Medium Text"
        android:layout_marginLeft="20dip"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

我真的不明白问题是什么。任何帮助表示赞赏。

4

1 回答 1

1

您正在膨胀布局,但没有膨胀到任何View. 这就是你的充气应该如何工作:

if (view == null) {
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = inflater.inflate(R.layout.airline_item, null);
}
于 2012-08-18T18:26:25.907 回答