我有这个功能:
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