Reputation: 7
I was really dizzy because of this problem, why i always getting java.lang.nullpointerexception while i try to set checkbox false, but when i set checkbox true , it is working normally.
this is my code
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;
}
I got error like this
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)
Upvotes: 0
Views: 737
Reputation: 77904
Use setOnCheckedChangeListener
for CheckBox c
instead setOnClickListener
.
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int id = buttonView.getId();
if(id == R.id.chk_register_type){
// ....
}
By this way you will be sure that you load CheckBox with proper id
Upvotes: 0
Reputation: 157437
checkRegistrationTypes = (CheckBox) v.findViewById(R.id.chk_register_type)
.findViewWithTag("RegistrationType " + i + " " + regTypes.length);
you are looking your checkbox in the wrong place. it belongs to the View hiearchy of your activity no to the view you pressed. Also you should check against NPE.
Use
checkRegistrationTypes = (CheckBox) findViewById(R.id.chk_register_type)
.findViewWithTag("RegistrationType " + i + " " + regTypes.length);
Upvotes: 1