Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
您的内容必须具有id属性为android.R.id.list的ListView_Android_Listview - Fatal编程技术网

您的内容必须具有id属性为android.R.id.list的ListView

您的内容必须具有id属性为android.R.id.list的ListView,android,listview,Android,Listview,可能重复: 我是android新手,遇到以下运行时错误 "Your content must have a ListView whose id attribute is android.R.id.list" 我试着跟着教程走 “自定义ArrayAdapter示例” 当我运行他们的代码时,工作正常,他们使用安卓2.3.3 我使用4.0 我根据我的要求修改了他们的代码,并在logcat中得到了上面的运行时错误。 这是我的密码 import android.app.ListActivity; i

可能重复:

我是android新手,遇到以下运行时错误

"Your content must have a ListView whose id attribute is android.R.id.list"
我试着跟着教程走

“自定义ArrayAdapter示例”

当我运行他们的代码时,工作正常,他们使用安卓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>

在他们的示例中,他们没有在xml文件中使用listview小部件,而是在日志cat中提到。 它说无法启动活动compnonetinfo

我哪里做错了? 请帮我纠正这个问题

下面是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>


我注意到他们的xml文件中没有UseListView小部件,但在这个例子中是有效的

扩展ListActivity时,xml布局文件必须具有id为
android.R.id.list


因此,将listview放在xml布局文件中,并在错误中找到解决方案

"Your content must have a ListView whose id attribute is android.R.id.list"
如果您使用的是ListActivity,则必须在布局的xml中包含ListView,并且必须是ListView的id is
android.R.id.list

所以必须在布局中添加listview,如下面的代码所示

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

如下设置Listview id

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

在布局xml文件中,必须将ListView的id设置为@android:id/list

如下设置适配器

  Adapter = new Adapter<String>(this,android.R.layout.simple_list_item_1, list);
Adapter=新适配器(这是android.R.layout.simple\u list\u item\u 1,list);
或者检查XML

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


当我将整个页面设置为列表视图项时。然后(我已经为背景设置了一个图像,即使该图像显示为列表项)。如何更正此问题?@menukadevinda u可以在listview或父布局上设置Background它在父布局中设置,这是一个相对的布局,现在整个页面像列表一样重复。如何更改列表项的颜色?这是教程的原始源代码在教程中他们不使用android:id=“@+id/android.R.id.list”虽然他们有很多活动