gpt4 book ai didi

java - android 复选框在 setchecked 复选框为 false 时获取 java.lang.nullpointerException

转载 作者:行者123 更新时间:2023-12-01 13:49:39 25 4
gpt4 key购买 nike

因为这个问题我真的很头晕,为什么当我尝试将 checkbox 设置为 false 时总是出现 java.lang.nullpointerException,但是当我设置 checkbox 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)

最佳答案

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

您在错误的位置查看了复选框。它属于您的 Activity 的 View 层次结构,不属于您按下的 View 。您还应该检查 NPE。

使用

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

关于java - android 复选框在 setchecked 复选框为 false 时获取 java.lang.nullpointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20067769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com