2013-10-07 82 views
0

我想要將數據從活動A傳遞到服務B,這是userNamephoneNumber。 僅在服務B中使用phoneNumber(此處電話號碼很重要),並生成randomNumber。從服務B傳遞給活動C的數據是userName,phoneNumberrandomNumber將活動A到服務B的數據傳遞到活動C

我想使用下面給出的代碼,但它不起作用。

在活性的:

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 

可有人請給一個簡單的解決上述問題?我搜查了很多,但無法獲得可理解的解決方案。

+0

「不起作用」是什麼意思?數據在哪裏丟失? –

+0

我的AVD在第一次活動之後顯示強制關閉錯誤 – VJP

+0

logcat說什麼? –

回答

0

在服務B中,您獲得臨時演員並將其存儲在本地字符串變量中。從該服務中的onStartCommand()返回後,這些變量消失。所以,無論你傳遞給Activity C的是不是你從Intent傳遞到ServiceB.onStartCommand()

你應該有兩個字符串變量專用於服務類並提取額外信息給他們。

編輯:現在與logcat錯誤變得更清晰。您在佈局中定義了一個ID爲'register'的按鈕。對於此按鈕,您可以使用來定義在活動UserRegistration中調用方法submit()。此方法不存在。

您必須在UserRegistration中將此方法實現爲public void submit(View view);才能使其調用。由於Android在運行時使用了Reflection的Java,所以Android無法檢測到這個缺失的方法(or.misspelled或...)。

+0

甚至在將變量聲明爲私有服務類 – VJP

+0

後也出現同樣的錯誤我看到logcat和第一個錯誤。按照建議更改酸碼,再次檢查logcat。如果這個錯誤消失了,你可以檢查下一個。 – jboi

0

java.lang.IllegalStateException:找不到方法提交(查看)在活動課com.beproject.groupmessenger.UserRegistration爲的onClick處理程序上的視圖類android.widget.Button ID爲「註冊」

來自logcat的錯誤表明問題與您的數據傳遞代碼無關。

此錯誤表示您在UserRegistration活動的XML中定義了一個按鈕android:id="@+id\register",該活動的點擊處理程序也設置爲android:onclick="submit"。但是,Android在UserRegistration活動中找不到submit(View)方法,從而導致此錯誤崩潰。

確保UserRegistration活動有一個公共submit(View)方法,並且應用程序不應再崩潰。

+0

應用程序仍然崩潰,出現上述錯誤 – VJP

相關問題