2011-04-27 31 views
-1

我試圖folling代碼到安卓讓問題在Android中的ListView

tv=(TextView) findViewById(R.id.ringtone); 
    if (!hasErrors) { 

     setListAdapter(new ArrayAdapter<String>(FindFilesByType.this, R.layout.main, 
       Ringtones)); 

     lv.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> aView, View v, 
        int position, long id) { 
       currentPosition = position; 
       playRingtone(DIRECTORY+Ringtones.get(position)); 
      } 
     }); 

但我得到以下logcat的和我的應用程序關閉強制關閉

 
04-27 11:05:08.212: INFO/ActivityManager(52): Starting activity: Intent { cmp=android.com.testHTML/.FindFilesByType } 
04-27 11:05:08.353: INFO/NotificationService(52): enqueueToast pkg=android.com.testHTML [email protected] duration=0 
04-27 11:05:08.454: ERROR/ArrayAdapter(715): You must supply a resource ID for a TextView 
04-27 11:05:08.454: WARN/dalvikvm(715): threadid=3: thread exiting with uncaught exception (group=0x4001aa28) 
04-27 11:05:08.454: ERROR/AndroidRuntime(715): Uncaught handler: thread main exiting due to uncaught exception 
04-27 11:05:08.473: ERROR/AndroidRuntime(715): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView 
04-27 11:05:08.473: ERROR/AndroidRuntime(715):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:347) 
04-27 11:05:08.473: ERROR/AndroidRuntime(715):  at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323) 
04-27 11:05:08.473: ERROR/AndroidRuntime(715):  at android.widget.AbsListView.obtainView(AbsListView.java:1273) 
04-27 11:05:08.473: ERROR/AndroidRuntime(715):  at android.widget.ListView.makeAndAddView(ListView.java:1658) 
04-27 11:05:08.473: ERROR/AndroidRuntime(715):  at android.widget.ListView.fillDown(ListView.java:637) 
04-27 11:05:08.473: ERROR/AndroidRuntime(715):  at android.widget.ListView.fillFromTop(ListView.java:694) 
04-27 11:05:08.473: ERROR/AndroidRuntime(715):  at android.widget.ListView.layoutChildren(ListView.java:1516) 
04-27 11:05:08.473: ERROR/AndroidRuntime(715):  at android.widget.AbsListView.onLayout(AbsListView.java:1112) 
04-27 11:05:08.473: ERROR/AndroidRuntime(715):  at android.view.View.layout(View.java:6569) 
04-27 11:05:08.473: ERROR/AndroidRuntime(715):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119) 
04-27 11:05:08.473: ERROR/AndroidRuntime(715):  at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1108) 
04-27 11:05:08.473: ERROR/AndroidRuntime(715):  at android.widget.LinearLayout.onLayout(LinearLayout.java:920) 
04-27 11:05:08.473: ERROR/AndroidRuntime(715):  at android.view.View.layout(View.java:6569) 
04-27 11:05:08.473: ERROR/AndroidRuntime(715):  at android.widget.FrameLayout.onLayout(FrameLayout.java:333) 
04-27 11:05:08.473: ERROR/AndroidRuntime(715):  at android.view.View.layout(View.java:6569) 
04-27 11:05:08.473: ERROR/AndroidRuntime(715):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119) 
04-27 11:05:08.473: ERROR/AndroidRuntime(715):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998) 
04-27 11:05:08.473: ERROR/AndroidRuntime(715):  at android.widget.LinearLayout.onLayout(LinearLayout.java:918) 
04-27 11:05:08.473: ERROR/AndroidRuntime(715):  at android.view.View.layout(View.java:6569) 
04-27 11:05:08.473: ERROR/AndroidRuntime(715):  at android.widget.FrameLayout.onLayout(FrameLayout.java:333) 
04-27 11:05:08.473: ERROR/AndroidRuntime(715):  at android.view.View.layout(View.java:6569) 
04-27 11:05:08.473: ERROR/AndroidRuntime(715):  at android.view.ViewRoot.performTraversals(ViewRoot.java:979) 
04-27 11:05:08.473: ERROR/AndroidRuntime(715):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1613) 
04-27 11:05:08.473: ERROR/AndroidRuntime(715):  at android.os.Handler.dispatchMessage(Handler.java:99) 
04-27 11:05:08.473: ERROR/AndroidRuntime(715):  at android.os.Looper.loop(Looper.java:123) 
04-27 11:05:08.473: ERROR/AndroidRuntime(715):  at android.app.ActivityThread.main(ActivityThread.java:4203) 
04-27 11:05:08.473: ERROR/AndroidRuntime(715):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-27 11:05:08.473: ERROR/AndroidRuntime(715):  at java.lang.reflect.Method.invoke(Method.java:521) 
04-27 11:05:08.473: ERROR/AndroidRuntime(715):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 
04-27 11:05:08.473: ERROR/AndroidRuntime(715):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549) 
04-27 11:05:08.473: ERROR/AndroidRuntime(715):  at dalvik.system.NativeStart.main(Native Method) 
04-27 11:05:08.473: ERROR/AndroidRuntime(715): Caused by: java.lang.ClassCastException: android.widget.LinearLayout 
04-27 11:05:08.473: ERROR/AndroidRuntime(715):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:340) 
04-27 11:05:08.473: ERROR/AndroidRuntime(715):  ... 30 more 
04-27 11:05:08.493: INFO/Process(52): Sending signal. PID: 715 SIG: 3 
04-27 11:05:08.493: INFO/dalvikvm(715): threadid=7: reacting to signal 3 
04-27 11:05:08.503: ERROR/dalvikvm(715): Unable to open stack trace file '/data/anr/traces.txt': Permission denied 
04-27 11:05:13.472: INFO/Process(715): Sending signal. PID: 715 SIG: 9 
04-27 11:05:13.503: INFO/ActivityManager(52): Process android.com.testHTML (pid 715) has died. 
04-27 11:05:13.512: INFO/WindowManager(52): WIN DEATH: Window{43885018 android.com.testHTML/android.com.testHTML.testHTMLActivity paused=false} 
04-27 11:05:13.512: INFO/WindowManager(52): WIN DEATH: Window{43933640 Toast paused=false} 
04-27 11:05:13.512: INFO/WindowManager(52): WIN DEATH: Window{439071b0 android.com.testHTML/android.com.testHTML.FindFilesByType paused=false} 
04-27 11:05:13.592: INFO/ActivityManager(52): Start proc android.com.testHTML for activity android.com.testHTML/.testHTMLActivity: pid=750 uid=10024 gids={3003, 1015} 
04-27 11:05:13.784: INFO/jdwp(750): received file descriptor 20 from ADB 
04-27 11:05:13.913: WARN/UsageStats(52): Something wrong here, didn't expect android.com.testHTML to be resumed 
04-27 11:05:14.503: WARN/InputManagerService(52): Got RemoteException sending setActive(false) notification to pid 715 uid 10024 
04-27 11:05:14.643: INFO/ActivityManager(52): Displayed activity android.com.testHTML/.testHTMLActivity: 1139 ms (total 6398 ms) 

