0

我得到了一个ListView工作正常但我希望我的标题在顶部所以我做了一个新的布局,我把标题和ListView. 现在我正在接近一个力量,我真的不知道它有什么问题。

public class AndroidListViewActivity extends ListActivity {

    String value;
    // Places Listview
        ListView lv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.list_item3);

        String[] restaurant = getResources().getStringArray(R.array.restaurant);
        String[] bar = getResources().getStringArray(R.array.bar);
        String[] gas = getResources().getStringArray(R.array.gas);
        String[] help = getResources().getStringArray(R.array.help);
        String[] hotel = getResources().getStringArray(R.array.hotel);
        String[] shopping = getResources().getStringArray(R.array.shopping);
        String[] bank = getResources().getStringArray(R.array.bank);
        String[] bus = getResources().getStringArray(R.array.bus);
        String[] film = getResources().getStringArray(R.array.film);


        Bundle extras = getIntent().getExtras();
        if (extras != null) {
         value = extras.getString("categoriename");
        }

        // storing string resources into Array
        if(value.equals("restaurant")){
             // Binding resources Array to ListAdapter
            this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item2, R.id.label, restaurant));
        }if(value.equals("bar")){
             // Binding resources Array to ListAdapter
            this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item2, R.id.label, bar));
        }if(value.equals("gas")){
             // Binding resources Array to ListAdapter
            this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item2, R.id.label, gas));
        }if(value.equals("film")){
             // Binding resources Array to ListAdapter
            this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item2, R.id.label, film));
        }if(value.equals("help")){
             // Binding resources Array to ListAdapter
            this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item2, R.id.label, help));
        }if(value.equals("bus")){
             // Binding resources Array to ListAdapter
            this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item2, R.id.label, bus));
        }if(value.equals("bank")){
             // Binding resources Array to ListAdapter
            this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item2, R.id.label, bank));
        }if(value.equals("shopping")){
             // Binding resources Array to ListAdapter
            this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item2, R.id.label, shopping));
        }if(value.equals("hotel")){
             // Binding resources Array to ListAdapter
            this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item2, R.id.label, hotel));
        }





        lv = (ListView) findViewById(R.id.list3);

        // listening to single list item on click
        lv.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view,
              int position, long id) {

              // selected item
              String product = ((TextView) view).getText().toString();

              // Launching new Activity on selecting single List Item
              Intent i = new Intent(getApplicationContext(), MainActivity.class);
              // sending data to new activity
              i.putExtra("thetext", product);
              startActivity(i);

          }
        });
    }

这是我的 list_item2:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="30dp"
    android:background="@drawable/list_selector"
    android:padding="10dip"
    android:textSize="16dip"
     android:layout_below="@+id/imageButton2" 
    android:textStyle="bold" >
    </TextView>

这是我的 list_item3:

<?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:orientation="vertical"
    android:background="#FFFFFF"
    android:id="@+id/mainLayout"
    > 
     <ImageView
        android:id="@+id/imageButton2"
        android:layout_width="fill_parent"
        android:layout_height="52dip"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:contentDescription="@string/header"
        android:src="@drawable/list_header" />
    <ImageButton
        android:id="@+id/imageButtonSearch"
        android:layout_width="60dip"
        android:layout_height="45dip"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:contentDescription="@string/header"
        android:src="@drawable/search" 
        android:background="@android:color/transparent" 
        />
    <ImageView
        android:id="@+id/imageButton3"
        android:layout_width="150dip"
        android:layout_height="50dip"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/titel" />

    <ListView
        android:id="@+id/list3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageButton2" />

    </RelativeLayout>

这是日志猫:

    12-04 18:29:49.886: E/AndroidRuntime(371): FATAL EXCEPTION: main
12-04 18:29:49.886: E/AndroidRuntime(371): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.laurenswuyts.find.it/com.laurenswuyts.find.it.AndroidListViewActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
12-04 18:29:49.886: E/AndroidRuntime(371):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-04 18:29:49.886: E/AndroidRuntime(371):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-04 18:29:49.886: E/AndroidRuntime(371):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-04 18:29:49.886: E/AndroidRuntime(371):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-04 18:29:49.886: E/AndroidRuntime(371):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-04 18:29:49.886: E/AndroidRuntime(371):  at android.os.Looper.loop(Looper.java:130)
12-04 18:29:49.886: E/AndroidRuntime(371):  at android.app.ActivityThread.main(ActivityThread.java:3683)
12-04 18:29:49.886: E/AndroidRuntime(371):  at java.lang.reflect.Method.invokeNative(Native Method)
12-04 18:29:49.886: E/AndroidRuntime(371):  at java.lang.reflect.Method.invoke(Method.java:507)
12-04 18:29:49.886: E/AndroidRuntime(371):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-04 18:29:49.886: E/AndroidRuntime(371):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-04 18:29:49.886: E/AndroidRuntime(371):  at dalvik.system.NativeStart.main(Native Method)
12-04 18:29:49.886: E/AndroidRuntime(371): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
12-04 18:29:49.886: E/AndroidRuntime(371):  at android.app.ListActivity.onContentChanged(ListActivity.java:243)
12-04 18:29:49.886: E/AndroidRuntime(371):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:210)
12-04 18:29:49.886: E/AndroidRuntime(371):  at android.app.Activity.setContentView(Activity.java:1657)
12-04 18:29:49.886: E/AndroidRuntime(371):  at com.laurenswuyts.find.it.AndroidListViewActivity.onCreate(AndroidListViewActivity.java:23)
12-04 18:29:49.886: E/AndroidRuntime(371):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-04 18:29:49.886: E/AndroidRuntime(371):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-04 18:29:49.886: E/AndroidRuntime(371):  ... 11 more
4

1 回答 1

2

ListActivity具有您在代码中未考虑的特殊行为。如果您将R.layout.list_item3布局文件用作 a 的内容视图ListActivity,则该ListView布局文件中的 id必须具有 id android:id="@android:id/list"(因此ListActivity将知道ListView要从自定义布局中使用哪个小部件)。所以像这样R.layout.list_item3修改ListView项目:

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

在您的代码中,您无需寻找ListView正当用途:

lv = getListView();
于 2012-12-04T18:09:29.783 回答