0

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

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

在活动 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

有人可以为上述问题提供一个简单的解决方案吗?我进行了很多搜索,但无法获得可以理解的解决方案。

4

2 回答 2

0

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

您应该有两个服务类私有的字符串变量,并将附加内容提取到它们。

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

您必须像public void submit(View view);在 UserRegistration 中那样实现此方法才能调用它。Android 无法检测到这种缺失的方法(或.mispelled 或...),因为它在运行时使用 Java 反射。

于 2013-10-07T16:02:57.090 回答
0

java.lang.IllegalStateException:在活动类 com.beproject.groupmessenger.UserRegistration 中找不到方法 submit(View) 用于视图类 android.widget.Button 上 ID 为“注册”的 onClick 处理程序

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

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

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

于 2013-10-07T16:16:10.083 回答