为什么我的Android应用程序在将JSON对象发送到另一个活动时崩溃了?

时间:2016-03-09 09:56:40

标签: java android

我正在尝试将JSON字符串发送到我的第二个活动onButtonClick,但是一旦我这样做我的应用程序崩溃,当我删除:startNewActivity.putExtra("jsondata", str);行它运行良好,为什么?我该如何解决呢?

    public void onButtonClick(View view){
    Intent startNewActivity = new Intent(this, Activity2.class);
    try {
        String str = obj.toString();
        startNewActivity.putExtra("jsondata", str);
        startActivity(startNewActivity);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

编辑: 这是日志:

03-09 12:29:27.325 23927-24014/com.example.elinaim.myapplication D/dalvikvm: GC_FOR_ALLOC freed 6310K, 28% free 18043K/24972K, paused 17ms, total 17ms
03-09 12:29:27.965 23927-23927/com.example.elinaim.myapplication D/dalvikvm: GC_FOR_ALLOC freed 1976K, 28% free 18115K/24972K, paused 12ms, total 13ms03-09 12:29:29.855 23927-23927/com.example.elinaim.myapplication D/dalvikvm: GC_FOR_ALLOC freed 642K, 22% free 19520K/24972K, paused 19ms, total 19ms
03-09 12:29:31.805 23927-23927/com.example.elinaim.myapplication D/dalvikvm: GC_FOR_ALLOC freed 666K, 17% free 20902K/24972K, paused 26ms, total 26ms
03-09 12:29:31.825 23927-23927/com.example.elinaim.myapplication I/Choreographer: Skipped 886 frames!  The application may be doing too much work on its main thread.
03-09 12:29:31.895 23927-23927/com.example.elinaim.myapplication I/Adreno-EGL: <qeglDrvAPI_eglInitialize:385>: EGL 1.4 QUALCOMM build:  ()
                                                                          OpenGL ES Shader Compiler Version: E031.24.00.02
                                                                           Build Date: 01/20/14 Mon
                                                                           Local Branch: PMH2-KK_3.5-RB1-AU61-554722-586267-set2
                                                                           Remote Branch: 
                                                                           Local Patches: 
                                                                           Reconstruct Branch: 
03-09 12:29:31.915 23927-23927/com.example.elinaim.myapplication D/OpenGLRenderer: Enabling debug mode 0
03-09 12:29:32.195 23927-23927/com.example.elinaim.myapplication I/ActivityManager: Timeline: Activity_idle id: android.os.BinderProxy@42838680 time:11069000
03-09 12:29:40.745 23927-23927/com.example.elinaim.myapplication I/ViewRootImpl: ViewRoot's Touch Event : Touch Down
03-09 12:29:40.785 23927-23927/com.example.elinaim.myapplication I/ViewRootImpl: ViewRoot's Touch Event : Touch UP
03-09 12:29:44.605 23927-23927/com.example.elinaim.myapplication D/dalvikvm: GC_FOR_ALLOC freed 1099K, 15% free 21445K/24972K, paused 27ms, total 28ms
03-09 12:29:46.805 23927-23927/com.example.elinaim.myapplication D/dalvikvm: GC_FOR_ALLOC freed 1187K, 12% free 22002K/24972K, paused 28ms, total 28ms
03-09 12:30:08.835 23927-23927/com.example.elinaim.myapplication D/dalvikvm: GC_FOR_ALLOC freed 1660K, 12% free 22056K/24972K, paused 27ms, total 27ms
03-09 12:30:12.705 23927-23927/com.example.elinaim.myapplication D/dalvikvm: GC_FOR_ALLOC freed 848K, 11% free 22416K/24972K, paused 26ms, total 26ms
03-09 12:30:13.795 23927-23927/com.example.elinaim.myapplication D/dalvikvm: GC_FOR_ALLOC freed 679K, 9% free 22739K/24972K, paused 28ms, total 28ms
03-09 12:30:13.805 23927-23927/com.example.elinaim.myapplication I/ActivityManager: Timeline: Activity_launch_request id:com.example.elinaim.myapplication time:11110611
03-09 12:56:25.825 3576-3576/com.example.elinaim.myapplication I/Choreographer: Skipped 892 frames!  The application may be doing too much work on its main thread.

在此之后我的应用程序崩溃,没有其他信息...... 我几乎肯定它是因为JSON字符串的大小

2 个答案:

答案 0 :(得分:0)

尝试以下代码:

活动1:

     Button b = (Button) findViewById(R.id.button);
     b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            Intent startNewActivity = new Intent(MainActivity.this, SecondActivity.class);
                try {
                     String str = obj.toString();
                     //json.isNull( "field-name" ).
                    //String str = "IcebreakerSandy";
                    startNewActivity.putExtra("jsondata", str);
                    startActivity(startNewActivity);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

活动2:

 Bundle extras = getIntent().getExtras();
    String value;
    if (extras != null) {
        value = extras.getString("jsondata");
        Log.e("Data","is: "+value);
    }

OR 否则你可以使用这个简单的方法来检查JSNO对象是否为空。

返回给定键映射的值,如果不存在则返回{@code null}或null。

public static String optString(JSONObject json, String key)
{
    // http://code.google.com/p/android/issues/detail?id=13830
    if (json.isNull(key))
        return null;
    else
        return json.optString(key, null);
}

检查此答案@Jacob Phillips,检查json中是否存在对象 Check if an object exists in JSON

如果有帮助,请投票。

答案 1 :(得分:0)

您需要检查null的{​​{1}}值。

String

Activity2 onCreate()中:

try {
     String str = obj.toString();
     if(str!=null){
      startNewActivity.putExtra("jsondata", str);
     }
     startActivity(startNewActivity);
    } catch (Exception e) {
       e.printStackTrace();
    }

希望这会对你有所帮助。