我尝试了很多次,但实际上我正在创建一个设置页面,我的意思是设置按钮,在哪里处理打开和关闭 在我从应用程序扩展的应用程序类中 它向我展示了布尔引用null异常。 以下是清单。
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name=".ApplicationClass">
<activity android:name=".SplashScr">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
这是java类。
public class ApplicationClass extends Application {
MyService.Reciver rec;
private Context context;
public Boolean getSoundFX_State() {
return soundFX_State;
}
public void setSoundFX_State(Boolean soundFX_State) {
this.soundFX_State = soundFX_State;
}
Boolean soundFX_State;
@Override
public void onCreate()
{
super.onCreate();
regster();
pndng();
setSoundFX_State(true);
}
protected void regster()
{
try {
rec=new MyService().new Reciver();
if(soundFX_State==true) {
registerReceiver(
rec, new IntentFilter(
"android.intent.action.PHONE_STATE"));
Toast.makeText(getBaseContext(), "Your call can disconect automatically", Toast.LENGTH_SHORT)
.show();
}
和setting.java类。
public class Setting extends Activity {
MyService.Reciver rec;
private CheckBox c1,c2;
protected ApplicationClass app;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setting);
app=(ApplicationClass)getApplication();
c1=(CheckBox)findViewById(R.id.checkBox);
c2=(CheckBox)findViewById(R.id.checkBox2);
rec=new MyService().new Reciver();
if(app.getSoundFX_State())
{
c1.setChecked(true);
}
else {
c1.setChecked(false);
}
c1.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
Toast.makeText(getBaseContext(), "c1", Toast.LENGTH_LONG).show();
app.setSoundFX_State(true);
}
else {
app.setSoundFX_State(false);
Toast.makeText(getBaseContext(),"off",Toast.LENGTH_SHORT).show();
}
}
});
c2.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(getBaseContext(),"c2",Toast.LENGTH_LONG).show();
}
});
{
};
}
public void chkboxclicked(View view)
{
}
}
答案 0 :(得分:0)
1)更改
Boolean soundFX_State;
到
boolean soundFX_State;
<强> 2)强>
@Override
public void onCreate()
{
super.onCreate();
setSoundFX_State(true);
regster();
pndng();
}
希望这是有道理的。