我想将数据从活动 A 传递到服务 B,即userName
和phoneNumber
。在服务 B 中,仅使用phoneNumber
(这里电话号码很重要)并randomNumber
生成 a。从服务 B 传递到活动 C 的数据是userName
,phoneNumber
和randomNumber
。
我正在尝试使用下面给出的这段代码,但它不起作用。
在活动 A 中:
Intent goToServiceB = new Intent(getApplicationContext(),ServiceB.class);
goToServiceB.putExtra("passedPhoneNumberAtoS", phoneNumberStr);
goToServiceB.putExtra("passedUserNameAtoS", userNameStr);
startService(goToServiceB);
在服务 B 中:
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
String phoneNumber = intent.getStringExtra("passedPhoneNumberAtoS");
String userName = intent.getStringExtra("passedUserNameAtoS");
return Service.START_NOT_STICKY;
}
在服务 B 本身中还有另一个意图:
Intent goToActivityC = new Intent(getApplicationContext(),ActivityC.class);
goToActivityC.putExtra("passedPhoneNumberStoA", phoneNumberStr);
goToActivityC.putExtra("passedUserNameStoA", userNameStr);
goToActivityC.putExtra("passedRandomNumberStoA", RandomNumberStr);
startactivity(goToActivityC);
在活动 C 中:
Intent intent=new Intent();
String phoneNumber = intent.getStringExtra("passedPhoneNumberStoA");
String userName = intent.getStringExtra("passedUserNameStoA");
String RandomNumber=intent.getStringExtra("passedRandomNumberStoA");
我的日志猫:
08-03 14:19:42.882: E/Trace(1923): error opening trace file: No such file or directory (2)
08-03 14:19:44.983: D/gralloc_goldfish(1923): Emulator without GPU emulation detected.
08-03 14:19:45.153: W/IInputConnectionWrapper(1923): showStatusIcon on inactive InputConnection
08-03 14:19:47.792: D/dalvikvm(1923): GC_CONCURRENT freed 84K, 7% free 2765K/2972K, paused 79ms+46ms, total 236ms
08-03 14:20:03.792: D/dalvikvm(1923): GC_FOR_ALLOC freed 54K, 6% free 2931K/3104K, paused 36ms, total 57ms
08-03 14:20:03.812: I/dalvikvm-heap(1923): Grow heap (frag case) to 4.050MB for 1127536-byte allocation
08-03 14:20:03.964: D/dalvikvm(1923): GC_FOR_ALLOC freed 50K, 6% free 3982K/4208K, paused 152ms, total 152ms
08-03 14:20:04.113: D/dalvikvm(1923): GC_CONCURRENT freed 1K, 6% free 3987K/4208K, paused 4ms+97ms, total 154ms
08-03 14:20:04.202: D/AndroidRuntime(1923): Shutting down VM
08-03 14:20:04.202: W/dalvikvm(1923): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
08-03 14:20:04.232: E/AndroidRuntime(1923): FATAL EXCEPTION: main
08-03 14:20:04.232: E/AndroidRuntime(1923): java.lang.IllegalStateException: Could not execute method of the activity
08-03 14:20:04.232: E/AndroidRuntime(1923): at android.view.View$1.onClick(View.java:3599)
08-03 14:20:04.232: E/AndroidRuntime(1923): at android.view.View.performClick(View.java:4204)
08-03 14:20:04.232: E/AndroidRuntime(1923): at android.view.View$PerformClick.run(View.java:17355)
08-03 14:20:04.232: E/AndroidRuntime(1923): at android.os.Handler.handleCallback(Handler.java:725)
08-03 14:20:04.232: E/AndroidRuntime(1923): at android.os.Handler.dispatchMessage(Handler.java:92)
08-03 14:20:04.232: E/AndroidRuntime(1923): at android.os.Looper.loop(Looper.java:137)
08-03 14:20:04.232: E/AndroidRuntime(1923): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-03 14:20:04.232: E/AndroidRuntime(1923): at java.lang.reflect.Method.invokeNative(Native Method)
08-03 14:20:04.232: E/AndroidRuntime(1923): at java.lang.reflect.Method.invoke(Method.java:511)
08-03 14:20:04.232: E/AndroidRuntime(1923): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-03 14:20:04.232: E/AndroidRuntime(1923): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-03 14:20:04.232: E/AndroidRuntime(1923): at dalvik.system.NativeStart.main(Native Method)
08-03 14:20:04.232: E/AndroidRuntime(1923): Caused by: java.lang.reflect.InvocationTargetException
08-03 14:20:04.232: E/AndroidRuntime(1923): at java.lang.reflect.Method.invokeNative(Native Method)
08-03 14:20:04.232: E/AndroidRuntime(1923): at java.lang.reflect.Method.invoke(Method.java:511)
08-03 14:20:04.232: E/AndroidRuntime(1923): at android.view.View$1.onClick(View.java:3594)
08-03 14:20:04.232: E/AndroidRuntime(1923): ... 11 more
08-03 14:20:04.232: E/AndroidRuntime(1923): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
08-03 14:20:04.232: E/AndroidRuntime(1923): at android.view.ViewRootImpl.setView(ViewRootImpl.java:571)
08-03 14:20:04.232: E/AndroidRuntime(1923): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246)
08-03 14:20:04.232: E/AndroidRuntime(1923): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
08-03 14:20:04.232: E/AndroidRuntime(1923): at android.app.Dialog.show(Dialog.java:281)
08-03 14:20:04.232: E/AndroidRuntime(1923): at com.beproject.groupmessenger.UserRegistration.showAlert(UserRegistration.java:91)
08-03 14:20:04.232: E/AndroidRuntime(1923): at com.beproject.groupmessenger.UserRegistration.submit(UserRegistration.java:97)
08-03 14:20:04.232: E/AndroidRuntime(1923): ... 14 more
有人可以为上述问题提供一个简单的解决方案吗?我进行了很多搜索,但无法获得可以理解的解决方案。