所以我正在制作应用程序,我需要应用程序从互联网 http://www.arleitiss.netne.net/DBindex.php?action=fetchTrans获取查询列表 并执行它们。我做到了,它在手机上测试时没有显示错误,但它在 AVD 上抛出了空指针。
package com.example.droid;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.content.Context;
import android.util.Log;
public class OnlineSync {
Context context;
public OnlineSync(Context context){
this.context = context;
}
public boolean AddUser(String username, String pass, String name, String city, String country, String age){
HttpClient http = new DefaultHttpClient();
HttpPost httpp = new HttpPost("http://www.arleitiss.netne.net/DBindex.php");
boolean result = false;
try{
List<NameValuePair> nvp = new ArrayList<NameValuePair>(6);
nvp.add(new BasicNameValuePair("action", "adduser"));
nvp.add(new BasicNameValuePair("username", username));
nvp.add(new BasicNameValuePair("password", pass));
nvp.add(new BasicNameValuePair("name", name));
nvp.add(new BasicNameValuePair("city", city));
nvp.add(new BasicNameValuePair("country", country));
nvp.add(new BasicNameValuePair("age", String.valueOf(age)));
httpp.setEntity(new UrlEncodedFormEntity(nvp));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = http.execute(httpp, responseHandler);
Log.d("RESPONSE", response);
if(response.toString().equals("ERROR1")){
result = true;
}
else if(response.toString().equals("ERROR-1")){
result = false;
}
}
catch(ClientProtocolException e){result = false;}
catch(IOException e){result = false;}
return result;
}
public String[] SyncTrans(){
BufferedReader in = null;
String data = null;
try{
HttpClient client = new DefaultHttpClient();
URI website = new URI("http://www.arleitiss.netne.net/DBindex.php?action=fetchTrans");
HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.seperator");
while(( l = in.readLine()) != null){
sb.append(l + nl);
}
in.close();
data = sb.toString();
String arr[] = data.split("<br>");
return arr;
}
catch(Exception e){}
return null;
}
public void Exec(){
DbCon dc = new DbCon(this.context);
String queries[] = SyncTrans();
for(int a = 0; a < queries.length-1; a++){
dc.ExecQuery(queries[a]);
Log.d("TAGG",queries[a].toString());
}
}
}
在数据库中我有简单的功能:
public void ExecQuery(String query){
DbCon.this.open();
database.rawQuery(query, null);
DbCon.this.close();
}
帮助?
日志猫:
04-22 02:36:42.648: E/AndroidRuntime(5983): 致命异常: main 04-22 02:36:42.648: E/AndroidRuntime(5983): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com. example.droid/com.example.droid.Incomes_act}: java.lang.NullPointerException 04-22 02:36:42.648: E/AndroidRuntime(5983): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 04 -22 02:36:42.648: E/AndroidRuntime(5983): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 04-22 02:36:42.648: E/AndroidRuntime(5983): 在 android.app .ActivityThread.access$600(ActivityThread.java:141) 04-22 02:36:42.648: E/AndroidRuntime(5983): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 04-22 02: 36:42.648: E/AndroidRuntime(5983): 在 android.os.Handler.dispatchMessage(Handler.java:99) 04-22 02:36:42.648: E/AndroidRuntime(5983): 在 android.os.Looper.loop(Looper.java:137) 04-22 02:36:42.648: E/AndroidRuntime(5983) : 在 android.app.ActivityThread.main(ActivityThread.java:5039) 04-22 02:36:42.648: E/AndroidRuntime(5983): 在 java.lang.reflect.Method.invokeNative(Native Method) 04-22 02 :36:42.648: E/AndroidRuntime(5983): at java.lang.reflect.Method.invoke(Method.java:511) 04-22 02:36:42.648: E/AndroidRuntime(5983): at com.android。 internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 04-22 02:36:42.648: E/AndroidRuntime(5983): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560 ) 04-22 02:36:42.648: E/AndroidRuntime(5983): at dalvik.system.NativeStart.main(Native Method) 04-22 02:36:42.648: E/AndroidRuntime(5983): Caused by: java.朗。NullPointerException 04-22 02:36:42.648: E/AndroidRuntime(5983): at com.example.droid.OnlineSync.Exec(OnlineSync.java:89) 04-22 02:36:42.648: E/AndroidRuntime(5983):在 com.example.droid.Incomes_act.onCreate(Incomes_act.java:88) 04-22 02:36:42.648: E/AndroidRuntime(5983): 在 android.app.Activity.performCreate(Activity.java:5104) 04- 22 02:36:42.648: E/AndroidRuntime(5983): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 04-22 02:36:42.648: E/AndroidRuntime(5983): 在 android.app。 ActivityThread.performLaunchActivity(ActivityThread.java:2144) 04-22 02:36:42.648: E/AndroidRuntime(5983): ... 11 更多Income_act.onCreate(Incomes_act.java:88) 04-22 02:36:42.648: E/AndroidRuntime(5983): 在 android.app.Activity.performCreate(Activity.java:5104) 04-22 02:36:42.648: E/AndroidRuntime(5983): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 04-22 02:36:42.648: E/AndroidRuntime(5983): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java :2144) 04-22 02:36:42.648: E/AndroidRuntime(5983): ... 11 更多Income_act.onCreate(Incomes_act.java:88) 04-22 02:36:42.648: E/AndroidRuntime(5983): 在 android.app.Activity.performCreate(Activity.java:5104) 04-22 02:36:42.648: E/AndroidRuntime(5983): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 04-22 02:36:42.648: E/AndroidRuntime(5983): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java :2144) 04-22 02:36:42.648: E/AndroidRuntime(5983): ... 11 更多performLaunchActivity(ActivityThread.java:2144) 04-22 02:36:42.648: E/AndroidRuntime(5983): ... 11 更多performLaunchActivity(ActivityThread.java:2144) 04-22 02:36:42.648: E/AndroidRuntime(5983): ... 11 更多
另一个 logcat 日志 #2:
04-22 13:55:48.304: E/AndroidRuntime(933): 致命异常: main 04-22 13:55:48.304: E/AndroidRuntime(933): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com. example.droid/com.example.droid.Incomes_act}:java.lang.NullPointerException 04-22 13:55:48.304:E/AndroidRuntime(933):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 04 -22 13:55:48.304: E/AndroidRuntime(933): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 04-22 13:55:48.304: E/AndroidRuntime(933): 在 android.app .ActivityThread.access$600(ActivityThread.java:130) 04-22 13:55:48.304: E/AndroidRuntime(933): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 04-22 13: 55:48.304: E/AndroidRuntime(933): 在 android.os.Handler.dispatchMessage(Handler.java:99) 04-22 13:55:48.304: E/AndroidRuntime(933): 在 android.os.Looper.loop(Looper.java:137) 04-22 13:55:48.304: E/AndroidRuntime(933): 在android.app.ActivityThread.main(ActivityThread.java:4745) 04-22 13:55:48.304: E/AndroidRuntime(933): 在 java.lang.reflect.Method.invokeNative(Native Method) 04-22 13:55 :48.304: E/AndroidRuntime(933): at java.lang.reflect.Method.invoke(Method.java:511) 04-22 13:55:48.304: E/AndroidRuntime(933): at com.android.internal。 os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 04-22 13:55:48.304: E/AndroidRuntime(933): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 04 -22 13:55:48.304: E/AndroidRuntime(933): at dalvik.system.NativeStart.main(Native Method) 04-22 13:55:48.304: E/AndroidRuntime(933): Caused by: java.lang. NullPointerException 04-22 13:55:48。304: E/AndroidRuntime(933): 在 com.example.droid.OnlineSync.Exec(OnlineSync.java:92) 04-22 13:55:48.304: E/AndroidRuntime(933): 在 com.example.droid.Incomes_act .onCreate(Incomes_act.java:88) 04-22 13:55:48.304: E/AndroidRuntime(933): 在 android.app.Activity.performCreate(Activity.java:5008) 04-22 13:55:48.304: E /AndroidRuntime(933): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 04-22 13:55:48.304: E/AndroidRuntime(933): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2023)5008) 04-22 13:55:48.304: E/AndroidRuntime(933): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 04-22 13:55:48.304: E/AndroidRuntime(933): 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)5008) 04-22 13:55:48.304: E/AndroidRuntime(933): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 04-22 13:55:48.304: E/AndroidRuntime(933): 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
创建线程:
String arr[];
Thread trd = new Thread(new Runnable(){
@Override
public void run(){
BufferedReader in = null;
String data = null;
try{
final HttpClient client = new DefaultHttpClient();
URI website = new URI("http://www.arleitiss.netne.net/DBindex.php?action=fetchTrans");
final HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.seperator");
while(( l = in.readLine()) != null){
sb.append(l + nl);
}
in.close();
data = sb.toString();
String arr2[] = data.split("<br>");
arr = arr2;
}
catch(Exception e){
e.printStackTrace();
return;
}
}
});