自定义listview的ArrayAdapter报空指针

布局activity_list_view_test.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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".ListViewTest" >

    <TextView
        android:id="@+id/txtselection"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </TextView>

    <ListView
        android:id="@android:id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="false"
        android:orientation="vertical" >
    </ListView>

</LinearLayout>


row  list_vew_test_row.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="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="22dp"
        android:layout_height="wrap_content"
        android:paddingLeft="2dp"
        android:paddingRight="2dp"
        android:paddingTop="2dp"
        android:src="@drawable/ok" />

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="44sp" />

</LinearLayout>


java代码

public class ListViewTest extends ListActivity {

    TextView selection;
    String[] items = {
            "Apple", "orange", "Banana", "peach", "Grape", "Strawberry", "matermelon", "lemon"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view_test);
        //setContentView(R.layout.list_vew_test_row);
        // setListAdapter(new ArrayAdapter<String>(this,
        // android.R.layout.simple_list_item_1, items));
        setListAdapter(new IconicAdapter());

        selection = (TextView)findViewById(R.id.txtselection);
        selection.setText("Please select");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.list_view_test, menu);
        return true;
    }

    public void onListItemClick(ListView parent, View v, int position, long id) {

        selection.setText(items[position]);
    }

    class IconicAdapter extends ArrayAdapter {
        IconicAdapter() {
            super(ListViewTest.this, R.layout.list_vew_test_row, items);
        }

        public View getView(int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater = getLayoutInflater();
            View row = inflater.inflate(R.layout.list_vew_test_row, parent, false);
            TextView label = (TextView)findViewById(R.id.label);
            label.setText(items[position]);

            ImageView icon = (ImageView)findViewById(R.id.icon);
            if (items[position].length() >= 5) {
                icon.setImageResource(R.drawable.ok);
            } else {
                icon.setImageResource(R.drawable.delete);
            }
            return row;
        }
    }
}


error:

D/TextLayoutCache( 8977): Using debug level: 0 - Debug Enabled: 0
D/AndroidRuntime( 8977): Shutting down VM
W/dalvikvm( 8977): threadid=1: thread exiting with uncaught exception (group=0x40aaf210)
E/AndroidRuntime( 8977): FATAL EXCEPTION: main
E/AndroidRuntime( 8977): java.lang.NullPointerException
E/AndroidRuntime( 8977):  at com.example.test.ListViewTest$IconicAdapter.getView(ListViewTest.java:58)
E/AndroidRuntime( 8977):  at android.widget.AbsListView.obtainView(AbsListView.java:2043)
E/AndroidRuntime( 8977):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
E/AndroidRuntime( 8977):  at android.widget.ListView.onMeasure(ListView.java:1155)
E/AndroidRuntime( 8977):  at android.view.View.measure(View.java:12897)
E/AndroidRuntime( 8977):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4805)
E/AndroidRuntime( 8977):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1385)
E/AndroidRuntime( 8977):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:676)
E/AndroidRuntime( 8977):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:557)
E/AndroidRuntime( 8977):  at android.view.View.measure(View.java:12897)
E/AndroidRuntime( 8977):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4805)
E/AndroidRuntime( 8977):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:297)
E/AndroidRuntime( 8977):  at android.view.View.measure(View.java:12897)
E/AndroidRuntime( 8977):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:828)
E/AndroidRuntime( 8977):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:557)
E/AndroidRuntime( 8977):  at android.view.View.measure(View.java:12897)
E/AndroidRuntime( 8977):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4805)
E/AndroidRuntime( 8977):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:297)
E/AndroidRuntime( 8977):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
E/AndroidRuntime( 8977):  at android.view.View.measure(View.java:12897)
E/AndroidRuntime( 8977):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
E/AndroidRuntime( 8977):  at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
E/AndroidRuntime( 8977):  at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 8977):  at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 8977):  at android.app.ActivityThread.main(ActivityThread.java:4441)
E/AndroidRuntime( 8977):  at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 8977):  at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 8977):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
E/AndroidRuntime( 8977):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
E/AndroidRuntime( 8977):  at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager(  376):   Force finishing activity com.example.test/.ListViewTest


58行:
label.setText(items[position]);

阅读更多
上一篇拼音如何转化为汉字
下一篇为什么安卓开发者文档里找不到RecyclerView类?
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