Java 很遗憾,myapp已停止消息
我使用JSON在android中编写了一个客户机-服务器通信代码。我使用的是WAMP服务器,PHP文件中没有错误。当我运行我的应用程序模拟器时,会显示“不幸的是,我的应用程序已停止”。请给我一些建议。我试着去解决,但我做不到。 我的java文件:Java 很遗憾,myapp已停止消息,java,php,android,Java,Php,Android,我使用JSON在android中编写了一个客户机-服务器通信代码。我使用的是WAMP服务器,PHP文件中没有错误。当我运行我的应用程序模拟器时,会显示“不幸的是,我的应用程序已停止”。请给我一些建议。我试着去解决,但我做不到。 我的java文件: package com.example.http; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import
package com.example.http;
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.TextView;
import android.widget.Toast;
public class MainActivity extends ListActivity {
JSONArray jArray;
String result = null;
InputStream is = null;
StringBuilder sb=null;
TextView tv;
@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/android_connect/read1.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
try{
jArray = new JSONArray(result);
JSONObject json_data=null;
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
String ct_name = json_data.getString("User_ID");
tv = (TextView) findViewById(R.id.textView1);
tv.setText(ct_name);
}
}
catch(JSONException e1){
Toast.makeText(getBaseContext(), "No City Found" ,Toast.LENGTH_LONG).show();
} catch (ParseException e1) {
e1.printStackTrace();
}
}
}
您的内容必须具有ListView
,其id
属性为“android.R.id.list”
您的R.layout.activity\u main没有listview元素
PS:在扩展
ListActivity
并提供自定义布局时,您不应该在主线程read about上使用网络请求,您必须将以下Id设置为xml格式的ListView
——在本例中设置为活动\u main
android:id="@android:id/list"
顺便说一句,异常本身已经足够解释了。从错误消息中:
04-21 22:43:33.502: E/AndroidRuntime(399): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.http/com.example.http.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
您的“activity_main.xml”可能没有id为“android.R.id.list”的属性。
因为您的活动扩展了“ListActivity”,所以应该包含它
android:id="@android:id/list"
04-21 22:43:33.502: E/AndroidRuntime(399): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.http/com.example.http.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'