0

所以我正在制作应用程序,我需要应用程序从互联网 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; 
                }

      }
    });
4

1 回答 1

0

手机运行的是什么版本的 Android?AVD 运行的是什么版本?

在 Android Honeycomb 及更高版本上,当您尝试在主线程上进行网络操作时会出现异常(更多信息请参见:Android http 连接异常)。如果是这种情况,您应该在单独的线程上执行网络操作。您的手机可能没有抱怨,因为它的 Android < 3.0(可能是 2.3?)允许您在主线程上执行网络操作。

这可能是您的空指针异常的原因,因为在这种情况下,SyncTrans 无法在 AVD 上执行。当您在此函数处静默捕获异常并在失败时返回 null 时,会引发 NullPointerException,因为在执行 SyncTrans 之后,您正在访问其返回值 at 的属性for(int a = 0; a < queries.length-1; a++)。如果 SyncTrans 失败,则Aqueries为 null,您将在尝试访问它的长度时抛出 NullPointerException。

我希望这能澄清你的问题。

编辑:在您的代码中没有发现有关此行为的错误,因为您实际上是在隐藏它们:

try{
    //Any code here
}
catch(Exception e){}

这通常是一种不好的做法,因为您只是默默地捕获错误而不做任何事情。例如,如果您更改为此:

try{
    //Any code
}
catch(Exception e){
e.printStackTrace();
}

您将在 Logcat 中看到带有错误的堆栈跟踪。

于 2013-04-22T13:55:16.590 回答