下面是我复制和编辑的一些代码。如果我让 ColorsActivity 扩展 Activity ,一切正常。但是如果它扩展了 ListActivity,它会给我一个错误,“您的内容必须有一个 ID 属性为 android.R.Id.List 的 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>
最佳答案
用这个
<ListView
android:id="@android:id/list"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>
关于Android ListView 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9595833/