空对象引用上的复选框android.widget.CheckBox.setVisibility(int)'出错

时间:2017-12-15 07:58:47

标签: android

   checkBox=(CheckBox) findViewById(checkbox);
   checkBox.setVisibility(View.GONE);

    db = new Db(Listdata.this,"register2",null,2);
    list2 = db.listdata();
    Log.d("list", String.valueOf(list2));
    listAdapter adapter = new listAdapter(Listdata.this,list2);
    listView.setAdapter(adapter);
    a= list2.size();
    Log.d("teb", String.valueOf(a));

    listView.setLongClickable(true);
    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos, long id) {
            for(int i = 0; i!=a; i++) {
                checkBox = (CheckBox) listView.getChildAt(i).findViewById(R.id.checkbox);
                checkBox.setVisibility(View.VISIBLE);
            }
            return true;
        }
    });

致命的例外:主要                                                                                 处理:com.example.developer.register,PID:21571

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
                                                                                at com.example.developer.register.Listdata$1.onItemLongClick(Listdata.java:52)
                                                                                at android.widget.AbsListView.performLongPress(AbsListView.java:3112)
                                                                                at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:3061)
                                                                                at android.os.Handler.handleCallback(Handler.java:739)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                at android.os.Looper.loop(Looper.java:135)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5221)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

1 个答案:

答案 0 :(得分:1)

如果您想从ListView复选框中获取价值,请尝试此

View view = ((ViewGroup)arg1).getChildAt(i);
checkBox = (CheckBox) view.findViewById(R.id.checkboxId);
checkBox.setVisibility(View.VISIBLE);