无法将字符串发送到另一个活动

时间:2017-01-12 04:12:27

标签: android json

我想创建app可以将一个活动中的字符串(json)发送给另一个活动,我检查第一个活动,我认为正常,因为systemlog显示了json。但是,在第二项活动中,我无法看到json只是[]。我认为第二个活动不会检索数据,所以我该怎么办?

这是我的第一个活动:

String data = response.body().string();
System.out.println(data);
Intent kirim = new Intent(this, UploadImage.class);
kirim.putExtra(datakirim, data.toString());
startActivity(kirim);
finish();

而且,这第二个活动:

kirim = getIntent();
olahdata = kirim.getStringExtra(MainActivity.datakirim);
System.out.println(olahdata);

这是我的日志:

01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter I/System.out: [] 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err: org.json.JSONException: Index 0 out of range [0..0) 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err:  at org.json.JSONArray.get(JSONArray.java:282) 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err:  at org.json.JSONArray.getJSONObject(JSONArray.java:510) 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err:     at com.example.client18.androidwithcodeigniter.UploadImage.onCreate(UploadImage.java:78) 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err:  at android.app.Activity.performCreate(Activity.java:5336) 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090) 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err:  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err:  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err:  at android.app.ActivityThread.access$800(ActivityThread.java:135) 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err:  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err:  at android.os.Handler.dispatchMessage(Handler.java:102) 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err:     at android.os.Looper.loop(Looper.java:136) 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5021) 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method) 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err:  at java.lang.reflect.Method.invoke(Method.java:515) 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827) 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err:     at dalvik.system.NativeStart.main(Native Method) 01-12 10:58:57.057 17527-17527/com.example.client18.androidwithcodeigniter D/dalvikvm: GC_FOR_ALLOC freed 403K, 14% free 3254K/3744K, paused 15ms, total 15ms 01-12 10:58:57.097 17527-17527/com.example.client18.androidwithcodeigniter I/dalvikvm-heap: Grow heap (frag case) to 4.583MB for 1440012-byte allocation 01-12 10:58:57.127 17527-17535/com.example.client18.androidwithcodeigniter D/dalvikvm: GC_FOR_ALLOC freed 15K, 10% free 4645K/5152K, paused 35ms, total 35ms 01-12 10:58:57.147 2120-2120/com.bluestacks.home D/Home.AppLaunchReceiver: *************************Received app launch broadcast 01-12 10:58:57.157 2120-2120/com.bluestacks.home D/Home.AppLaunchReceiver: APP LAUNCH com.example.client18.androidwithcodeigniter 01-12 10:58:57.177 1742-2982/system_process V/WindowManager: isVisibleLw false for win : Window{4ea71844 u0 com.example.client18.androidwithcodeigniter/com.example.client18.androidwithcodeigniter.MainActivity EXITING} 01-12 10:58:57.187 1871-2943/com.android.inputmethod.latin I/LatinIME:LogUtils: Dictionary info: dictionary = contacts.en_US.dict ; version = ? ; date = ? 01-12 10:58:57.207 1934-18112/com.bluestacks.BstCommandProcessor D/dalvikvm: GC_FOR_ALLOC freed 2292K, 25% free 7623K/10076K, paused 48ms, total 48ms 01-12 10:58:57.227 1934-18112/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-httpd: command: ping 01-12 10:58:57.237 1934-18112/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-httpd: response: {"result":"ok"} 01-12 10:58:57.237 1742-2982/system_process D/dalvikvm: GC_FOR_ALLOC freed 1880K, 24% free 8356K/10988K, paused 44ms, total 44ms 01-12 10:58:57.247 1934-18115/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-httpd: command: ping 01-12 10:58:57.247 1934-18115/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-httpd: response: {"result":"ok"} 01-12 10:58:57.327 1934-18116/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-httpd: command: getdefaultlauncher 01-12 10:58:57.327 1934-18119/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-httpd: command: getlaunchactivityname com.example.client18.androidwithcodeigniter 01-12 10:58:57.327 1934-18119/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-httpd: response: {"value":"androidwithcodeigniter","result":"ok"} 01-12 10:58:57.337 1934-18116/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-httpd: response: {"defaultLauncher":"com.bluestacks.gamepophome","result":"ok"} 01-12 10:58:57.397 1934-18123/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-httpd: command: ping 01-12 10:58:57.397 1934-18123/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-httpd: response: {"result":"ok"} 01-12 10:58:57.407 1934-18124/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-httpd: command: getdefaultlauncher 01-12 10:58:57.407 1934-18124/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-httpd: response: {"defaultLauncher":"com.bluestacks.gamepophome","result":"ok"} 01-12 10:58:57.477 1742-10091/system_process D/InputMethodManagerService: ime_enabled = true is same as last value, no change 01-12 10:58:57.477 1742-10091/system_process D/InputMethodManagerService: ime_enabled = true is same as last value, no change 01-12 10:58:57.517 1742-1742/system_process D/NotificationService: In writeNotificationInfo: pkgName android 01-12 10:58:57.517 1934-1934/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-Service: startService called with arg: notification 01-12 10:58:57.517 1934-1934/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-Service: New Notification has been observed, informing frontend now 01-12 10:58:57.527 1934-1934/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-Application: in sendHttpRequest, request to send to (fqdn): http://10.0.2.2:2861/notification
                                           data: {"content":"Choose input method*bst*English (US) - Android Keyboard (AOSP)","id":"17040512","pkg":"android","tickerText":"android"} 01-12 10:58:57.547 1742-1742/system_process D/NotificationService: In writeNotificationInfo: pkgName android 01-12 10:58:57.557 1934-1934/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-Service: startService called with arg: notification 01-12 10:58:57.557 1934-1934/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-Service: New Notification has been observed, informing frontend now 01-12 10:58:57.557 1934-1934/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-Application: in sendHttpRequest, request to send to (fqdn): http://10.0.2.2:2861/notification
                                           data: {"content":"Choose input method*bst*English (US) - Android Keyboard (AOSP)","id":"17040512","pkg":"android","tickerText":"android"} 01-12 10:58:57.707 1742-1755/system_process V/WindowManager: isVisibleLw false for win : Window{4ea71844 u0 com.example.client18.androidwithcodeigniter/com.example.client18.androidwithcodeigniter.MainActivity EXITING} 01-12 10:58:57.717 1742-1755/system_process I/ActivityManager: Displayed com.example.client18.androidwithcodeigniter/.UploadImage: +776ms 01-12 10:58:57.737 1742-1755/system_process W/WindowManager: Rebuild removed 3 windows but added 2
     java.lang.RuntimeException: here
         at com.android.server.wm.WindowManagerService.rebuildAppWindowListLocked(WindowManagerService.java:8270)
         at com.android.server.wm.WindowManagerService.rebuildAppWindowListLocked(WindowManagerService.java:8206)
         at com.android.server.wm.WindowManagerService.handleAnimatingStoppedAndTransitionLocked(WindowManagerService.java:8950)
         at com.android.server.wm.WindowManagerService.performLayoutAndPlaceSurfacesLockedInner(WindowManagerService.java:9464)
         at com.android.server.wm.WindowManagerService.performLayoutAndPlaceSurfacesLockedLoop(WindowManagerService.java:8422)
         at com.android.server.wm.WindowManagerService.performLayoutAndPlaceSurfacesLocked(WindowManagerService.java:8364)
         at com.android.server.wm.WindowManagerService.access$400(WindowManagerService.java:168)
         at com.android.server.wm.WindowManagerService$H.handleMessage(WindowManagerService.java:7398)
         at android.os.Handler.dispatchMessage(Handler.java:102)
         at android.os.Looper.loop(Looper.java:136)
         at android.os.HandlerThread.run(HandlerThread.java:61)

