2014-02-18 60 views
0

我想創建非常簡單的ListView,但不幸的是每次應用程序都會擦除。 這是我的代碼:ListView崩潰應用程序

import android.app.ListActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

public class MainActivity extends ListActivity implements OnItemClickListener { 

    public static String[] daysArray = {"Ponedeljek", "Torek", "Sreda", "Četrtek", "Petek", "Sobota", "Nedelja"}; 

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

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, daysArray); 

     setListAdapter(adapter); 

     ListView lv = getListView(); 
     lv.setOnItemClickListener(this); 

    } 

    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
     // TODO Auto-generated method stub 
     Log.i("Info", "CLICKED"); 
    } 

} 

佈局的main.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" > 

    <ListView android:id="@+id/listView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</RelativeLayout> 

編輯:

02-18 14:56:51.549: D/dalvikvm(2618): GC_FOR_ALLOC freed 68K, 6% free 3096K/3280K, paused 17ms, total 17ms 
02-18 14:56:51.561: I/dalvikvm-heap(2618): Grow heap (frag case) to 4.202MB for 1127532-byte allocation 
02-18 14:56:51.565: D/dalvikvm(2618): GC_FOR_ALLOC freed 2K, 5% free 4195K/4384K, paused 4ms, total 4ms 
02-18 14:56:51.581: D/AndroidRuntime(2618): Shutting down VM 
02-18 14:56:51.585: W/dalvikvm(2618): threadid=1: thread exiting with uncaught exception (group=0xa4d89b20) 
02-18 14:56:51.585: E/AndroidRuntime(2618): FATAL EXCEPTION: main 
02-18 14:56:51.585: E/AndroidRuntime(2618): Process: com.example.listsstudy, PID: 2618 
02-18 14:56:51.585: E/AndroidRuntime(2618): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.listsstudy/com.example.listsstudy.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 
02-18 14:56:51.585: E/AndroidRuntime(2618):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 
02-18 14:56:51.585: E/AndroidRuntime(2618):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
02-18 14:56:51.585: E/AndroidRuntime(2618):  at android.app.ActivityThread.access$800(ActivityThread.java:135) 
02-18 14:56:51.585: E/AndroidRuntime(2618):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
02-18 14:56:51.585: E/AndroidRuntime(2618):  at android.os.Handler.dispatchMessage(Handler.java:102) 
02-18 14:56:51.585: E/AndroidRuntime(2618):  at android.os.Looper.loop(Looper.java:136) 
02-18 14:56:51.585: E/AndroidRuntime(2618):  at android.app.ActivityThread.main(ActivityThread.java:5017) 
02-18 14:56:51.585: E/AndroidRuntime(2618):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-18 14:56:51.585: E/AndroidRuntime(2618):  at java.lang.reflect.Method.invoke(Method.java:515) 
02-18 14:56:51.585: E/AndroidRuntime(2618):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
02-18 14:56:51.585: E/AndroidRuntime(2618):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
02-18 14:56:51.585: E/AndroidRuntime(2618):  at dalvik.system.NativeStart.main(Native Method) 
02-18 14:56:51.585: E/AndroidRuntime(2618): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 
02-18 14:56:51.585: E/AndroidRuntime(2618):  at android.app.ListActivity.onContentChanged(ListActivity.java:243) 
02-18 14:56:51.585: E/AndroidRuntime(2618):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:293) 
02-18 14:56:51.585: E/AndroidRuntime(2618):  at android.app.Activity.setContentView(Activity.java:1929) 
02-18 14:56:51.585: E/AndroidRuntime(2618):  at com.example.listsstudy.MainActivity.onCreate(MainActivity.java:19) 
02-18 14:56:51.585: E/AndroidRuntime(2618):  at android.app.Activity.performCreate(Activity.java:5231) 
02-18 14:56:51.585: E/AndroidRuntime(2618):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
02-18 14:56:51.585: E/AndroidRuntime(2618):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
02-18 14:56:51.585: E/AndroidRuntime(2618):  ... 11 more 
+4

請張貼的logcat錯誤 – 2Dee

回答

1

需要使用

android:id="@android:id/list" 

代替:

android:id="@+id/listView" 

如果使用ListActivity。或者只是嘗試使用Activity,而不是ListActivity

編輯: 我的代碼; 佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 
    <ListView android:id="@android:id/list" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"/> 
... 

而且在我的課:

public class AnyNameHere extends ListActivity { 
    @Override 
    protected void onCreate (Bundle savedInstanceState) { 
     super.onCreate (savedInstanceState); 
     setContentView (R.layout.listview); 
      // do what you want here; 
} 

,它的工作:)

+0

嗯得到這個: '錯誤:錯誤:找不到與給定名稱相匹配的資源('id',值爲'@ id/list')' –

+0

我編輯我的回答:a dd例子。 HTH – Volodymyr

3

要擴展ListActivity。您的ListView必須有

android:id="@android:id/list" 

as id。

+1

我相信這只是@android:id/list – Submersed

0

由於您的setContentView方法調用。

2

你的ListView有錯誤的ID。從documentation

To do this, your own view MUST contain a ListView object with the id "@android:id/list" (or list if it's in code)

+0

得到錯誤: 找不到與給定名稱相匹配的資源(在'id'處,值爲'@ id/list')。 –

+0

你使用android:id =「@ android:id/list」作爲xml中的id嗎? – Submersed

0

當你想entend ListActivity以創建活動,爲MainActivity,那麼你的ListView XML必須用list如下標識的android:id ...

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