【问题标题】:ListView error :Your content must have a ListView whose id attribute is 'android.R.id.list'ListView 错误:您的内容必须有一个 id 属性为 'android.R.id.list' 的 ListView
【发布时间】:2013-03-19 18:50:23
【问题描述】:

我一直在为 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>

【问题讨论】:

  • 另一个错误是 08-27 10:36:32.837: E/AndroidRuntime(908): Caused by: java.lang.RuntimeException: Your content must have a ListView its id attribute is 'android.R .id.list'
  • 使用 listview id ==>android:id="@android:id/list"
  • 已经完成,你可以在上面的 main.xml 文件中看到它

标签: android


【解决方案1】:

将此ListView 放入您的main.xml

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

查看this existing answer. 并查看ListActivity tutorial 以获得更多帮助

【讨论】:

  • 它工作但出现以下错误 08-27 10:49:08.328: E/AndroidRuntime(1051): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mokshya.hosprsing/com. mokshya.hosprsing.HomeActivity}:android.os.NetworkOnMainThreadException 08-27 10:49:08.328:E/AndroidRuntime(1051):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
  • @sur007 为此,您必须使用 AsyncTask 从 JSON 获取详细信息,例如 this answer
  • 我应该在哪里使用这个 AsyncTask 混淆
  • @sur007 查看AsyncTask 的一些示例,用于将数据从 Json 获取到 ListView。 1) AsyncTask with ListView 2) populate ListView from JSON
  • 谢谢这对我很有帮助
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多