简体   繁体   中英

check if checkbox is checked return null pointer

I have this code for an expandable list, I want to have a checkbox in the childgroups of the list view, and check if one of the checkboxes is checked. The problem is that when I check if the checkbox is checked I get a NULL Pointer Exception.

Can you please tell me whats wrong?

here's my code - I've edited the code to inflate a view of the child_row.xml that holds that checkbox but I still get that null pointer, what am I doing wrong?!?!

   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");
                }
            }
        });

    }

Here is some of the Logcat log:

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

If you get a nullpointer on a certain line, something on that line is null . If it is the if(childBox.isChecked() line, probably childBox is null . Hard to say without the stack, but most probable cause is the line where you retrieve that checkbox.

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

Might be returning null , and this could be for several reasons. It could be your id is childBox instead of childBOX . Or that it is not in R.layout.list .

The best thing you can do is start debugging. What line is the error. Find the object that is null . Find out why it is null and if that is expected or not.

Instead of checking the childBox.isChecked() you can use the isChecked value. So your code would look like this:

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

Check your layout list.xml I think this layout may is missing android:id="@+id/childBOX" for Check Box or android:id="@+id/choosen" for TextView

Check the folowin sample

<CheckBox android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/childBOX"/>
    <TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/choosen"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM