gpt4 book ai didi

java - 为什么 Android 中不缓存 JSON 异常?

转载 作者:行者123 更新时间:2023-12-01 17:38:02 24 4
gpt4 key购买 nike

我有以下代码,但有时我有以下未捕获的异常..原因和解决方案是什么?

12-02 12:03:29.101: ERROR/AndroidRuntime(348): FATAL EXCEPTION: main
12-02 12:03:29.101: ERROR/AndroidRuntime(348): java.lang.RuntimeException: Unable to start activity ComponentInfo{ayanoo.forms/ayanoo.forms.FriendsActivity}: java.lang.NullPointerException
12-02 12:03:29.101: ERROR/AndroidRuntime(348): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
12-02 12:03:29.101: ERROR/AndroidRuntime(348): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-02 12:03:29.101: ERROR/AndroidRuntime(348): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-02 12:03:29.101: ERROR/AndroidRuntime(348): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-02 12:03:29.101: ERROR/AndroidRuntime(348): at android.os.Handler.dispatchMessage(Handler.java:99)
12-02 12:03:29.101: ERROR/AndroidRuntime(348): at android.os.Looper.loop(Looper.java:123)
12-02 12:03:29.101: ERROR/AndroidRuntime(348): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-02 12:03:29.101: ERROR/AndroidRuntime(348): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 12:03:29.101: ERROR/AndroidRuntime(348): at java.lang.reflect.Method.invoke(Method.java:521)
12-02 12:03:29.101: ERROR/AndroidRuntime(348): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-02 12:03:29.101: ERROR/AndroidRuntime(348): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-02 12:03:29.101: ERROR/AndroidRuntime(348): at dalvik.system.NativeStart.main(Native Method)
12-02 12:03:29.101: ERROR/AndroidRuntime(348): Caused by: java.lang.NullPointerException
12-02 12:03:29.101: ERROR/AndroidRuntime(348): at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:112)
12-02 12:03:29.101: ERROR/AndroidRuntime(348): at org.json.JSONTokener.nextValue(JSONTokener.java:90)
12-02 12:03:29.101: ERROR/AndroidRuntime(348): at org.json.JSONObject.<init>(JSONObject.java:154)
12-02 12:03:29.101: ERROR/AndroidRuntime(348): at org.json.JSONObject.<init>(JSONObject.java:171)
12-02 12:03:29.101: ERROR/AndroidRuntime(348): at ayanoo.forms.FriendsActivity.displayFriends(FriendsActivity.java:79)
12-02 12:03:29.101: ERROR/AndroidRuntime(348): at ayanoo.forms.FriendsActivity.onCreate(FriendsActivity.java:36)
12-02 12:03:29.101: ERROR/AndroidRuntime(348): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-02 12:03:29.101: ERROR/AndroidRuntime(348): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
12-02 12:03:29.101: ERROR/AndroidRuntime(348): ... 11 more





try {
// String searchText = (String) getIntent().getExtras().get("searchTextValue");
JSONObject jsonObject = new JSONObject(Friend.getFriends(1));
if(jsonObject!=null)
{
JSONArray resultsData = jsonObject.getJSONArray("GetFriendsResult");
for (int userIndex = 0; userIndex < resultsData.length(); userIndex++) {
if (friendsResulted == null) {
friendsResulted = new ArrayList<FriendResulted>(resultsData.length());
}
String[] userArrayData = resultsData.getString(userIndex).split("--!!--");
FriendResulted frindAccountResulted = new FriendResulted();

frindAccountResulted.setFriendId(Integer.parseInt(userArrayData[0]));
frindAccountResulted.setUsername(userArrayData[1]);
frindAccountResulted.setImagePath(userArrayData[2]);
friendsResulted.add(frindAccountResulted);
addFriend(frindAccountResulted);
counter ++;
if(counter>9)break;
}


}
}

catch (JSONException e) {
Toast.makeText(this,"Please try again, "+e.getMessage() , Toast.LENGTH_SHORT).show();
}

最佳答案

这是一个NullPointerException,而不是一个JSONEcxeption。我认为这是由这一行引起的:

JSONObject jsonObject = new JSONObject(Friend.getFriends(1));

看起来 Friend.getFriends(1) 返回 null。你必须改变这一点。

关于java - 为什么 Android 中不缓存 JSON 异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4291572/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com