在我的应用程序中,我希望默认情况下音乐将会运行。但是如果点击about.java中的开关那么它应该关闭。所以我做了一个全局变量。但问题是当我点击开关关闭时,它会出错。
MainActivity.java
package com.example.asifsabir.breathholdingchallenge;
import android.content.Intent;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity {
TextView tv1;
private android.support.v7.widget.Toolbar toolbar;
EditText et1, et2;
Button bt1, bt2;
public int min = 0;
public int sec = 0;
public int second = 0;
public CountDownTimer myC;
public MediaPlayer mySong;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
toolbar.setTitle("Hold Breath Challenge");
toolbar.setSubtitleTextColor(R.color.colortitle);
toolbar.setPopupTheme(R.style.AppTheme);
toolbar.inflateMenu(R.menu.main_menu);
toolbar.setTitleTextColor(Color.WHITE);
tv1 = (TextView) findViewById(R.id.textView1);
et1 = (EditText) findViewById(R.id.et1);
et2 = (EditText) findViewById(R.id.et2);
bt1 = (Button) findViewById(R.id.bt1);
bt2 = (Button) findViewById(R.id.bt2);
mySong = MediaPlayer.create(getApplicationContext(), R.raw.heartbeat);
mySong.setLooping(true);
MyApplication mAxx = (MyApplication) getApplicationContext();
if (mAxx.getGlobalVarValue())
mySong.start();
else
mySong.pause();
Toast.makeText(MainActivity.this, "You must click RESET before stating a new countdown!", Toast.LENGTH_LONG).show();
bt1.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
if ((!et1.getText().toString().isEmpty() && !et2.getText().toString().isEmpty()) ||
!et1.getText().toString().isEmpty() ||
!et2.getText().toString().isEmpty()) {
if (!et1.getText().toString().isEmpty())
min = Integer.parseInt(et1.getText().toString());
if (et1.getText().toString().isEmpty())
min = 0;
if (!et2.getText().toString().isEmpty())
second = Integer.parseInt(et2.getText().toString());
if (et2.getText().toString().isEmpty())
second = 0;
sec = min * 60 * 1000 + second * 1000;
myC = new CountDownTimer(sec, 1000) {
public void onTick(long millisUntilFinished) {
tv1.setText("" + String.format("%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
}
public void onFinish() {
tv1.setText("done!");
}
}.start();
}
}
}
);
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myC.cancel();
tv1.setText("Count");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, 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.
switch (item.getItemId()) {
//noinspection SimplifiableIfStatement
case R.id.about:
startActivity(new Intent(getApplicationContext(), About.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
MyApplcation.java
package com.example.asifsabir.breathholdingchallenge;
import android.app.Application;
/**
* Created by Asif Sabir on 29-Apr-16.
*/
public class MyApplication extends Application {
public static boolean mGlobalVarValue=true;
public boolean getGlobalVarValue() {
return mGlobalVarValue;
}
public void setGlobalVarValue(boolean str) {
mGlobalVarValue = str;
}
}
About.java
package com.example.asifsabir.breathholdingchallenge;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;
/**
* Created by Asif Sabir on 20-Apr-16.
*/
public class About extends AppCompatActivity {
android.support.v7.widget.Toolbar t1;
public static MyApplication myA;
Switch sw1;
public MyApplication mApp = (MyApplication) getApplication();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
sw1 = (Switch) findViewById(R.id.sw1);
t1 = (android.support.v7.widget.Toolbar) findViewById(R.id.about_bar);
t1.setTitleTextColor(Color.WHITE);
setSupportActionBar(t1);
getSupportActionBar().setTitle("About");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
sw1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
mApp.setGlobalVarValue(true);
} else {
mApp.setGlobalVarValue(false);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
@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.
switch (item.getItemId()) {
//noinspection SimplifiableIfStatement
case R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
错误信息是:
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.asifsabir.breathholdingchallenge.MyApplication.setGlobalVarValue(boolean)' on a null object reference
at com.example.asifsabir.breathholdingchallenge.About$1.onCheckedChanged(About.java:41)
答案 0 :(得分:0)
如果你的意图(基于你的评论)真的要做到以下几点:
如何在包含布尔值的设置中使用开关,并根据该布尔值更改其他活动?
然后可能的解决方案是: