我现在正在处理我的 android 应用程序项目,但 listView 有问题 下面是代码和 LogCat - Errormessage;我的数据库存储在本地主机上,我正在使用 php 脚本来获取 mysql 数据库
主 - 应用程序的活动
public class PlanActivity extends Activity {
String url = "http://10.0.2.2/phpBusplan/phpConnect.php";
InputStream is;
ArrayList<String> results = new ArrayList<String>();
JSONObject json_data;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.planlayout);
RadioGroup rgZeit = (RadioGroup) findViewById(R.id.rgZeiten);
}
在这里,我从 php 脚本中获取数据
public void getData() {
String result = "";
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
// http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Fehler bei der http Verbindung "+e.toString());
}
// convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
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());
}
// parse json data
try{
JSONArray jArray = new JSONArray(result);
for(int i=1;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
results.add((String) json_data.get("IDHaltestelle") + " "+ json_data.get("HaltestelleName"));
}
filllist();
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
public <T> void filllist() {
ListView listStart = (ListView) findViewById(android.R.id.list);
ListAdapter adapter = new ArrayAdapter<T>(getApplicationContext(), android.R.layout.simple_list_item_1);
listStart.setAdapter(adapter);;
}`
日志 - 猫消息
10-07 17:43:39.557: E/AndroidRuntime(702): FATAL EXCEPTION: main
10-07 17:43:39.557: E/AndroidRuntime(702): java.lang.RuntimeException: Unable to start activity ComponentInfo{at.atn.android/at.atn.android.MainActivity}:java.lang.RuntimeException: Unable to start activity ComponentInfo{at.atn.android/at.atn.android.PlanActivity}: java.lang.NullPointerException
10-07 17:43:39.557: E/AndroidRuntime(702): at.android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-07 17:43:39.557: E/AndroidRuntime(702): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-07 17:43:39.557: E/AndroidRuntime(702): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-07 17:43:39.557: E/AndroidRuntime(702): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-07 17:43:39.557: E/AndroidRuntime(702): at android.os.Handler.dispatchMessage(Handler.java:99)
10-07 17:43:39.557: E/AndroidRuntime(702): at android.os.Looper.loop(Looper.java:123)
10-07 17:43:39.557: E/AndroidRuntime(702): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-07 17:43:39.557: E/AndroidRuntime(702): at java.lang.reflect.Method.invokeNative(Native Method)
10-07 17:43:39.557: E/AndroidRuntime(702): at java.lang.reflect.Method.invoke(Method.java:507)
10-07 17:43:39.557: E/AndroidRuntime(702): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-07 17:43:39.557: E/AndroidRuntime(702): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-07 17:43:39.557: E/AndroidRuntime(702): at dalvik.system.NativeStart.main(Native Method)
10-07 17:43:39.557: E/AndroidRuntime(702): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{at.atn.android/at.atn.android.PlanActivity}: java.lang.NullPointerException
10-07 17:43:39.557: E/AndroidRuntime(702): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-07 17:43:39.557: E/AndroidRuntime(702): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1487)
10-07 17:43:39.557: E/AndroidRuntime(702): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
10-07 17:43:39.557: E/AndroidRuntime(702): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
10-07 17:43:39.557: E/AndroidRuntime(702): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654)
10-07 17:43:39.557: E/AndroidRuntime(702): at android.widget.TabHost.setCurrentTab(TabHost.java:326)
10-07 17:43:39.557: E/AndroidRuntime(702): at android.widget.TabHost.addTab(TabHost.java:216)
10-07 17:43:39.557: E/AndroidRuntime(702): at at.atn.android.MainActivity.onCreate(MainActivity.java:38)
10-07 17:43:39.557: E/AndroidRuntime(702): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-07 17:43:39.557: E/AndroidRuntime(702): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-07 17:43:39.557: E/AndroidRuntime(702): ... 11 more
10-07 17:43:39.557: E/AndroidRuntime(702): Caused by: java.lang.NullPointerException
10-07 17:43:39.557: E/AndroidRuntime(702): at at.atn.android.PlanActivity.filllist(PlanActivity.java:143)
10-07 17:43:39.557: E/AndroidRuntime(702): at at.atn.android.PlanActivity.getData(PlanActivity.java:132)
10-07 17:43:39.557: E/AndroidRuntime(702): at at.atn.android.PlanActivity.onCreate(PlanActivity.java:51)
10-07 17:43:39.557: E/AndroidRuntime(702): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-07 17:43:39.557: E/AndroidRuntime(702): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-07 17:43:39.557: E/AndroidRuntime(702): ... 20 more
php - 脚本(名称:phpConnect.php)
<?php
$host = "localhost";
$username = "XXXXX";
$password = "XXXXX";
$db_name = "XXXXX";
mysql_connect($host,$username,$password);
mysql_select_db($db_name);
$q=mysql_query("SELECT * FROM haltestelle");
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));
mysql_close();
?>
json - 来自 php 脚本的数据
[{"IDHaltestelle":"1","HaltestelleName":"Schwarzenberg"},{"IDHaltestelle":"2","HaltestelleName":"Ulrichsberg"},{"IDHaltestelle":"3","HaltestelleName":"Aigen"},{"IDHaltestelle":"4","HaltestelleName":"Rohrbach"},{"IDHaltestelle":"5","HaltestelleName":"St. Owald"},{"IDHaltestelle":"6","HaltestelleName":"St. Stefan"},{"IDHaltestelle":"7","HaltestelleName":"Afiesl"},{"IDHaltestelle":"8","HaltestelleName":"Guglwald"},{"IDHaltestelle":"9","HaltestelleName":"Bad Leonfelden"},{"IDHaltestelle":"10","HaltestelleName":null},{"IDHaltestelle":"11","HaltestelleName":"Helfenberg"},{"IDHaltestelle":"12","HaltestelleName":"Haslach"},{"IDHaltestelle":"13","HaltestelleName":"Peilstein"},{"IDHaltestelle":"14","HaltestelleName":"Kollerschlag"},{"IDHaltestelle":"15","HaltestelleName":"Julbach"},{"IDHaltestelle":"16","HaltestelleName":"Nebelberg Ost"},{"IDHaltestelle":"17","HaltestelleName":"Sarleinsbach"},{"IDHaltestelle":"18","HaltestelleName":"HTL Neufelden"},{"IDHaltestelle":"19","HaltestelleName":"Niederwaldkirchen"},{"IDHaltestelle":"20","HaltestelleName":"St. Peter"},{"IDHaltestelle":"21","HaltestelleName":"St. Johann"},{"IDHaltestelle":"22","HaltestelleName":"St, Veit"},{"IDHaltestelle":"23","HaltestelleName":"Oberneukirchen"},{"IDHaltestelle":"24","HaltestelleName":"Herzogsdorf"},{"IDHaltestelle":"25","HaltestelleName":"St, Martin"},{"IDHaltestelle":"26","HaltestelleName":"Gramastetten"},{"IDHaltestelle":"27","HaltestelleName":"Linz Hauptbahnhof"},{"IDHaltestelle":"28","HaltestelleName":null},{"IDHaltestelle":"29","HaltestelleName":"Ottensheim"},{"IDHaltestelle":"30","HaltestelleName":"Walding"},{"IDHaltestelle":"31","HaltestelleName":"Gerling B127"},{"IDHaltestelle":"32","HaltestelleName":"Feldkirchen"},{"IDHaltestelle":"33","HaltestelleName":"Landshaag"},{"IDHaltestelle":"34","HaltestelleName":"Aschach Bahnhof"},{"IDHaltestelle":"35","HaltestelleName":"Hartkirchen"},{"IDHaltestelle":"36","HaltestelleName":"Eferding"},{"IDHaltestelle":"37","HaltestelleName":"Haibach"},{"IDHaltestelle":"38","HaltestelleName":"Kirchberg ob der Donau"},{"IDHaltestelle":"39","HaltestelleName":null},{"IDHaltestelle":"40","HaltestelleName":"Lembach"},{"IDHaltestelle":"41","HaltestelleName":"Niederkappel"},{"IDHaltestelle":"42","HaltestelleName":"Pfarrkirchen"},{"IDHaltestelle":"43","HaltestelleName":"Hofkirchen"},{"IDHaltestelle":"44","HaltestelleName":"Neustift"},{"IDHaltestelle":"45","HaltestelleName":"Oberkappel"},{"IDHaltestelle":"46","HaltestelleName":"Niederranna"},{"IDHaltestelle":"47","HaltestelleName":"Waldkirchen"},{"IDHaltestelle":"48","HaltestelleName":"Neukirchen"},{"IDHaltestelle":"49","HaltestelleName":"Peuerbach"}]
希望你能帮助我谢谢!