i am trying folling code into android
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));
}
});
but i am getting following Logcat and my application close forcefully close
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 callback=android.app.ITransientNotification$Stub$Proxy@4394f760 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-开发者_如何学Python27 11:05:14.643: INFO/ActivityManager(52): Displayed activity android.com.testHTML/.testHTMLActivity: 1139 ms (total 6398 ms)
what is wrong i am doing?
Thanks in advance
well friend your problem is you are using r.layout.main for list row.
you have to create a separete listrow layout where you mention only textview and pass it as a layout your error is on this line
setListAdapter(new ArrayAdapter<String>(FindFilesByType.this, R.layout.main,
Ringtones));
create a layout say listrow.xml
<?xml version="1.0" encoding="utf-8"?>
<TexView
.....
/>
and change your line
setListAdapter(new ArrayAdapter<String>(FindFilesByType.this, R.layout.listrow,
Ringtones));
The logcat clearly states that you need to provide the resource id for a textview, where as what you are providing is a linearlayout (My guess since it throws ClassCastException) I believe you main.xml is a linearlayout and not a textview. Instead try R.id.ringtone
You problem is described in these lines:
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)
It probably means that you are using something as a LinearLayout
, while it isn't one.
(most of the time you get a ClassCastException you're doing something like
foobar = (LinearLayout) someCode();
)
精彩评论