android复选框获取java.lang.nullpointerexception而setchecked复选框为false

时间:2013-11-19 09:30:47

标签: java android checkbox

由于这个问题,我真的很头晕,为什么我总是得到java.lang.nullpointerexception而我试图设置复选框false,但是当我设置复选框为true时,它正常工作。

这是我的代码

private int jlhChampion = 5;
private int jlhParticipant = 14;
private int jlhRegisterType = 4;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.detail_tournament);
    titleTournament = (TextView) findViewById(R.id.tournament_detail_title);
    descTournament = (TextView) findViewById(R.id.tournament_title_desc);
    titleTournament = (TextView) findViewById(R.id.tournament_detail_title);
    descTournament = (TextView) findViewById(R.id.tournament_title_desc);
    descStatusTournament = (TextView) findViewById(R.id.txt_tournament_detail_status_desc);
    layout_champion_tournament = (LinearLayout) findViewById(R.id.layout_champion_detail_tournament);
    layoutParticipant = (LinearLayout) findViewById(R.id.layout_participant_detail_status_desc);
    layoutRegistrationType = (LinearLayout) findViewById(R.id.layout_registration_type_tournament_detail);
    titleTournament.setText(tournamentTest);
    descStatusTournament.setText(tournamentDescStatusTest);
    initPastChampion();
    initPastParticipant();
    initRegistrationType();

}

private void initPastChampion() {
    if (jlhChampion != 0) {
        if (jlhChampion > 4) {
            jlhChampion = jlhChampion - (jlhChampion - 4);
        }
        for (int i = 0; i < jlhChampion; i++) {
            LayoutInflater inflater = (LayoutInflater) this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(
                    R.layout.tournament_participant_view, null);
            ((TextView) view
                    .findViewById(R.id.txt_title_status_participant))
                    .setText("Nama user" + i);
            ((TextView) view
                    .findViewById(R.id.txt_title_status_participant))
                    .setTextAppearance(getApplicationContext(),
                            R.style.text_sub_title_white_style);
            ((TextView) view
                    .findViewById(R.id.txt_description_status_participant))
                    .setText(detailChampion + " " + i);
            ((TextView) view
                    .findViewById(R.id.txt_description_status_participant))
                    .setTextAppearance(getApplicationContext(),
                            R.style.textContentWhite_style);
            Button b = ((Button) view
                    .findViewById(R.id.btn_view_profile_of_participant));
            b.setOnClickListener(clickButton);
            layout_champion_tournament.addView(view);
            b.setTag("Champion " + i);
        }
    } else {

    }
}

private void initPastParticipant() {
    if (jlhParticipant != 0) {
        if (jlhParticipant > 4) {
            jlhParticipant = jlhParticipant - (jlhParticipant - 4);
        }
        for (int i = 0; i < jlhParticipant; i++) {
            LayoutInflater inflater = (LayoutInflater) this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(
                    R.layout.tournament_participant_view, null);
            ((TextView) view
                    .findViewById(R.id.txt_title_status_participant))
                    .setText("Nama user" + " Participant " + i);
            ((TextView) view
                    .findViewById(R.id.txt_title_status_participant))
                    .setTextAppearance(getApplicationContext(),
                            R.style.text_sub_title_white_style);
            ((TextView) view
                    .findViewById(R.id.txt_description_status_participant))
                    .setText(registrationType + " " + i);
            ((TextView) view
                    .findViewById(R.id.txt_description_status_participant))
                    .setTextAppearance(getApplicationContext(),
                            R.style.textContentWhite_style);
            Button b = ((Button) view
                    .findViewById(R.id.btn_view_profile_of_participant));
            b.setOnClickListener(clickButton);
            layoutParticipant.addView(view);
            b.setTag("Participant " + i);

        }
    } else {

    }
}

