0

我做了一个简单的 android ListView 演示,但它不起作用我的代码如下:

主要的.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="20sp"
    android:textStyle="bold" >
</TextView>

main.java

    package com.example.mylist;

    import android.os.Bundle;
    import android.app.Activity;
    import android.app.ListActivity;
    import android.database.DataSetObserver;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;

    public class MainActivity extends ListActivity {
        static final String[] list = new String[] { "Awesome", "New", "Retro",
                "Rock", "Pop", "Bollywood", "hollywood" };

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_main,
                    list));
            ListView listvw = getListView();
            listvw.setTextFilterEnabled(true);

            listvw.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(getApplicationContext(),
                            ((TextView) v).getText(), Toast.LENGTH_SHORT).show();

                }
            });

        }

    }

日志猫

07-29 14:16:21.335: E/AndroidRuntime(27347): FATAL EXCEPTION: main
07-29 14:16:21.335: E/AndroidRuntime(27347): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mylist/com.example.mylist.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
07-29 14:16:21.335: E/AndroidRuntime(27347):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-29 14:16:21.335: E/AndroidRuntime(27347):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-29 14:16:21.335: E/AndroidRuntime(27347):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-29 14:16:21.335: E/AndroidRuntime(27347):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-29 14:16:21.335: E/AndroidRuntime(27347):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 14:16:21.335: E/AndroidRuntime(27347):    at android.os.Looper.loop(Looper.java:137)
07-29 14:16:21.335: E/AndroidRuntime(27347):    at android.app.ActivityThread.main(ActivityThread.java:4424)
07-29 14:16:21.335: E/AndroidRuntime(27347):    at java.lang.reflect.Method.invokeNative(Native Method)
07-29 14:16:21.335: E/AndroidRuntime(27347):    at java.lang.reflect.Method.invoke(Method.java:511)
07-29 14:16:21.335: E/AndroidRuntime(27347):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-29 14:16:21.335: E/AndroidRuntime(27347):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-29 14:16:21.335: E/AndroidRuntime(27347):    at dalvik.system.NativeStart.main(Native Method)
07-29 14:16:21.335: E/AndroidRuntime(27347): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
07-29 14:16:21.335: E/AndroidRuntime(27347):    at android.app.ListActivity.onContentChanged(ListActivity.java:243)
07-29 14:16:21.335: E/AndroidRuntime(27347):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:254)
07-29 14:16:21.335: E/AndroidRuntime(27347):    at android.app.Activity.setContentView(Activity.java:1862)
07-29 14:16:21.335: E/AndroidRuntime(27347):    at com.example.mylist.MainActivity.onCreate(MainActivity.java:20)

请帮助我...!

4

4 回答 4

1

只需在您的代码中注释这一行。然后我希望它有效

// setContentView(R.layout.activity_main);

并且不要调用 onclick 试试这种方式

listvw.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
        // When clicked, show a toast with the TextView text
        Toast.makeText(getApplicationContext(),
        ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
    }
});

并导入这两个

import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
于 2013-07-29T07:08:53.140 回答
0

您正在提供自定义布局,但未ListView在其中定义 a 。

将您的main.xml(我假设它是main.xmlor activity_main.xml,您的代码引用一个但您发布另一个)更改为:

<?xml version="1.0" encoding="utf-8"?>
<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:padding="10dp" />

http://developer.android.com/reference/android/app/ListActivity.html

ListActivity 有一个默认布局,它由屏幕中央的单个全屏列表组成。但是,如果您愿意,您可以通过在 onCreate() 中使用 setContentView() 设置您自己的视图布局来自定义屏幕布局。为此,您自己的视图必须包含一个 ID 为“@android:id/list”的 ListView 对象(如果它在代码中,则为列表)

或者,正如文档所建议的那样,setContentView()如果您不打算自定义布局,请删除该调用。

于 2013-07-29T07:06:37.100 回答
0
   <?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:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> 
    <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" /> 
  </LinearLayout>
于 2013-07-29T07:39:58.383 回答
0
    package com.ListView;

    public class ListViewActivity extends Activity implements OnItemSelectedListener{
   /** Called when the activity is first created. */
    private String[] 
    Name={"Destop","CPU","Pendrive","Mouse","Keyboard","Harddisk","Cardreader","MotherBord"};
    ListView l1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    l1=(ListView)findViewById(R.id.listView1);
    l1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,Name));
    l1.setOnItemSelectedListener(this);
   }

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub

    Toast.makeText(this, Name[arg2].toString(), Toast.LENGTH_LONG).show();
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

    Toast.makeText(this, "Item Not Selected", Toast.LENGTH_LONG).show();
         }
     }
于 2013-07-29T07:36:09.740 回答