2012-10-30 56 views
0

可能重复:
runtime exception ListView whose id attribute is ‘android.R.id.list’您的内容必须有一个ListView,其id属性是android.R.id.list

我是新到Android我得到以下运行时错误。

"Your content must have a ListView whose id attribute is android.R.id.list" 

我尝试下面的教程

http://www.mkyong.com/android/android-listview-example/ “自定义ArrayAdapter例如”

当我运行他们的代码,它工作得很好,他们使用Android 2.3.3 我用4.0

我根据自己的需求更改了自己的代码,并在logcat中获得了超出运行时错误的信息。 这是我的代码。

import android.app.ListActivity; 
import android.content.Intent; 
import android.content.pm.ActivityInfo; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ListView; 
import android.widget.Toast; 

import com.xxxx.xxx.adapter.ListArrayAdapter; 

public class Page5SubActivity extends ListActivity { 

    static final String[] MOBILE_OS = new String[] { "Android", "iOS", 
      "WindowsMobile", "Blackberry", "test", "test2" }; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.page5sub); 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
     Intent intent = getIntent(); 
     int val = intent.getIntExtra("id", 0); 

     switch (val) { 

     case 2: 
      setListAdapter(new ListArrayAdapter(this, MOBILE_OS)); 

      break; 

     case 3: 
      break; 

     case 4: 
      break; 

     } 

    } 

    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 

     // get selected items 
     String selectedValue = (String) getListAdapter().getItem(position); 
     Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show(); 

    } 

    public void onClickBtn(View v) { 

     switch (v.getId()) { 

     case R.id.back_arrow: 
      Intent intent2 = new Intent(Page5SubActivity.this, 
        Page5Activity.class); 
      // intent2.putExtra("id", 2); 
      startActivity(intent2); 

      break; 
     } 

    } 

} 

这里是他们的示例XML文件

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/page1background" 
    android:gravity="center" > 


    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_centerInParent="false" 
     android:layout_marginBottom="@dimen/padding_Xlarge" 
     android:layout_marginTop="@dimen/padding_large" 
     android:paddingTop="@dimen/padding_large" 
     android:text="@string/text" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textColor="@color/textbody" /> 

    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="wrap_content" 
     android:layout_height="100dp" 
     android:layout_below="@id/textView1" 
     android:layout_centerHorizontal="true" 
     android:orientation="horizontal" > 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/text" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/linearLayout2" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_below="@id/linearLayout1" 
     android:weightSum="10" > 

     <View 
      android:id="@+id/view1" 
      android:layout_width="0dip" 
      android:layout_height="30dp" 
      android:layout_marginBottom="35dp" 
      android:layout_weight="1" /> 

     <ImageView 
      android:id="@+id/logo" 
      android:layout_width="50dp" 
      android:layout_height="50dp" 
      android:layout_marginLeft="5dp" 
      android:layout_marginRight="20dp" 
      android:layout_marginTop="5dp" 
      android:contentDescription="@string/Description" > 
     </ImageView> 

     <TextView 
      android:id="@+id/label" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@+id/label" > 
     </TextView> 

     <View 
      android:id="@+id/view2" 
      android:layout_width="0dip" 
      android:layout_height="30dp" 
      android:layout_marginBottom="35dp" 
      android:layout_weight="1" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/linearLayout3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:weightSum="10" > 

     <View 
      android:id="@+id/view3" 
      android:layout_width="0dip" 
      android:layout_height="30dp" 
      android:layout_marginBottom="35dp" 
      android:layout_weight="4.5" /> 

     <ImageButton 
      android:id="@+id/back_arrow" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginBottom="35dp" 
      android:layout_weight="1" 
      android:background="@drawable/backbut" 
      android:contentDescription="@string/Description" 
      android:onClick="onClickBtn" 
      android:src="@drawable/backarrowpress" /> 

     <View 
      android:id="@+id/view4" 
      android:layout_width="0dip" 
      android:layout_height="30dp" 
      android:layout_marginBottom="35dp" 
      android:layout_weight="4.5" /> 
    </LinearLayout> 

</RelativeLayout> 

,他们不使用的ListView控件在XML文件中,但在日志猫提到的代码。 除了它说无法启动活动compnonetinfo。

我在哪里做错了? PLZ帮我改正这一点。

这里是xml文件的原始代码。 这里是原代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="5dp" > 

    <ImageView 
     android:id="@+id/logo" 
     android:layout_width="50px" 
     android:layout_height="50px" 
     android:layout_marginLeft="5px" 
     android:layout_marginRight="20px" 
     android:layout_marginTop="5px" 
     android:src="@drawable/windowsmobile_logo" > 
    </ImageView> 

    <TextView 
     android:id="@+id/label" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@+id/label" 
     android:textSize="30px" > 
    </TextView> 

</LinearLayout> 

我发现他们没有使用的ListView控件中的XML文件,但工作这个例子源。

回答

3

你在错误的解决方案

"Your content must have a ListView whose id attribute is android.R.id.list" 

如果你正在使用ListActivity,那么你必须在你的xml布局中有ListView,并且必须是ListView的id是android.R.id.list

所以必须在你的布局像列表视图中添加下面的代码

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

http://stackoverflow.com/questions/3040374/runtime-exception-listview-whose-id-attribute-is-android-r-id-list –

+0

当我设置为你metion整个我的网页成为列表视图的项目。然后(我已经为背景设置了一个图像,即使这个图像显示为一个列表项目)。这有多正确? – newday

+0

@menukadevinda你可以在列表视图或父布局上设置背景 –

2

当您扩展ListActivity那么你的XML布局文件必须具有ListView控件的ID是android.R.id.list

在XML布局文件,以便把ListView和android:id="@+id/android.R.id.list"

+0

这里是在教程教程 – newday

+0

的原始源代码,他们不使用机器人:ID = “@ + ID/android.R.id.list” 虽然他们有扩展列表活动 – newday

1

设置你的列表视图ID这样

android:id="@id/android:list" 
1
在布局xml文件

你的ListView到@android必须设置ID:ID /列表

1

设置这样的适配器。

Adapter = new Adapter<String>(this,android.R.layout.simple_list_item_1, list); 

或检查XML

<ListView 
android:layout_height="wrap_content" 
android:layout_width="fill_parent" 
android:id="@+id/android:list" /> 
相关问题