private void initRegistrationType() {
    if (jlhRegisterType != 0) {
        for (int i = 0; i < jlhRegisterType; i++) {
            LayoutInflater inflater = (LayoutInflater) this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.registration_type_item,
                    null);
            ((TextView) view
                    .findViewById(R.id.txt_title_status_participant))
                    .setText("Register " + i);
            ((TextView) view
                    .findViewById(R.id.txt_title_status_participant))
                    .setTextAppearance(getApplicationContext(),
                            R.style.text_sub_title_white_style);
            ((TextView) view
                    .findViewById(R.id.txt_description_of_registration_type))
                    .setText(detailChampion + " " + " Participant " + i);
            ((TextView) view
                    .findViewById(R.id.txt_description_of_registration_type))
                    .setTextAppearance(getApplicationContext(),
                            R.style.textContentWhite_style);
            CheckBox c = ((CheckBox) view
                    .findViewById(R.id.chk_register_type));
            c.setOnClickListener(clickButton);
            layoutRegistrationType.addView(view);
            c.setTag("RegistrationType " + i + " " + jlhRegisterType);
        }
    } else {

    }
}

OnClickListener clickButton = new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        String idxStr = (String) v.getTag();
        CheckBox checkRegistrationTypes;
        if (idxStr.startsWith("RegistrationType", 0)) {
            if(((CheckBox)v).isPressed()){
                String[] regType = idxStr.split(" ");
                String[] regTypes = new String[Integer.parseInt(regType[2])];
                for (int i = 0; i < regTypes.length; i++) {
                    checkRegistrationTypes = (CheckBox) v.findViewById(R.id.chk_register_type)
                            .findViewWithTag("RegistrationType " + i + " " + regTypes.length);

                    if(Integer.toString(i).equals(regType[1])){
                        checkRegistrationTypes.setChecked(true);
                    }else{
                        //this is the problem
                        checkRegistrationTypes.setChecked(false);
                    }
                }
            }
        }
    }
};

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

我得到了这样的错误

W/dalvikvm(2648): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
 E/AndroidRuntime(2648): FATAL EXCEPTION: main
 E/AndroidRuntime(2648): java.lang.NullPointerException
 E/AndroidRuntime(2648):    at com.chess.DetailTournament$1.onClick(DetailTournament.java:194)
 E/AndroidRuntime(2648):    at android.view.View.performClick(View.java:4204)
 E/AndroidRuntime(2648):    at android.widget.CompoundButton.performClick(CompoundButton.java:100)
 E/AndroidRuntime(2648):    at android.view.View$PerformClick.run(View.java:17355)
 E/AndroidRuntime(2648):    at android.os.Handler.handleCallback(Handler.java:725)
 E/AndroidRuntime(2648):    at android.os.Handler.dispatchMessage(Handler.java:92)
 E/AndroidRuntime(2648):    at android.os.Looper.loop(Looper.java:137)
 E/AndroidRuntime(2648):    at android.app.ActivityThread.main(ActivityThread.java:5041)
 E/AndroidRuntime(2648):    at java.lang.reflect.Method.invokeNative(Native Method)
 E/AndroidRuntime(2648):    at java.lang.reflect.Method.invoke(Method.java:511)
 E/AndroidRuntime(2648):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
 E/AndroidRuntime(2648):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
 E/AndroidRuntime(2648):    at dalvik.system.NativeStart.main(Native Method)
 E/Trace(2698): error opening trace file: No such file or directory (2)

2 个答案:

答案 0 :(得分:1)

 checkRegistrationTypes = (CheckBox) v.findViewById(R.id.chk_register_type)
                            .findViewWithTag("RegistrationType " + i + " " + regTypes.length);
你正在错误的地方查看你的复选框。它属于您对活动的视图层次结构,而不是您按下的视图。你也应该检查NPE。

使用

checkRegistrationTypes = (CheckBox)  findViewById(R.id.chk_register_type)
                            .findViewWithTag("RegistrationType " + i + " " + regTypes.length);

答案 1 :(得分:0)

setOnCheckedChangeListener使用CheckBox c代替setOnClickListener

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    int id = buttonView.getId();

    if(id == R.id.chk_register_type){
              // ....
    }

通过这种方式,您将确保加载具有正确ID

的CheckBox