检查是否选中复选框返回空指针

时间:2011-12-23 07:42:12

标签: java android checkbox

我有一个可扩展列表的代码,我希望在列表视图的子组中有一个复选框,并检查是否选中了其中一​​个复选框。 问题是当我检查复选框是否被选中时,我得到一个NULL指针异常。

你能告诉我什么错了吗?

这是我的代码 - 我编辑了代码以扩充一个包含该复选框的child_row.xml视图,但我仍然得到那个空指针,我做错了什么?!?!

   package send.Shift;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;

public class Shifts extends ExpandableListActivity implements
        OnCheckedChangeListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.list);

        SimpleExpandableListAdapter expListAdapter = new SimpleExpandableListAdapter(
                this, createGroupList(), R.layout.group_row,
                new String[] { "Group Item" }, new int[] { R.id.row_name },
                createChildList(), R.layout.child_row,
                new String[] { "Sub Item" }, new int[] { R.id.grp_child });
        getExpandableListView().setGroupIndicator(
                getResources().getDrawable(R.drawable.expander_group));

        ExpandableListView EX = (ExpandableListView) findViewById(android.R.id.list);
        EX.setAdapter(expListAdapter);

        final CheckBox childBox = (CheckBox) findViewById(R.id.childBOX);

        final TextView choosenGroup = (TextView) findViewById(R.id.choosen);



        LayoutInflater inflater = LayoutInflater.from(Shifts.this);
        View view2 = inflater.inflate(R.layout.child_row, null);


        childBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                // TODO Auto-generated method stub
                if(childBox.isChecked() == true){
                choosenGroup.setText("Shift Set");
                }
            }
        });

    }

以下是一些Logcat日志:

12-23 07:38:00.644: W/dalvikvm(880): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-23 07:38:00.654: E/AndroidRuntime(880): FATAL EXCEPTION: main
12-23 07:38:00.654: E/AndroidRuntime(880): java.lang.RuntimeException: Unable to start activity ComponentInfo{send.Shift/send.Shift.Shifts}: java.lang.NullPointerException
12-23 07:38:00.654: E/AndroidRuntime(880):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-23 07:38:00.654: E/AndroidRuntime(880):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-23 07:38:00.654: E/AndroidRuntime(880):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-23 07:38:00.654: E/AndroidRuntime(880):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-23 07:38:00.654: E/AndroidRuntime(880):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-23 07:38:00.654: E/AndroidRuntime(880):  at android.os.Looper.loop(Looper.java:123)
12-23 07:38:00.654: E/AndroidRuntime(880):  at android.app.ActivityThread.main(ActivityThread.java:3683)
12-23 07:38:00.654: E/AndroidRuntime(880):  at java.lang.reflect.Method.invokeNative(Native Method)
12-23 07:38:00.654: E/AndroidRuntime(880):  at java.lang.reflect.Method.invoke(Method.java:507)
12-23 07:38:00.654: E/AndroidRuntime(880):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-23 07:38:00.654: E/AndroidRuntime(880):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-23 07:38:00.654: E/AndroidRuntime(880):  at dalvik.system.NativeStart.main(Native Method)
12-23 07:38:00.654: E/AndroidRuntime(880): Caused by: java.lang.NullPointerException
12-23 07:38:00.654: E/AndroidRuntime(880):  at send.Shift.Shifts.onCreate(Shifts.java:41)
12-23 07:38:00.654: E/AndroidRuntime(880):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-23 07:38:00.654: E/AndroidRuntime(880):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-23 07:38:00.654: E/AndroidRuntime(880):  ... 11 more

3 个答案:

答案 0 :(得分:2)

如果你在某一行上得到一个nullpointer,那一行就是null。如果是if(childBox.isChecked()行,则childBox可能是null。很难说没有堆栈,但最可能的原因是您检索该复选框的行。

 final CheckBox childBox = (CheckBox) findViewById(R.id.childBOX);

可能会返回null,这可能有几个原因。您的ID可能是childBox,而不是childBOX。或者它不在R.layout.list

你能做的最好的事情就是开始调试。什么行是错误。找到null的对象。找出原因null以及是否有预期。

答案 1 :(得分:0)

您可以使用isChecked值,而不是检查childBox.isChecked()。所以你的代码看起来像这样:

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if(isChecked == true){
      choosenGroup.setText("Shift Set");
    }
}

答案 2 :(得分:0)

检查您的布局list.xml我认为此复选框可能缺少android:id="@+id/childBOX"或TextView <{1}}

检查下面的样本

android:id="@+id/choosen"