0

我一直在为 JSON 解析进行编码,所有事情都是正确的,但是在 logcat 中显示了以下错误

08-27 10:25:49.628: E/AndroidRuntime(816): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mokshya.hosprsing/com.mokshya.hosprsing.HomeActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

以下是我的主要代码

package com.mokshya.hosprsing;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.app.ListActivity;
import android.view.Menu;
import android.widget.ArrayAdapter;

public class HomeActivity extends ListActivity {


@SuppressWarnings({"rawtypes","unchecked"})
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    setListAdapter(new ArrayAdapter (this,     android.R.layout.simple_list_item_1,this.fetchHosTime()));
}
public ArrayList<String> fetchHosTime(){
    ArrayList<String> listitems=new ArrayList<String>();
    try {
        URL hos=new URL("http://ldsclient.com/ftp/strtojson.php");
        URLConnection tc=hos.openConnection();
        BufferedReader in=new BufferedReader(new     InputStreamReader(tc.getInputStream()));
        String line;
        while((line=in.readLine())!=null) {
            JSONArray ja=new JSONArray(line);
                for(int i=0; i<ja.length();i++) {
                    JSONObject jo=(JSONObject) ja.get(i);
                    listitems.add(jo.getString("text"));

                }
        }
    }
    catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return listitems;
}


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
 }

下面是我的 main.xml 代码

<ListView android:id="@android:id/list" 
    android:layout_height="fill_parent"
    android:layout_width="wrap_content"></ListView>

</RelativeLayout>
4

2 回答 2

1

把它ListView放在你的main.xml

<ListView
  android:id="@android:id/list"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" >
</ListView>

请参阅此现有答案。并且,查看ListActivity 教程以获得更多帮助

于 2012-08-27T04:55:20.207 回答
1

不要在主 UI 线程上使用网络调用。使用另一个线程或 Asynctask 。最好的方法是 Asynctask

于 2012-08-27T06:22:48.517 回答