每当我尝试运行任何 Android 应用程序时都会遇到问题。我已经下载了 Eclipse indigo 及其 ADT 插件,它也是版本:20.0.0.v201206242043-391819。
MainActivity.java
:
package com.example.onlineres;
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://10.0.2.2/city.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");
}
}
catch(JSONException e1){
Toast.makeText(getBaseContext(), "No City Found" ,Toast.LENGTH_LONG).show();
} catch (ParseException e1) {
e1.printStackTrace();
}
}
}
manifest.xml
:(我已经添加了访问互联网的使用权限)
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.onlineres"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
日志猫是
07-19 14:08:04.933: W/ActivityThread(271): Application com.example.onlineres is waiting for the debugger on port 8100...
07-19 14:08:04.963: I/System.out(271): Sending WAIT chunk
07-19 14:08:05.023: I/dalvikvm(271): Debugger is active
07-19 14:08:05.173: I/System.out(271): Debugger has connected
07-19 14:08:05.173: I/System.out(271): waiting for debugger to settle...
07-19 14:08:05.373: I/System.out(271): waiting for debugger to settle...
07-19 14:08:05.573: I/System.out(271): waiting for debugger to settle...
07-19 14:08:05.776: I/System.out(271): waiting for debugger to settle...
07-19 14:08:05.981: I/System.out(271): waiting for debugger to settle...
07-19 14:08:06.183: I/System.out(271): waiting for debugger to settle...
07-19 14:08:06.383: I/System.out(271): waiting for debugger to settle...
07-19 14:08:06.586: I/System.out(271): waiting for debugger to settle...
07-19 14:08:06.793: I/System.out(271): debugger has settled (1348)
07-19 14:08:45.853: D/AndroidRuntime(271): Shutting down VM
07-19 14:08:45.853: W/dalvikvm(271): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-19 14:08:45.943: E/AndroidRuntime(271): FATAL EXCEPTION: main
07-19 14:08:45.943: E/AndroidRuntime(271): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.onlineres/com.example.onlineres.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
07-19 14:08:45.943: E/AndroidRuntime(271): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-19 14:08:45.943: E/AndroidRuntime(271): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-19 14:08:45.943: E/AndroidRuntime(271): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-19 14:08:45.943: E/AndroidRuntime(271): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-19 14:08:45.943: E/AndroidRuntime(271): at android.os.Handler.dispatchMessage(Handler.java:99)
07-19 14:08:45.943: E/AndroidRuntime(271): at android.os.Looper.loop(Looper.java:123)
07-19 14:08:45.943: E/AndroidRuntime(271): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-19 14:08:45.943: E/AndroidRuntime(271): at java.lang.reflect.Method.invokeNative(Native Method)
07-19 14:08:45.943: E/AndroidRuntime(271): at java.lang.reflect.Method.invoke(Method.java:521)
07-19 14:08:45.943: E/AndroidRuntime(271): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-19 14:08:45.943: E/AndroidRuntime(271): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-19 14:08:45.943: E/AndroidRuntime(271): at dalvik.system.NativeStart.main(Native Method)
07-19 14:08:45.943: E/AndroidRuntime(271): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
07-19 14:08:45.943: E/AndroidRuntime(271): at android.app.ListActivity.onContentChanged(ListActivity.java:245)
07-19 14:08:45.943: E/AndroidRuntime(271): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:201)
07-19 14:08:45.943: E/AndroidRuntime(271): at android.app.Activity.setContentView(Activity.java:1647)
07-19 14:08:45.943: E/AndroidRuntime(271): at com.example.onlineres.MainActivity.onCreate(MainActivity.java:38)
07-19 14:08:45.943: E/AndroidRuntime(271): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-19 14:08:45.943: E/AndroidRuntime(271): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-19 14:08:45.943: E/AndroidRuntime(271): ... 11 more