您的内容必须具有一个ID属性为android.R.id.list的ListView [英] Your content must have a ListView whose id attribute is android.R.id.list

查看:45
本文介绍了您的内容必须具有一个ID属性为android.R.id.list的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
运行时异常ListView,其id属性为'android.R.id.list'

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

I am new to android I get the following runtime error.

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

我正在尝试按照本教程进行学习

I was trying following tutorial

http://www.mkyong.com/android/android-listview-例子/自定义ArrayAdapter示例"

http://www.mkyong.com/android/android-listview-example/ "Custom ArrayAdapter example"

当我运行他们的代码时,它可以正常工作,并且他们使用的是Android 2.3.3我使用4.0

when I run their code it works fine and they use android 2.3.3 I use 4.0

我根据自己的要求更改了他们的代码,并在logcat中遇到了运行时错误.这是我的代码.

I changed their code according to my requriment and got above runtime error in logcat. here is my code.

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小部件,但在日志猫中提及.除此之外,它说无法启动活动compnonetinfo.

in their example they dont use listview widget in xml file but mention in the log cat. apartfrom that it says unable to start activity compnonetinfo.

我在哪里做错了?请帮我改正这个问题.

where do I have done wrong? plz help me to correct this.

这是xml文件的原始代码.这是原始代码的来源

Here is the original code of the xml file. here is the source of original code

<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文件中没有使用listview窗口小部件,但是可以运行此示例.

I notice they dont have use listview widget in their xml file but works this example.

推荐答案

您的错误解决方案

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

如果您使用的是ListActivity,则布局的xml中必须具有ListView,并且ListView的ID必须为 android.R.id.list

If your are using ListActivity then you must have ListView in your xml of layout and must be id of ListView is android.R.id.list

因此必须像下面的代码一样在您的布局中添加listview

So must add listview in your layout like below code

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

这篇关于您的内容必须具有一个ID属性为android.R.id.list的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