我已经在后台系统和两个Activities中创建了一个运行服务的应用程序。我在Activity1中启动了服务,在Activity2中我已经设置了一个带有“是”和“否”的切换按钮。当我运行应用程序服务时,在activity1中启动,当从activity1打开activity2时,切换按钮状态使用共享首选项自动“打开”服务状态,但当我按下切换按钮“关闭”以停止服务时,我得到了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
答案 0 :(得分:1)
第一项活动:
String togvalue = togglebtn.gettext().tostring();
Intent i = new Intent(first.this, sec.class);
i.putextra("togvalue", togvalue);
startactivity(i);
第二项活动:
String togvalue = getIntent().getStringextra("togvalue");