由于某种原因,我得到了这个布尔值的空对象引用,我不知道为什么。我无法弄清楚为什么布尔值返回一个空对象引用
这是活动代码
package com.spizer.mizer2;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
public class ProblemSelector extends AppCompatActivity {
public Boolean AddProb;
public Boolean SubProb;
public Boolean MultiProb;
public Boolean DivisProb;
public int ANum = 0;
public int SNum;
public int MNum;
public int DNum;
//ProblemSelector PS = new ProblemSelector();
CheckBox checkbox1, checkbox2, checkbox3, checkbox4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_problem_selector);
checkbox1 = (CheckBox) findViewById(R.id.checkBox1);
checkbox2 = (CheckBox) findViewById(R.id.checkBox2);
checkbox3 = (CheckBox) findViewById(R.id.checkBox3);
checkbox4 = (CheckBox) findViewById(R.id.checkBox4);
checkbox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
AddProb = true;
Toast.makeText(getBaseContext(), "Add: True", Toast.LENGTH_SHORT).show();
} else {
AddProb = false;
Toast.makeText(getBaseContext(), "Add: False", Toast.LENGTH_SHORT).show();
}
}
});
checkbox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
SubProb = true;
if(AddProb == false) {
SNum = 0;
}
else if(AddProb == true) {
SNum = 1;
}
} else {
SubProb = false;
}
}
});
checkbox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
MultiProb = true;
if(AddProb == false && SubProb == false) {
MNum = 0;
}
else if(AddProb == true && SubProb == false || AddProb == false && SubProb == true) {
MNum = 1;
}
else if(AddProb == true && SubProb == true) {
MNum = 2;
}
} else {
MultiProb = false;
}
}
});
checkbox4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
DivisProb = true;
if(AddProb == false && SubProb == false && MultiProb == false) {
DNum = 0;
}
else if(AddProb == true && SubProb == false && MultiProb == false || AddProb == false && SubProb == true && MultiProb == false || AddProb == false && SubProb == false && MultiProb == true) {
DNum = 1;
}
else if(AddProb == true && SubProb == true && MultiProb == false || AddProb == false && SubProb == true && MultiProb == true || AddProb == true && SubProb == false && MultiProb == true) {
DNum = 2;
}
else if(AddProb == true && SubProb == true && MultiProb == true) {
DNum = 3;
}
} else {
DivisProb = 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.menu_problem_selector, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/** this is called when the user hits the continue button **/
public void DifficultyMenu(View view) {
Intent DifficultyView = new Intent(this, DifficultyMenu.class);
//DifficultyView.putExtra("addProb", PS.AddProb);
//DifficultyView.putExtra("subProb", PS.SubProb);
//DifficultyView.putExtra("multiProb", PS.MultiProb);
//DifficultyView.putExtra("divisProb", PS.DivisProb);
startActivity(DifficultyView);
}
}
这是完整的错误日志,它发生在第101行和第75行
09-21 11:26:24.671 5078-5078/com.spizer.mizer2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.spizer.mizer2, PID: 5078
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Boolean.booleanValue()' on a null object reference
at com.spizer.mizer2.ProblemSelector$3.onCheckedChanged(ProblemSelector.java:75)
at android.widget.CompoundButton.setChecked(CompoundButton.java:161)
at android.widget.CompoundButton.toggle(CompoundButton.java:115)
at android.widget.CompoundButton.performClick(CompoundButton.java:127)
at android.view.View$PerformClick.run(View.java:20916)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5972)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
答案 0 :(得分:1)
你应该使用布尔基元数据类型而不是布尔类对象(注意布尔中的小b) When should null values of Boolean be used?
另外,如果你真的想使用布尔对象,你应该使用equals()来比较值