什麼是錯的我在做什麼?提前

+0

你可以發佈你的lay out XML?你確定'R.id.ringtone'是指TextView嗎? – Bostone 2011-04-27 05:48:29

回答

0

的logcat中明確指出,你需要爲一個TextView,那裏的東西你提供一個LinearLayout中提供的資源ID (我的猜測,因爲它拋出ClassCastException)我相信你main.xml是一個linearlayout而不是一個textview。而不是嘗試R.id.ringtone

0

你的問題是在這些線路中描述

感謝:

11:05:08.473: ERROR/AndroidRuntime(715): Caused by: java.lang.ClassCastException: android.widget.LinearLayout 
04-27 11:05:08.473: ERROR/AndroidRuntime(715):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:340) 

這可能意味着你正在使用的LinearLayout,而不是一個。
(你最讓你做一些像
foobar = (LinearLayout) someCode();一個ClassCastException的時間)

1

好朋友你的問題是你使用r.layout.main列表行。

你必須創建在這裏你只提的TextView 一個separete listrow佈局,並把它作爲一個佈局 你的錯誤是在這條線

setListAdapter(new ArrayAdapter<String>(FindFilesByType.this, R.layout.main, 
       Ringtones)); 

創建佈局說listrow.xml

<?xml version="1.0" encoding="utf-8"?> 
<TexView 
..... 
/> 

並更換你的線

setListAdapter(new ArrayAdapter<String>(FindFilesByType.this, R.layout.listrow, 
        Ringtones)); 
+0

好的我得到的解決方案: - this.lv1.setAdapter(new ArrayAdapter (FindFilesByType.this,android.R.layout.test_list_item,Ringtones)); – Android 2011-04-27 06:06:30