1

我正在尝试使用带有 MySQL 数据库的 Json 创建一个 android 登录应用程序,但是当我运行它并尝试登录时,我收到以下错误:

11-22 12:29:08.079: E/AndroidRuntime(731): FATAL EXCEPTION: main
11-22 12:29:08.079: E/AndroidRuntime(731): java.lang.NullPointerException
11-22 12:29:08.079: E/AndroidRuntime(731):  at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
11-22 12:29:08.079: E/AndroidRuntime(731):  at com.example.loctrans.DBConnection.DBConnection(DBConnection.java:41)
11-22 12:29:08.079: E/AndroidRuntime(731):  at com.example.loctrans.MainActivity.onClick(MainActivity.java:56)
11-22 12:29:08.079: E/AndroidRuntime(731):  at android.view.View.performClick(View.java:4084)
11-22 12:29:08.079: E/AndroidRuntime(731):  at android.view.View$PerformClick.run(View.java:16966)
11-22 12:29:08.079: E/AndroidRuntime(731):  at android.os.Handler.handleCallback(Handler.java:615)
11-22 12:29:08.079: E/AndroidRuntime(731):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-22 12:29:08.079: E/AndroidRuntime(731):  at android.os.Looper.loop(Looper.java:137)
11-22 12:29:08.079: E/AndroidRuntime(731):  at android.app.ActivityThread.main(ActivityThread.java:4745)
11-22 12:29:08.079: E/AndroidRuntime(731):  at java.lang.reflect.Method.invokeNative(Native Method)
11-22 12:29:08.079: E/AndroidRuntime(731):  at java.lang.reflect.Method.invoke(Method.java:511)
11-22 12:29:08.079: E/AndroidRuntime(731):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-22 12:29:08.079: E/AndroidRuntime(731):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-22 12:29:08.079: E/AndroidRuntime(731):  at dalvik.system.NativeStart.main(Native Method)

当我验证代码时,我添加了一个 log.e() 方法来查找有错误的行。这是我的BD连接代码

public class DBConnection extends Activity{
String result =  null;
    InputStream is = null;

public String DBConnection(ArrayList<NameValuePair> nameValuePairs, String URL){

      try{
          Log.e("info error", " fase 2");
          HttpClient httpclient = new DefaultHttpClient();
          Log.e("info error", " fase 3");
          HttpPost httppost = new HttpPost(URL);
          Log.e("info error", " fase 4");
          httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
          Log.e("info error", httppost.toString());
          HttpResponse response = httpclient.execute(httppost);
          Log.e("info error", " fase 6");
          HttpEntity entity = response.getEntity();
          Log.e("info error", " fase 7");
          is = entity.getContent();
          Log.e("info error", " fase 8");
  }catch(Exception e){
      Toast.makeText(getApplicationContext(), "Error in http connection"+e.toString(), Toast.LENGTH_LONG).show();
  }
  //convert response to string
  try{
      Log.e("info error", " fase 9");
          BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
          StringBuilder sb = new StringBuilder();
          Log.e("info error", " fase 10");
          String line = null;
          while ((line = reader.readLine()) != null) {
                  sb.append(line + "\n");
          }
          is.close();
          result=sb.toString();
          Log.e("log_tag", "Cadena JSon " + result);
  }catch(Exception e){
      Toast.makeText(getApplicationContext(), "Error converting result "+e.toString(), 
              Toast.LENGTH_LONG).show();
      Log.e("log_tag", "Error converting result " + e.toString());
  }
  return result;
}

public String getResult(){
    return result;
}

}

在 logcat 中只有前三个 log.e 消息。

11-22 12:29:07.899: E/info error(731):  fase 2
11-22 12:29:07.919: E/info error(731):  fase 3
11-22 12:29:07.929: E/info error(731):  fase 4

所以我认为问题出在这一行: httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs) 但我不知道究竟是什么错误

这是主要活动代码的一部分:

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("Login" ,logi));
String URL = "http://192.168.1.10/Android/getlogin.php";          
DBConnection DBC = new DBConnection();
String result = DBC.DBConnection(nameValuePairs, URL);
4

2 回答 2

1
java.lang.NullPointerException
  at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
  at com.example.loctrans.DBConnection.DBConnection(DBConnection.java:41)

LogCat 报告问题出getApplicationContext()在您的 Toast 中,但在 catch 块中。你是对的,真正的问题在于你的 Http 代码,它可能抛出了 NetworkOnMainThread 异常。(您使用的是 Android 3.0+ 吗?)

您需要在不同的线程中执行可能较慢的网络操作。只需创建一个自定义 AsyncTask 并将您的代码移动到doInBackground().


DBConnection为什么要扩展Activity?您是否像使用未发布的代码中的 Activity 一样使用它?

于 2012-11-22T18:26:03.770 回答
0

在 Activity 中,您应该在需要上下文而不是 getApplicationContext() 的地方使用关键字this 。

Toast.makeText(this, "Error in http connection"+e.toString(), Toast.LENGTH_LONG).show();

这个错误指的是这个,就像 Sam 说的,有一个问题与你在 Thread UI 中运行的 HTTP 代码有关。您需要使用 AsyncTask 来完成所有 HTTP 工作。

http://developer.android.com/reference/android/os/AsyncTask.html

于 2012-11-22T18:35:02.580 回答