我正在尝试使用带有 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);