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レイアウト ファイルを のコンテンツ ビューとして使用する場合ListActivityListViewそのレイアウト ファイル内の には id が必要android:id="@android:id/list"です(これにより、はカスタム レイアウトから使用するウィジェットを認識します) ListActivityListViewしたがって、次のようにアイテムを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 に答える