【问题标题】:Android: Your content must have a ListView whose id attribute is android.R.id.listAndroid:您的内容必须有一个 id 属性为 android.R.id.list 的 ListView
【发布时间】:2011-07-11 08:57:30
【问题描述】:

我遇到了这个运行时错误,我真的很难找到它的根源:“您的内容必须有一个 id 属性为 android.R.id.list 的 ListView”。

这是我的代码:

public class ShowAllJobsInArea extends ListActivity{

    Context context;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_jobs_in_area);
        context=getApplicationContext();

        String area=Cookie.getAreaSelected();

        final ProgressBar thinger=(ProgressBar) findViewById(R.id.progressBar2);
        TabHost tabHost=(TabHost)findViewById(android.R.id.tabhost);
        tabHost.setup();

        TabSpec spec1=tabHost.newTabSpec("Tab 1");
        spec1.setContent(R.id.tab1);
        spec1.setIndicator("Starting");

        TabSpec spec2=tabHost.newTabSpec("Tab 2");
        spec2.setContent(R.id.tab2);
        spec2.setIndicator("# Days");        

        TabSpec spec3=tabHost.newTabSpec("Tab 3");
        spec3.setContent(R.id.tab3);
        spec3.setIndicator("Rate");


        tabHost.addTab(spec1);
        tabHost.addTab(spec2);
        tabHost.addTab(spec3);

        Handler handler = new Handler() {
             public void handleMessage(Message message) {
                  switch (message.what) {
                      case HttpConnection.DID_START:
                          thinger.setVisibility(View.VISIBLE);
                          break;
                      case HttpConnection.DID_SUCCEED:
                          String response = (String) message.obj;
                          Log.i("EOH",response);

                          ArrayList<String> startDates=new ArrayList<String>();
                          ArrayList<String> ns=new ArrayList<String>();
                          ArrayList<String> rates=new ArrayList<String>();
                          HashMap<String, JSONObject> countyObjs=new HashMap<String, JSONObject>();

                          JSONObject object = null;
                          try {
                              object = (JSONObject) new JSONTokener(response).nextValue();

                              for(int i=0;i<object.length();i++){
                                  String area="";
                                  String endDate="";
                                  String endTimes="";
                                  String id="";
                                  String startDate="";
                                  String startTimes="";
                                  String rate="";
                                  String alreadyApplied="";
                                  String n="";
                                  JSONObject countyObj=object.getJSONObject(String.valueOf(i));
                                  countyObjs.put(id, countyObj);

                                  area=countyObj.getString("area");
                                  endDate=countyObj.getString("endDate");
                                  endTimes=countyObj.getString("endTimes");
                                  id=countyObj.getString("id");
                                  startDate=countyObj.getString("startDate");
                                  startTimes=countyObj.getString("startTimes");
                                  rate=(countyObj.getString("rates").split(","))[0];
                                  alreadyApplied=countyObj.getString("alreadyApplied");
                                  n=countyObj.getString("n");

                                  startDates.add(startDate+","+id);
                                  ns.add(n+","+id);
                                  rates.add(rate+","+id);
                              }
                          }catch (JSONException e) {
                              e.printStackTrace();
                          }

                          Collections.sort(startDates);
                          Collections.sort(ns);
                          Collections.sort(rates);                    

                          String[] startDates_str = new String[startDates.size()];
                          startDates.toArray(startDates_str);

                          setListAdapter(new ArrayAdapter<String>(getApplicationContext(), R.layout.list_item, startDates_str));
                          //setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));
                          ListView lv = getListView();
                          lv.setTextFilterEnabled(true);



                          thinger.setVisibility(View.INVISIBLE);

                          break;
                      case HttpConnection.DID_ERROR:
                          thinger.setVisibility(View.INVISIBLE);
                          break;
                      default:
                          break;
                  }
             }
        };

        List<NameValuePair> params = new ArrayList<NameValuePair>(1);
        params.add(new BasicNameValuePair("area", area)); 
        new HttpConnection(handler).post("http://www.xlhi.com/ajax/getJobsInArea.php",params);
    }
}

show_jobs_in_area.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <TabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent">
        <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
            <TabWidget android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@android:id/tabs"></TabWidget>
            <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@android:id/tabcontent">
                <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tab1" android:orientation="vertical">
                    <ProgressBar android:id="@+id/progressBar2" android:layout_height="wrap_content" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="25dip"></ProgressBar>
                    <ListView android:id="@+id/listView1" android:layout_height="wrap_content" android:layout_width="match_parent"></ListView>
                </LinearLayout>
                <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tab2"></LinearLayout>
                    <ListView android:id="@+id/listView2" android:layout_height="wrap_content" android:layout_width="match_parent">
                </ListView>
                <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tab3">
                    <ListView android:id="@+id/listView3" android:layout_height="wrap_content" android:layout_width="match_parent"></ListView>
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>
</LinearLayout>

list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp" >
</TextView>

我尝试将 id 更改为 android.id="@android:id/list" 无济于事 - 仍然收到相同的错误。我在想这与我正在做的异步请求有关......我现在真的被卡住了,希望有人能提供帮助。

非常感谢,

【问题讨论】:

    标签: android android-activity listactivity


    【解决方案1】:

    如果您的布局中有多个ListView,则不应扩展ListActivity,而应扩展Activity 并像自己一样处理ListViews

    ListView list1 = (ListView) findViewById(R.id.myList1);
    list1.setAdapter(...);
    
    ListView list2 = (ListView) findViewById(R.id.myList2);
    list2.setAdapter(...);
    

    ListActivity 是一个速记助手类,当您在布局中只使用一个 ListView 时,它可以让您的生活更轻松。

    【讨论】:

    • 是的,这似乎可以解决问题!不幸的是,现在我得到了一个空指针异常ListView list1 = (ListView) findViewById(android.R.id.list); list1.setAdapter(new ArrayAdapter&lt;String&gt;(getApplicationContext(),R.layout.list_item,startDates_str);
    • 在您的情况下,它可能应该是 findViewById(android.R.id.listView1) 和 findViewById(android.R.id.listView2)?
    • 现在深入了解...ListView list1 = (ListView) findViewById(R.id.listView1);list1.setAdapter(new ArrayAdapter&lt;String&gt;(getApplicationContext(),R.layout.list_item,startDates_str));
    • 非常感谢您的提示! +1
    • 但是如何更新这个适配器呢?我使用 myAdapter.notifyDataSetChanged();但什么也没发生!请帮忙
    【解决方案2】:

    在你的 show_jobs_in_area.xml 列表 id 之一中:

    这里有一个很好的例子 Listview error: "Your content must have a ListView whose id attribute is 'android.R.id.list'"

    【讨论】:

      【解决方案3】:

      如果你使用 getListView();确保目标列表的 id 是“列表”。

      【讨论】:

        猜你喜欢
        • 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
        相关资源
        最近更新 更多