2012-12-04 43 views
0

我得到了一個ListView工作正常,但我想我的頭在頂部,所以我做了一個新的佈局,我把標題和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> 

這裏是logcat的:

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 
+2

豎起logcat的! –

+0

嗨,我很抱歉我把logcat了。 – Laurenswuyts

回答

2

ListActivity具有特殊的行爲你不要考慮你的代碼。如果使用R.layout.list_item3佈局文件作爲ListActivity的內容視圖,然後在佈局文件必須ListView有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(); 
+0

嗨,謝謝。我改變了它,但我仍然接近力量。我把logcat放了起來。你可以看看這個。提前致謝。 – Laurenswuyts

+0

@ user1827512您的logcat中的錯誤非常明顯,「由於java.lang.RuntimeException:您的內容必須具有ID屬性爲'android.R.id.list'的ListView」請再次檢查您的XML。 Luksprog絕對正確。 – Simon

+0

是的,我看到了。但我確實改變了它......但我確實有另一個活動,它也有一個listview,它也使用id:list ..可以這樣嗎? – Laurenswuyts