1

我有以下代码,我想在 JSON.getJSONfromURL() 方法中传递附加参数,但是当我检查 android 时它会抛出这个异常

04-11 11:58:18.929: E/AndroidRuntime(566): 致命异常: main 04-11 11:58:18.929: E/AndroidRuntime(566): java.lang.RuntimeException: 无法启动活动 ComponentInfo{cyclist.项目/cyclist.project.user_discussion}:java.lang.NullPointerException 04-11 11:58:18.929:E/AndroidRuntime(566):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 04-11 11: 58:18.929: E/AndroidRuntime(566): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 04-11 11:58:18.929: E/AndroidRuntime(566): 在 android.app.ActivityThread.access $2300(ActivityThread.java:125) 04-11 11:58:18.929: E/AndroidRuntime(566): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 04-11 11:58:18.929: E/AndroidRuntime(566): 在 android.os.Handler.dispatchMessage(Handler.java:99) 04-11 11:58:18.929: E/AndroidRuntime(566): 在 android.os.Looper.loop(Looper.java:123) 04-11 11:58:18.929: E/AndroidRuntime(566): 在android.app.ActivityThread.main(ActivityThread.java:4627) 04-11 11:58:18.929: E/AndroidRuntime(566): 在 java.lang.reflect.Method.invokeNative(Native Method) 04-11 11:58 :18.929: E/AndroidRuntime(566): at java.lang.reflect.Method.invoke(Method.java:521) 04-11 11:58:18.929: E/AndroidRuntime(566): at com.android.internal。 os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 04-11 11:58:18.929: E/AndroidRuntime(566): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 04 -11 11:58:18.929: E/AndroidRuntime(566): at dalvik.system.NativeStart.main(Native Method) 04-11 11:58:18.929: E/AndroidRuntime(566): Caused by: java.lang. NullPointerException 04-11 11:58:18。929: E/AndroidRuntime(566): 在 cyclist.project.JSON.getJSONfromURL(JSON.java:60) 04-11 11:58:18.929: E/AndroidRuntime(566): 在 cyclist.project.user_discussion.setList(user_discussion .java:250) 04-11 11:58:18.929: E/AndroidRuntime(566): 在 cyclist.project.user_discussion.onCreate(user_discussion.java:154) 04-11 11:58:18.929: E/AndroidRuntime(566 ): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 04-11 11:58:18.929: E/AndroidRuntime(566): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 04- 11 11:58:18.929: E/AndroidRuntime(566): ... 11 更多929: E/AndroidRuntime(566): 在 cyclist.project.user_discussion.onCreate(user_discussion.java:154) 04-11 11:58:18.929: E/AndroidRuntime(566): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation .java:1047) 04-11 11:58:18.929: E/AndroidRuntime(566): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 04-11 11:58:18.929: E/AndroidRuntime(566 ): ... 11 更多929: E/AndroidRuntime(566): 在 cyclist.project.user_discussion.onCreate(user_discussion.java:154) 04-11 11:58:18.929: E/AndroidRuntime(566): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation .java:1047) 04-11 11:58:18.929: E/AndroidRuntime(566): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 04-11 11:58:18.929: E/AndroidRuntime(566 ): ... 11 更多

代码:

    JSONObject json;

    try {
        json = new JSONObject(
        JSON.getJSONfromURL("http://myweb.com/ws_cyc/events_service.php?action=get_chats&title=fsfbnn" ));
    JSONArray miles = json.getJSONArray("cyc");
    JSONObject rec;
    JSONObject rec2 = null; 

    for (int i=0; i<miles.length(); i++){
                rec = miles.getJSONObject(i);
                rec2 = rec.getJSONObject("cycObject");
                new discussion_item(rec2.getString("Chats").toString());
                ImgList.add( rec2.getString("image_path").toString() );
                list.add( rec2.getString("email").toString() + ": " + rec2.getString("Chats").toString() );
            }                                   
    } catch (JSONException e) {
            e.printStackTrace();
        }   

请指导我如何在 JSON.getJSONfromURL() 中传递其他参数以及 php 将如何获取它,我要捕获 jason 的 php 代码是

$titlez = json_decode($_GET['title']);
4

2 回答 2

1

最好使用 Gson 来获取 JSON 数据,它既快速又高效。

于 2012-04-11T07:09:42.077 回答
0

正如错误日志所说,它是由 null 值被传递并试图在文件的第 154 行处理引起的user_discussion.java

于 2012-04-11T08:49:17.697 回答