2015-09-21 89 views
-2

由于某种原因我得到这个布尔的空对象引用,我不知道为什么。我无法弄清楚,为什么布尔返回一个空对象引用null对象参考布尔

这里活动代码

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

你能告诉我们代码中的第75行吗? – Heinrich

+0

检查此http://stackoverflow.com/questions/1295170/whats-the-difference-between-boolean-and-boolean-in-java 它将更好地使用布尔值,而不是布尔值 – Pavan

回答

1

您应该使用布尔原语数据类型而不是布尔类对象(注意布尔中的小b) When should null values of Boolean be used?

另外,如果你真的想要使用布尔对象,你应该使用equals()来比较值

+0

此错误只发生在第3和第4如果(b)班不是第一和第2班 –

+0

,因为在第一堂课你初始化AddProb和第二次使用它检查直接第二点击 – Pavan

+0

没关系,但为什么其他人不工作? –