Json异常导致android设备崩溃

时间:2016-03-21 12:58:34

标签: android json

我有一个jsonarray,它会检查一个值worksheetID,然后存储并稍后在变量中使用。问题是,当这个值不存在时,android设备会崩溃,即使我通过try catch块运行它也不会阻止它崩溃。

这是我的代码:

try {
    try
    {
        jsonArray = jsonObject.getJSONArray("WorksheetID");
        Log.v("Worked", "WorksheetID is in ");

    } catch (JSONException e)
    {

       Log.v("failed", "No WorksheetID");
        Log.v("failed", WorksheetID[0]+"");
        e.printStackTrace();


    }

    if(WorksheetID[0] != 0)
    {
        WorksheetID = new int[jsonArray.length()];
        for (int i = 0; i < jsonArray.length(); i++) {

            try {
                WorksheetID[i] = jsonArray.getInt(i);
            } catch (JSONException e)
            {
                e.printStackTrace();
                Log.v("failed", "cant do loop 2");
                WorksheetID[0] = 0;

            }
        }
    }
    else
    {
        return;
    }
}catch (Exception e)
{
    WorksheetID[0] = 0;

}

这是我得到的logcat错误。由于某种原因,机器似乎在第一次捕获后关闭,甚至没有执行捕获本身的所有线路!

    03-21 08:55:42.494  21224-21224/net.azurewebsites.cosy W/System.err﹕ org.json.JSONException: No value for WorksheetID
03-21 08:55:42.494  21224-21224/net.azurewebsites.cosy W/System.err﹕ at org.json.JSONObject.get(JSONObject.java:355)
03-21 08:55:42.494  21224-21224/net.azurewebsites.cosy W/System.err﹕ at org.json.JSONObject.getJSONArray(JSONObject.java:549)
03-21 08:55:42.498  21224-21224/net.azurewebsites.cosy W/System.err﹕ at net.azurewebsites.cosy.Classroom.onCreate(Classroom.java:127)
03-21 08:55:42.498  21224-21224/net.azurewebsites.cosy W/System.err﹕ at android.app.Activity.performCreate(Activity.java:5231)
03-21 08:55:42.498  21224-21224/net.azurewebsites.cosy W/System.err﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-21 08:55:42.498  21224-21224/net.azurewebsites.cosy W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
03-21 08:55:42.498  21224-21224/net.azurewebsites.cosy W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
03-21 08:55:42.498  21224-21224/net.azurewebsites.cosy W/System.err﹕ at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-21 08:55:42.498  21224-21224/net.azurewebsites.cosy W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-21 08:55:42.498  21224-21224/net.azurewebsites.cosy W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
03-21 08:55:42.498  21224-21224/net.azurewebsites.cosy W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
03-21 08:55:42.498  21224-21224/net.azurewebsites.cosy W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5001)
03-21 08:55:42.498  21224-21224/net.azurewebsites.cosy W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
03-21 08:55:42.498  21224-21224/net.azurewebsites.cosy W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
03-21 08:55:42.498  21224-21224/net.azurewebsites.cosy W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
03-21 08:55:42.498  21224-21224/net.azurewebsites.cosy W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
03-21 08:55:42.498  21224-21224/net.azurewebsites.cosy W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
03-21 08:55:42.498  21224-21224/net.azurewebsites.cosy V/failed﹕ No WorksheetID
03-21 08:55:42.506  21224-21224/net.azurewebsites.cosy D/AndroidRuntime﹕ Shutting down VM
03-21 08:55:42.506  21224-21224/net.azurewebsites.cosy W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4d59b20)
03-21 08:55:42.510  21224-21224/net.azurewebsites.cosy E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: net.azurewebsites.cosy, PID: 21224
    java.lang.RuntimeException: Unable to start activity ComponentInfo{net.azurewebsites.cosy/net.azurewebsites.cosy.Classroom}: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
            at net.azurewebsites.cosy.Classroom.onCreate(Classroom.java:163)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)

4 个答案:

答案 0 :(得分:0)

当WorksheetID为null时,你得到一个ArrayIndexOut ArrayIndexOutOfBoundsException,你可能在Catch-block中对数组做错了。

可能的罪魁祸首是这一行:

  

Log.v(“失败”,WorksheetID [0] +“”);

看起来数组是空的或未初始化,并且您正在尝试访问第一个元素(它是空的),因此您得到一个ArrayIndexOutOfBoundsException。

确保在尝试访问WorksheetID之前初始化它。

答案 1 :(得分:0)

更改

if(WorksheetID[0] != 0)

if(WorksheetID.length > 0 && WorksheetID[0] != 0)

答案 2 :(得分:0)

你的代码中有很多错误,而且不清楚你实际在做什么。

试试这个

 try
    {

      if(jsonObject.has("WorksheetID")){

      jsonArray = jsonObject.getJSONArray("WorksheetID");


       int[] WorksheetID = new int[jsonArray.length()];
        for (int i = 0; i < jsonArray.length(); i++) {

            try {
                WorksheetID[i] = jsonArray.getInt(i);
            } catch (JSONException e)
            {
                e.printStackTrace();
                Log.v("failed", "cant do loop 2");

            }
        }


        }else{
           Log.v("NoWorksheet", "No WorksheetID  available");
        }


    } catch (JSONException e)
    {

       Log.v("failed", "No WorksheetID");
        e.printStackTrace();


    }

答案 3 :(得分:0)

您收到的是ArrayIndexOutOfBoundsException,而不是JSONException,这就是崩溃的原因,因为它无法捕获异常类型。即使您使用了另一个捕获一般异常错误的try-catch语句,由于第二个try-catch语句发生错误,它也无法访问该部分。如果您希望自己的应用不崩溃,请在第二个捕获语句中将JSONException更改为Exception