4 个答案:

答案 0 :(得分:1)

在第二个活动中获取意图值:getIntent().getStringExtra("datakirim");

将意图值放入1ST活动:kirim.putExtra("datakirim",data);

答案 1 :(得分:1)

我认为您的问题在于您在第二项活动中使用的密钥。只需要在活动一和活动二的两个地方使用静态键值,而不是使用MainActivity.datakirim。它将解决您的问题。

问题:因为您正在完成第一项活动,所以无论您在设置和获取时使用的密钥都不一样。

如果你没有静止,那么调试你的应用程序并检查你从第一个活动传递的任何密钥,你在第二个活动中获得的是否相同。

你可以在android中使用Bundle来做同样的事情:

在ActivityOne中执行此操作以传递bundle -

        Intent intent = new Intent(ActivityOne.this, ActivityTwo.class);

        //Create the bundle
        Bundle bundle = new Bundle();

        //Add your data to bundle
        bundle.putString("String_Stuff_Key", "Your string value");
        //key value should be static 

        //Add the bundle to the intent
        intent.putExtras(bundle);

        //Fire that second activity
        startActivity(intent);

        //finish the current activity
        finish();

现在在ActivityTwo:

//Get the bundle
Bundle bundle = getIntent().getExtras();

//Extract the data…
String stuff = bundle.getString("String_Stuff_Key");

答案 2 :(得分:0)

这里有“完成()”你的第一个活动,这就是为什么你无法在第二个活动中获得“MainActivity.datakirim”的价值。

<强>解决方案:

1)。创建界面: AppConstants.java

public interface AppConstants {

    String EXTRA_DATA_KIRIM = "dataKirim";
}

2)。的 FirstActivity.java

String data = response.body().string();
System.out.println(data);
Intent kirim = new Intent(this, UploadImage.class);
kirim.putExtra(AppConstants.EXTRA_DATA_KIRIM, data.toString());
startActivity(kirim);
finish();

3)。的 UploadImage.java

String olahdata = getIntent().getStringExtra(AppConstants.EXTRA_DATA_KIRIM);
System.out.println(olahdata);

答案 3 :(得分:0)

从第一个活动代码中删除finish();然后试试。

这可能会有所帮助,因为调用finish();结束活动。