Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
611 views
in Technique[技术] by (71.8m points)

your content must have a listview whose id attribute is ' android.r.id.list ' .

I write the program but when i run this program, the program has runtime error :(

The error in LogCat is :

your content must have a listview whose id attribute is 'android.r.id.list'

I set android:id="@android:id/list" in ListView in activity_main.xml file.

I write my code, Please read this and help me .

MainActivity.java:

public class MainActivity extends ListActivity {

private TextView text;
private static final String[] items={"test", "test1" , "test2" , "test3" , "test4" , "test5" , "test6"};

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setListAdapter(new ArrayAdapter<String>(this,R.layout.row,R.id.label,items));
    text=(TextView)findViewById(R.id.selection);
}
public void onListItemClick(ListView parent,View v,int position,Long id){
    text.setText(items[position]);
}
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical">
   <TextView
       android:id="@+id/selection"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"/>
   <ListView
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@android:id/list"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:drawSelectorOnTop="false"/>
</LinearLayout>

row.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
   <ImageView
       android:id="@+id/pic"
       android:padding="2dip"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:src="@drawable/pic"/>
   <TextView
       android:id="@+id/label"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"/>
</LinearLayout>

Cheers.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Please extend Activity instead of ListActivity,

Then get its view from layout file

ListView listView = (ListView) findViewById(R.id.listView);

Then call listView.setAdapter(/*agruments*/); method

instead of calling setListAdapter().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...