将数据从活动A传递到服务B到活动C.

时间:2013-10-07 15:49:51

标签: android android-intent android-activity android-service

我想将数据从活动A传递到服务B,即userNamephoneNumber。 在服务B中,仅使用phoneNumber(此处电话号码很重要)并生成randomNumber。从服务B传递到活动C的数据为userNamephoneNumberrandomNumber

我正在尝试使用下面给出的代码,但它不起作用。

在活动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");

我的logcat:

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

有人可以给出上述问题的简单解决方案吗?我搜索了很多,但无法得到一个可以理解的解决方案。

2 个答案:

答案 0 :(得分:0)

在服务B中,您可以获得额外内容并将其存储在本地字符串变量中。从该服务中的onStartCommand()返回后,这些变量消失了。因此,无论您传递给活动C的是什么,都不是您从传递给ServiceB.onStartCommand()

的意图中得到的

您应该将两个String变量专用于Service Class,并将其添加到它们中。

编辑:现在使用logcat,错误变得更加清晰。您在布局中定义了一个id为“register”的按钮。对于此按钮,您可以使用android:onClick定义在活动submit()中调用方法UserRegistration。此方法不存在。

您必须在UserRegistration中将此方法实现为public void submit(View view);才能调用它。 Android无法检测此缺失方法(或.misspelled或...),因为它在运行时使用了Reflection of Java。

答案 1 :(得分:0)

  

java.lang.IllegalStateException:无法在活动类com.beproject.groupmessenger.UserRegistration中找到方法提交(View),用于视图类android.widget.Button上的onClick处理程序,其id为“register”

logcat中的错误表明问题与您的数据传递代码无关。

此错误表示您的android:id="@+id\register"活动的XML中定义了一个UserRegistration按钮,该活动的点击处理程序设置为android:onclick="submit"。但是,Android无法在submit(View)活动中找到UserRegistration方法,因此会因此错误而崩溃。

确保UserRegistration活动具有公共submit(View)方法,并且应用程序不应再崩溃。