2012-03-07 69 views
1

下面是我复制和编辑的一些代码。如果我进行ColorsActivity扩展活动,一切正常。但是,如果它扩展ListActivity,它给了我错误,“你的内容必须有一个ListView谁是ID属性是android.R.Id.List”。Android ListView错误

有人可以向我解释这个吗?

public class ColorsActivity extends Activity { 
/** Called when the activity is first created. */ 
//ListView that will hold our items references back to main.xml 
ListView lstTest; 
//Array Adapter that will hold our ArrayList and display the items on the ListView 
SeekBarAdaptor seekBarAdaptor; 

//List that will host our items and allow us to modify that array adapter 
ArrayList<SeekBar> seekBarArrayList=null; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.seekbarlist); 
    //Initialize ListView 
    lstTest= (ListView)findViewById(R.id.myList); 

    //Initialize our ArrayList 
    seekBarArrayList = new ArrayList<SeekBar>(); 
    //Initialize our array adapter notice how it references the listitems.xml layout 
    seekBarAdaptor = new SeekBarAdaptor(ColorsActivity.this, R.layout.seekbars,seekBarArrayList); 
    SeekBar red = new SeekBar(ColorsActivity.this); 
    SeekBar blue = new SeekBar(ColorsActivity.this); 
    //Set the above adapter as the adapter of choice for our list 
    lstTest.setAdapter(seekBarAdaptor); 
    seekBarArrayList.add(red); 
    seekBarArrayList.add(blue); 
    //Instantiate the Web Service Class with he URL of the web service not that you must pass 
    //WebService webService = new WebService("http://www.sumasoftware.com/alerts/GetAlerts.php"); 

    //Pass the parameters if needed , if not then pass dummy one as follows 
    Map<String, String> params = new HashMap<String, String>(); 
    params.put("var", ""); 

    //Get JSON response from server the "" are where the method name would normally go if needed example 
    // webService.webGet("getMoreAllerts", params); 
    //String response = webService.webGet("", params); 

    /*try 
    { 
     //Parse Response into our object 
     Type collectionType = new TypeToken<ArrayList<alerts>>(){}.getType(); 

     //JSON expects an list so can't use our ArrayList from the lstart 
     List<alerts> lst= new Gson().fromJson(response, collectionType); 

     //Now that we have that list lets add it to the ArrayList which will hold our items. 
     for(alerts l : lst) 
     { 
      alrts.add(l); 
     } 

     //Since we've modified the arrayList we now need to notify the adapter that 
     //its data has changed so that it updates the UI 
     arrayAdapter.notifyDataSetChanged(); 
    } 
    catch(Exception e) 
    { 
     Log.d("Error: ", e.getMessage()); 
    }*/ 
} 

}

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
> 

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

</LinearLayout> 

回答

3

使用本

<ListView 
     android:id="@android:id/list" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_weight="1" > 
    </ListView> 
+0

看起来好像是 android:id =“@ android:id/list” works但是 android:id =“@ android:id/mylist” 没有。 错误:找不到与给定名称匹配的资源。在值为@android的id:id/mylist – Spectrem 2012-03-07 04:57:39

+0

您指的是“@android:id/mylist”,它正在寻找一个在Android资源中不存在的id。该ID中的@ android位表示'查看'mylist'的Android系统资源,这根本就不存在。 – 2012-03-07 05:02:55

1

与此

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

,如果你的类继承的活动,然后将其更改为

更改列表视图ID
public class className extends ListActivity 

,您可以通过调用方法

ListView listView = getListView(); 

得到你的ListView insted的的

lstTest= (ListView)findViewById(R.id.myList);