Listview与Checkbox和textview

时间:2013-01-01 06:51:48

标签: android listview

在我的应用程序中,我有一个列表视图,其中包含一个复选框和textview,以及屏幕底部的按钮。当用户单击按钮时,我必须在列表视图中获取所选项目的数组。试过这个

我的XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">

    <Button android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:id="@+id/testbutton"
        android:text="OK" android:layout_alignParentBottom="true" />

    <ListView android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:id="@+id/listforSetting"
        android:layout_alignParentTop="true" android:layout_above="@id/testbutton" />

</RelativeLayout> 

我的活动:

String[] item = common.context.getInstance().getCategoryitem();
    String[] name;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listforsetting);
        final ListView lView = (ListView) findViewById(R.id.listforSetting);
        Button b=(Button)findViewById(R.id.testbutton);

        lView.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_multiple_choice, item));
        lView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);



        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                SparseBooleanArray checked = lView.getCheckedItemPositions();

                for (int i = 0; i < item.length; i++){
                    if (checked.get(i))
                    {
                        name[i]=lView.getItemAtPosition(i).toString();
                        System.out.println("item="+name[i]);
                    }

                }


            }
        });

但它显示为空指针异常(单击按钮时)..请帮我解决此问题。

我的logcat:

01-01 12:17:20.817: E/AndroidRuntime(584): Uncaught handler: thread main exiting due to uncaught exception
01-01 12:17:20.847: E/AndroidRuntime(584): java.lang.NullPointerException
01-01 12:17:20.847: E/AndroidRuntime(584):  at com.newspaper.ListforSetting$1.onClick(ListforSetting.java:39)
01-01 12:17:20.847: E/AndroidRuntime(584):  at android.view.View.performClick(View.java:2364)
01-01 12:17:20.847: E/AndroidRuntime(584):  at android.view.View.onTouchEvent(View.java:4179)
01-01 12:17:20.847: E/AndroidRuntime(584):  at android.widget.TextView.onTouchEvent(TextView.java:6541)
01-01 12:17:20.847: E/AndroidRuntime(584):  at android.view.View.dispatchTouchEvent(View.java:3709)
01-01 12:17:20.847: E/AndroidRuntime(584):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-01 12:17:20.847: E/AndroidRuntime(584):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-01 12:17:20.847: E/AndroidRuntime(584):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-01 12:17:20.847: E/AndroidRuntime(584):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-01 12:17:20.847: E/AndroidRuntime(584):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
01-01 12:17:20.847: E/AndroidRuntime(584):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
01-01 12:17:20.847: E/AndroidRuntime(584):  at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
01-01 12:17:20.847: E/AndroidRuntime(584):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
01-01 12:17:20.847: E/AndroidRuntime(584):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
01-01 12:17:20.847: E/AndroidRuntime(584):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-01 12:17:20.847: E/AndroidRuntime(584):  at android.os.Looper.loop(Looper.java:123)
01-01 12:17:20.847: E/AndroidRuntime(584):  at android.app.ActivityThread.main(ActivityThread.java:4363)
01-01 12:17:20.847: E/AndroidRuntime(584):  at java.lang.reflect.Method.invokeNative(Native Method)
01-01 12:17:20.847: E/AndroidRuntime(584):  at java.lang.reflect.Method.invoke(Method.java:521)
01-01 12:17:20.847: E/AndroidRuntime(584):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
01-01 12:17:20.847: E/AndroidRuntime(584):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
01-01 12:17:20.847: E/AndroidRuntime(584):  at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:1)

您未使用name[]

name[] = new String[item.length] ;
for (int i = 0; i < item.length; i++)
{
     if (checked.get(i))
     {
         name[i]=lView.getItemAtPosition(i).toString();
         System.out.println("item="+name[i]);
     }

}

答案 1 :(得分:0)

确保初始化name数组

答案 2 :(得分:0)

item[]数组的项目是什么?确保item[]数组包含要添加到其中的项目列表。并初始化String[] name=null;String[] name=new String[];