我正在尝试从我的 MySQL-DB 中获取密码,但 android 程序无法启动,这是我的 MainActivity.java 代码:
package com.example.datenbankconnectiontest;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class MainActivity extends ListActivity {
JSONArray jArray;
String result = null;
InputStream is = null;
StringBuilder sb=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://178.190.156.114/Datenbankconnectiontest.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection"+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line="0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//paring data
int ct_id;
String ct_name;
try{
jArray = new JSONArray(result);
JSONObject json_data=null;
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
ct_id=json_data.getInt("CITY_ID");
ct_name=json_data.getString("CITY_NAME");
Toast.makeText(getBaseContext(), "PW: " +ct_name ,Toast.LENGTH_LONG).show();
}
}
catch(JSONException e1){
Toast.makeText(getBaseContext(), "No City Found" ,Toast.LENGTH_LONG).show();
} catch (ParseException e1) {
e1.printStackTrace();
}
}
}
这是我的日志:
02-18 19:21:42.831: E/Trace(675): error opening trace file: No such file or directory (2)
02-18 19:21:43.331: D/AndroidRuntime(675): Shutting down VM
02-18 19:21:43.331: W/dalvikvm(675): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
02-18 19:21:43.340: E/AndroidRuntime(675): FATAL EXCEPTION: main
02-18 19:21:43.340: E/AndroidRuntime(675): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.datenbankconnectiontest/com.example.datenbankconnectiontest.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
02-18 19:21:43.340: E/AndroidRuntime(675): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
02-18 19:21:43.340: E/AndroidRuntime(675): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
02-18 19:21:43.340: E/AndroidRuntime(675): at android.app.ActivityThread.access$600(ActivityThread.java:130)
02-18 19:21:43.340: E/AndroidRuntime(675): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
02-18 19:21:43.340: E/AndroidRuntime(675): at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 19:21:43.340: E/AndroidRuntime(675): at android.os.Looper.loop(Looper.java:137)
02-18 19:21:43.340: E/AndroidRuntime(675): at android.app.ActivityThread.main(ActivityThread.java:4745)
02-18 19:21:43.340: E/AndroidRuntime(675): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 19:21:43.340: E/AndroidRuntime(675): at java.lang.reflect.Method.invoke(Method.java:511)
02-18 19:21:43.340: E/AndroidRuntime(675): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-18 19:21:43.340: E/AndroidRuntime(675): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-18 19:21:43.340: E/AndroidRuntime(675): at dalvik.system.NativeStart.main(Native Method)
02-18 19:21:43.340: E/AndroidRuntime(675): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
02-18 19:21:43.340: E/AndroidRuntime(675): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
02-18 19:21:43.340: E/AndroidRuntime(675): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:259)
02-18 19:21:43.340: E/AndroidRuntime(675): at android.app.Activity.setContentView(Activity.java:1867)
02-18 19:21:43.340: E/AndroidRuntime(675): at com.example.datenbankconnectiontest.MainActivity.onCreate(MainActivity.java:35)
02-18 19:21:43.340: E/AndroidRuntime(675): at android.app.Activity.performCreate(Activity.java:5008)
02-18 19:21:43.340: E/AndroidRuntime(675): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
02-18 19:21:43.340: E/AndroidRuntime(675): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
02-18 19:21:43.340: E/AndroidRuntime(675): ... 11 more
如果您能帮我找出错误,我将非常感激!如果您需要 PHP 端,请告诉我,但我认为没有必要,尽管它看起来像一个 android 错误。
我没有自己编写代码,它是从http://blog.sptechnolab.com/2011/02/10/android/android-connecting-to-mysql-using-php复制的(并且变量和 url 已更改) /
新的堆栈跟踪:
02-18 19:38:42.040: E/Trace(731): error opening trace file: No such file or directory (2)
02-18 19:38:42.580: E/log_tag(731): Error in http connectionandroid.os.NetworkOnMainThreadException
02-18 19:38:42.580: E/log_tag(731): Error converting result java.lang.NullPointerException
02-18 19:38:42.580: D/AndroidRuntime(731): Shutting down VM
02-18 19:38:42.580: W/dalvikvm(731): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
02-18 19:38:42.600: E/AndroidRuntime(731): FATAL EXCEPTION: main
02-18 19:38:42.600: E/AndroidRuntime(731): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.datenbankconnectiontest/com.example.datenbankconnectiontest.MainActivity}: java.lang.NullPointerException
02-18 19:38:42.600: E/AndroidRuntime(731): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
02-18 19:38:42.600: E/AndroidRuntime(731): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
02-18 19:38:42.600: E/AndroidRuntime(731): at android.app.ActivityThread.access$600(ActivityThread.java:130)
02-18 19:38:42.600: E/AndroidRuntime(731): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
02-18 19:38:42.600: E/AndroidRuntime(731): at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 19:38:42.600: E/AndroidRuntime(731): at android.os.Looper.loop(Looper.java:137)
02-18 19:38:42.600: E/AndroidRuntime(731): at android.app.ActivityThread.main(ActivityThread.java:4745)
02-18 19:38:42.600: E/AndroidRuntime(731): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 19:38:42.600: E/AndroidRuntime(731): at java.lang.reflect.Method.invoke(Method.java:511)
02-18 19:38:42.600: E/AndroidRuntime(731): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-18 19:38:42.600: E/AndroidRuntime(731): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-18 19:38:42.600: E/AndroidRuntime(731): at dalvik.system.NativeStart.main(Native Method)
02-18 19:38:42.600: E/AndroidRuntime(731): Caused by: java.lang.NullPointerException
02-18 19:38:42.600: E/AndroidRuntime(731): at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
02-18 19:38:42.600: E/AndroidRuntime(731): at org.json.JSONTokener.nextValue(JSONTokener.java:94)
02-18 19:38:42.600: E/AndroidRuntime(731): at org.json.JSONArray.<init>(JSONArray.java:87)
02-18 19:38:42.600: E/AndroidRuntime(731): at org.json.JSONArray.<init>(JSONArray.java:103)
02-18 19:38:42.600: E/AndroidRuntime(731): at com.example.datenbankconnectiontest.MainActivity.onCreate(MainActivity.java:68)
02-18 19:38:42.600: E/AndroidRuntime(731): at android.app.Activity.performCreate(Activity.java:5008)
02-18 19:38:42.600: E/AndroidRuntime(731): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
02-18 19:38:42.600: E/AndroidRuntime(731): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
02-18 19:38:42.600: E/AndroidRuntime(731): ... 11 more