我创建了一个在后台系统中运行服务的应用程序和两个Activity。我在Activity1中启动了服务,在Activity2中,我设置了带有“是”和“否”的切换按钮。当我运行应用程序服务时,它是在activity1中启动的,而当从activity1中打开activity2时,使用共享的首选项将服务状态的切换按钮状态自动设置为“ ON”,但是当我按下用于停止服务的切换按钮“ off”时,则会收到NullPointerException.I已经尝试过,但没有用。请任何人帮助我。这是我的代码
public class Service_Demo extends Activity implements OnClickListener {
private static final String TAG = "ServicesDemo";
Button buttonStart, buttonStop;
// boolean value;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
System.out.println("In OnCreate(");
startService(new Intent(this, MyService.class));
buttonStart = (Button) findViewById(R.id.buttonStart);
buttonStart.setOnClickListener(this);
boolean value=getIntent().getExtras().getBoolean("toggleBtn");
Log.e("Boolean Value","4 toggle button in Service demo"+value);
}
public void onClick(View src)
{
Intent i=new Intent(this , Toggle_Activity.class);
startActivity(i);
}
}
public class Toggle_Activity extends Activity
{
ToggleButton tgButton;
private boolean isService=false;
private String strService;
public final String service_Prefs="servicePrefs";
private static final String StrMyService = "zdf";
public static boolean status=false;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.toggle);
final SharedPreferences servicePrefs=this.getSharedPreferences("Service_Prefs",MODE_WORLD_READABLE);
strService=servicePrefs.getString(StrMyService , "myservice");
Log.e("",""+strService);
final boolean mBool = servicePrefs.getBoolean("myservice", true);
Log.e("Boolean Value mBool","="+mBool);
Boolean b = mBool;
Log.e("Update pref", b.toString());
tgButton = (ToggleButton)findViewById(R.id.toggleButton);
tgButton.setChecked(mBool);
System.out.println("*****tgButton.setChecked(mBool1);****");
final boolean mBool1 = servicePrefs.getBoolean("myservice", false);
Log.e("Boolean Value mBool","="+mBool1);
final Boolean c = mBool1;
Log.e("Update pref", c.toString());
tgButton=(ToggleButton)findViewById(R.id.toggleButton);
tgButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
if(tgButton.isChecked())
{
startService(new Intent(Toggle_Activity.this , MyService.class));
System.out.println("Service is started in togglr button");
}
else
{
stopService(new Intent(Toggle_Activity.this,MyService.class));
Intent intent=new Intent(Toggle_Activity.this,Service_Demo.class);
status=false;
intent.putExtra("toggleBtn",status);
startActivity(intent);
}
}
});
}
}
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.toggle_button/com.toggle_button.Service_Demo}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-15 12:44:32.154: E/AndroidRuntime(793): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-15 12:44:32.154: E/AndroidRuntime(793): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-15 12:44:32.154: E/AndroidRuntime(793): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-15 12:44:32.154: E/AndroidRuntime(793): at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 12:44:32.154: E/AndroidRuntime(793): at android.os.Looper.loop(Looper.java:123)
05-15 12:44:32.154: E/AndroidRuntime(793): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-15 12:44:32.154: E/AndroidRuntime(793): at java.lang.reflect.Method.invokeNative(Native Method)
05-15 12:44:32.154: E/AndroidRuntime(793): at java.lang.reflect.Method.invoke(Method.java:507)
05-15 12:44:32.154: E/AndroidRuntime(793): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-15 12:44:32.154: E/AndroidRuntime(793): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-15 12:44:32.154: E/AndroidRuntime(793): at dalvik.system.NativeStart.main(Native Method)
05-15 12:44:32.154: E/AndroidRuntime(793): Caused by: java.lang.NullPointerException
05-15 12:44:32.154: E/AndroidRuntime(793): at com.toggle_button.Service_Demo.onCreate(Service_Demo.java:43)
05-15 12:44:32.154: E/AndroidRuntime(793): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-15 12:44:32.154: E/AndroidRuntime(793): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-15 12:44:32.154: E/AndroidRuntime(793): ... 11 more
最佳答案
第一次活动:
String togvalue = togglebtn.gettext().tostring();
Intent i = new Intent(first.this, sec.class);
i.putextra("togvalue", togvalue);
startactivity(i);
第二项活动:
String togvalue = getIntent().getStringextra("togvalue");
关于java - 如何在Android中的上一个 Activity 中获取切换按钮值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16558740/