0

我有这个功能:

public static String getXML(int id, int choose, int foo){    
        String line = null;
        try {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = null;
            if(id == 100)
            {
                httpPost = new HttpPost("http://andronekat.com/sawalef/logFollow.php?id="+ foo + "");
            }
            else if(id == 20)
            {
                httpPost = new HttpPost("http://andronekat.com/sawalef/logMyTimeLine.php?id="+ foo + "");
            }
            else if(id == 19)
            {
                httpPost = new HttpPost("http://andronekat.com/sawalef/logMyEntity.php?id="+ foo + "");
            }
            else if(choose == 1)
            {
                httpPost = new HttpPost("http://andronekat.com/sawalef/log.php?id="+ id + "");
            }
            else if(choose == 2)
                httpPost = new HttpPost("http://andronekat.com/sawalef/log2.php?id="+ id + "");
            else if(choose == 3)
                httpPost = new HttpPost("http://andronekat.com/sawalef/comments.php?tag="+ id + "");
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            line = EntityUtils.toString(httpEntity, "UTF-8");

        }

除了第一个 ULR 之外,所有字符串 URL 都可以正常工作:

    if(id == 100)
    {
        httpPost = new HttpPost("http://andronekat.com/sawalef/logFollow.php?id="+ foo + "");
    }

它在这一行给了我 java.lang.NullPointerException

HttpResponse httpResponse = httpClient.execute(httpPost);

我不知道我从哪里得到空值。如果我把它放在浏览器中,链接就可以正常工作。任何人都可以帮助我吗?

日志猫:

04-28 09:37:34.500: E/AndroidRuntime(21842): FATAL EXCEPTION: main
04-28 09:37:34.500: E/AndroidRuntime(21842): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.softshell.sowalef/com.softshell.sowalef.Follow}: java.lang.NullPointerException
04-28 09:37:34.500: E/AndroidRuntime(21842):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
04-28 09:37:34.500: E/AndroidRuntime(21842):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
04-28 09:37:34.500: E/AndroidRuntime(21842):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
04-28 09:37:34.500: E/AndroidRuntime(21842):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
04-28 09:37:34.500: E/AndroidRuntime(21842):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-28 09:37:34.500: E/AndroidRuntime(21842):    at android.os.Looper.loop(Looper.java:137)
04-28 09:37:34.500: E/AndroidRuntime(21842):    at android.app.ActivityThread.main(ActivityThread.java:4898)
04-28 09:37:34.500: E/AndroidRuntime(21842):    at java.lang.reflect.Method.invokeNative(Native Method)
04-28 09:37:34.500: E/AndroidRuntime(21842):    at java.lang.reflect.Method.invoke(Method.java:511)
04-28 09:37:34.500: E/AndroidRuntime(21842):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
04-28 09:37:34.500: E/AndroidRuntime(21842):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
04-28 09:37:34.500: E/AndroidRuntime(21842):    at dalvik.system.NativeStart.main(Native Method)
04-28 09:37:34.500: E/AndroidRuntime(21842): Caused by: java.lang.NullPointerException
04-28 09:37:34.500: E/AndroidRuntime(21842):    at com.softshell.sowalef.Follow.onCreate(Follow.java:71)
04-28 09:37:34.500: E/AndroidRuntime(21842):    at android.app.Activity.performCreate(Activity.java:5206)
04-28 09:37:34.500: E/AndroidRuntime(21842):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
04-28 09:37:34.500: E/AndroidRuntime(21842):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
04-28 09:37:34.500: E/AndroidRuntime(21842):    ... 11 more
4

1 回答 1

4

确定在失败的情况id下设置为 100 吗?

即使你是,你的代码中也有一个应该修复的漏洞。具体来说,idis not 100, 20nor19chooseis not 1, 2nor的情况3

而且,如果这不是您的具体问题(我保证它仍然应该得到修复),您还应该在通话之前httpPosthttpClient之前检查您的值,execute以便您可以更轻松地找出问题所在。

于 2013-04-28T06:21:40.237 回答