-1

我使用 JSON 为 android 中的客户端服务器通信编写了代码。我正在使用 WAMP 服务器并且 PHP 文件中没有错误。当我运行我的应用程序模拟器时显示“不幸的是,myapp 已停止”。请建议我的想法。我试图修复,但我不能。我的java文件:

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();
    }
}
}

和 PHP 文件:

<?php
mysql_connect("localhost","root","");
mysql_select_db("androidhive");
$sql=mysql_query("select User_ID from opass");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
mysql_close();
?>

日志错误:

04-21 22:43:33.502: E/AndroidRuntime(399): FATAL EXCEPTION: main
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'
04-21 22:43:33.502: E/AndroidRuntime(399):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-21 22:43:33.502: E/AndroidRuntime(399):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-21 22:43:33.502: E/AndroidRuntime(399):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-21 22:43:33.502: E/AndroidRuntime(399):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-21 22:43:33.502: E/AndroidRuntime(399):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-21 22:43:33.502: E/AndroidRuntime(399):  at android.os.Looper.loop(Looper.java:137)
04-21 22:43:33.502: E/AndroidRuntime(399):  at android.app.ActivityThread.main(ActivityThread.java:5041)
04-21 22:43:33.502: E/AndroidRuntime(399):  at java.lang.reflect.Method.invokeNative(Native Method)
04-21 22:43:33.502: E/AndroidRuntime(399):  at java.lang.reflect.Method.invoke(Method.java:511)
04-21 22:43:33.502: E/AndroidRuntime(399):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-21 22:43:33.502: E/AndroidRuntime(399):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-21 22:43:33.502: E/AndroidRuntime(399):  at dalvik.system.NativeStart.main(Native Method)
04-21 22:43:33.502: E/AndroidRuntime(399): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
04-21 22:43:33.502: E/AndroidRuntime(399):  at android.app.ListActivity.onContentChanged(ListActivity.java:243)
04-21 22:43:33.502: E/AndroidRuntime(399):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:273)
04-21 22:43:33.502: E/AndroidRuntime(399):  at android.app.Activity.setContentView(Activity.java:1881)
04-21 22:43:33.502: E/AndroidRuntime(399):  at com.example.http.MainActivity.onCreate(MainActivity.java:40)
04-21 22:43:33.502: E/AndroidRuntime(399):  at android.app.Activity.performCreate(Activity.java:5104)
04-21 22:43:33.502: E/AndroidRuntime(399):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-21 22:43:33.502: E/AndroidRuntime(399):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
4

3 回答 3

2

扩展ListActivity和提供自定义布局时,您必须将以下Id设置为您的ListViewin xml - 在本例中为您的activity_main.

android:id="@android:id/list"

顺便说一句,异常本身就足够解释了。

于 2013-04-24T14:32:35.540 回答
1

您的内容必须具有ListViewid属性为 'android.R.id.list'

您的 R.layout.activity_main 没有 listview 元素。

PS:你不应该在主线程上使用网络请求阅读关于AsyncTask

于 2013-04-24T14:31:59.020 回答
0

从错误消息中:

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”,所以您应该包含它。

于 2013-04-24T14:36:49.877 回答