2016-02-01 80 views
-3

我是用android編程的中間人,我希望你能幫助我。使用ArrayAdapter添加項目到ListView對於API15和更少

我想添加一些項目到listView和當我從android開發人員網站閱讀,將項目添加到列表視圖您應該使用Adapter

例如我用了一個ArrayAdapter這樣:

ListView lst = (ListView) findViewById(R.id.listitm); 
    String[] values = new String[]{"Samsung", "Archos", "Itel", "HP", "TOSHIBA", "SonY", "Google", 
      "Microsoft", "Lenovo", "Acer", "Hawaii", "HTC", "LG", "etc.."}; 
    ArrayList<String> list = new ArrayList<String>(); 

    for(String value : values){ 
     list.add(value); 
    } 

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_main, list); 

    lst.setAdapter(adapter); 

但是,當我的手機上運行它的APK得到崩潰和調試時,它提供了錯誤的最後statment

lst.setAdapter(adapter);

它給在logcat此錯誤:

FATAL EXCEPTION: main 
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView 
    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386) 
    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362) 
    at android.widget.AbsListView.obtainView(AbsListView.java:2472) 
    at android.widget.ListView.measureHeightOfChildren(ListView.java:1250) 
    at android.widget.ListView.onMeasure(ListView.java:1162) 
    at android.view.View.measure(View.java:15575) 
    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617) 
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399) 
    at android.view.View.measure(View.java:15575) 
    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617) 
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399) 
    at android.view.View.measure(View.java:15575) 
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5107) 
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
    at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135) 
    at android.view.View.measure(View.java:15575) 
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5107) 
    at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391) 
    at android.view.View.measure(View.java:15575) 
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5107) 
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
    at android.view.View.measure(View.java:15575) 
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5107) 
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396) 
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:681) 
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574) 
    at android.view.View.measure(View.java:15575) 
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5107) 
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2423) 
    at android.view.View.measure(View.java:15575) 
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2031) 
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1249) 
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1425) 
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1142) 
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4724) 
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725) 
    at android.view.Choreographer.doCallbacks(Choreographer.java:555) 
    at android.view.Choreographer.doFrame(Choreographer.java:525) 
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711) 
    at android.os.Handler.handleCallback(Handler.java:615) 
    at android.os.Handler.dispatchMessage(Handler.java:92) 
    at android.os.Looper.loop(Looper.java:137) 
    at android.app.ActivityThread.main(ActivityThread.java:4947) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:511) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 
    at dalvik.system.NativeStart.main(Native Method) 
                         Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView 
    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379) 
    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)  
    at android.widget.AbsListView.obtainView(AbsListView.java:2472)  
    at android.widget.ListView.measureHeightOfChildren(ListView.java:1250)  
    at android.widget.ListView.onMeasure(ListView.java:1162)  
    at android.view.View.measure(View.java:15575)  
    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)  
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)  
    at android.view.View.measure(View.java:15575)  
    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)  
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)  
    at android.view.View.measure(View.java:15575)  
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5107)  
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)  
    at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)  
    at android.view.View.measure(View.java:15575)  
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5107)  
    at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)  
    at android.view.View.measure(View.java:15575)  
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5107)  
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)  
    at android.view.View.measure(View.java:15575)  
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5107)  
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)  
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)  
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)  
    at android.view.View.measure(View.java:15575)  
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5107)  
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)  
    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2423)  
    at android.view.View.measure(View.java:15575)  
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2031)  
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1249)  
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1425)  
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1142)  
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4724)  
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)  
    at android.view.Choreographer.doCallbacks(Choreographer.java:555)  
    at android.view.Choreographer.doFrame(Choreographer.java:525)  
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)  
    at android.os.Handler.handleCallback(Handler.java:615)  
    at android.os.Handler.dispatchMessage(Handler.java:92)  
    at android.os.Looper.loop(Looper.java:137)  
    at android.app.ActivityThread.main(ActivityThread.java:4947)  
    at java.lang.reflect.Method.invokeNative(Native Method)  
    at java.lang.reflect.Method.invoke(Method.java:511)  
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)  
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)  
                    at dalvik.system.NativeStart.main(Native Method)  

有幫助嗎?

+1

什麼是你的崩潰?請發佈日誌。 –

+0

如果它崩潰,那麼請張貼您的logcat錯誤跟蹤。 – Rohit5k2

+0

'R.layout.activity_main' for你的單元佈局看起來不對 –

回答

0

試試這個代碼。確保它能正常工作。

ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,list);

+0

如果它仍然無法讓我知道。 – Arun

+0

Thnx非常多,它對我來說很好,我忽視了背後的原因 –

相關問題