android - ClasscastException on Application Start -
i trying utilize loadermanager api <11. tried find error using log utility. no such record found in logcat. here code. application tries retrieve info contacts. possible reasons classcastexception cast incompatible types, believe there not such problem in code. help appreciated.
package com.example.androidcontact; import android.net.uri; import android.os.bundle; import android.database.cursor; import android.provider.contactscontract; import android.support.v4.app.listfragment; import android.support.v4.app.loadermanager; import android.support.v4.content.cursorloader; import android.support.v4.content.loader; import android.support.v4.widget.cursoradapter; import android.support.v4.widget.simplecursoradapter; import android.util.log; public class mainactivity extends listfragment implements loadermanager.loadercallbacks<cursor>{ private simplecursoradapter madapter; string tag = ""; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); log.d(tag,"oncreated"); madapter = new simplecursoradapter(getactivity().getapplicationcontext(), android.r.layout.simple_list_item_2, null, new string[]{contactscontract.contacts._id, contactscontract.contacts.display_name}, new int[]{android.r.id.text1, android.r.id.text2}, cursoradapter.flag_register_content_observer); setlistadapter(madapter); getloadermanager().initloader(0, null, this); } @override public loader<cursor> oncreateloader(int id, bundle arg1) { uri uri = contactscontract.contacts.content_uri; string[]projection = new string[]{ contactscontract.contacts._id, contactscontract.contacts.display_name}; homecoming new cursorloader(getactivity(), uri, projection, null, null, null); } @override public void onloadfinished(loader<cursor> arg0, cursor arg1) { madapter.swapcursor(arg1); } @override public void onloaderreset(loader<cursor> arg0) { madapter.swapcursor(null); } }
please, not me wrong. spent time on how stack trace. still dont know how 1 exception occurs @ start. here content find.
02-19 00:35:35.401: w/dalvikvm(533): threadid=1: thread exiting uncaught exception (group=0x40015560) 02-19 00:35:43.417: e/androidruntime(533): fatal exception: main 02-19 00:35:43.417: e/androidruntime(533): java.lang.runtimeexception: unable instantiate activity componentinfo{com.example.androidcontact/com.example.androidcontact.mainactivity}: java.lang.classcastexception: com.example.androidcontact.mainactivity 02-19 00:35:43.417: e/androidruntime(533): @ android.app.activitythread.performlaunchactivity(activitythread.java:1569) 02-19 00:35:43.417: e/androidruntime(533): @ android.app.activitythread.handlelaunchactivity(activitythread.java:1663) 02-19 00:35:43.417: e/androidruntime(533): @ android.app.activitythread.access$1500(activitythread.java:117) 02-19 00:35:43.417: e/androidruntime(533): @ android.app.activitythread$h.handlemessage(activitythread.java:931) 02-19 00:35:43.417: e/androidruntime(533): @ android.os.handler.dispatchmessage(handler.java:99) 02-19 00:35:43.417: e/androidruntime(533): @ android.os.looper.loop(looper.java:123) 02-19 00:35:43.417: e/androidruntime(533): @ android.app.activitythread.main(activitythread.java:3683) 02-19 00:35:43.417: e/androidruntime(533): @ java.lang.reflect.method.invokenative(native method) 02-19 00:35:43.417: e/androidruntime(533): @ java.lang.reflect.method.invoke(method.java:507) 02-19 00:35:43.417: e/androidruntime(533): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:839) 02-19 00:35:43.417: e/androidruntime(533): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:597) 02-19 00:35:43.417: e/androidruntime(533): @ dalvik.system.nativestart.main(native method) 02-19 00:35:43.417: e/androidruntime(533): caused by: java.lang.classcastexception: com.example.androidcontact.mainactivity 02-19 00:35:43.417: e/androidruntime(533): @ android.app.instrumentation.newactivity(instrumentation.java:1021) 02-19 00:35:43.417: e/androidruntime(533): @ android.app.activitythread.performlaunchactivity(activitythread.java:1561) 02-19 00:35:43.417: e/androidruntime(533): ... 11 more
as per manifest file, needed app run on gingerbread so,
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.androidcontact" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="9" android:targetsdkversion="17" /> <uses-permission android:name="android.permission.read_contacts"/> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:name="android.app.application" > <activity android:name="com.example.androidcontact.mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest>
your current issue you're trying start fragment activity, doesn't work have seen firsthand. alter class looks this:
public class mainactivity extends activity { @override protected void oncreate(bundle b) { super.oncreate(b); setcontentview (r.layout.main); } public static class myfragment extends listfragment implements loadermanager.loadercallbacks<cursor>{ private simplecursoradapter madapter; string tag = ""; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); log.d(tag,"oncreated"); madapter = new simplecursoradapter(getactivity().getapplicationcontext(), android.r.layout.simple_list_item_2, null, new string[]{contactscontract.contacts._id, contactscontract.contacts.display_name}, new int[]{android.r.id.text1, android.r.id.text2}, cursoradapter.flag_register_content_observer); setlistadapter(madapter); getactivity().getsupportloadermanager().initloader(0, null, this); } @override public loader<cursor> oncreateloader(int id, bundle arg1) { uri uri = contactscontract.contacts.content_uri; string[]projection = new string[]{ contactscontract.contacts._id, contactscontract.contacts.display_name}; homecoming new cursorloader(getactivity(), uri, projection, null, null, null); } @override public void onloadfinished(loader<cursor> arg0, cursor arg1) { madapter.swapcursor(arg1); } @override public void onloaderreset(loader<cursor> arg0) { madapter.swapcursor(null); } } }
then in res/layout
create layout file called main.xml
have contain:
<?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id ="@+id/my_fragment" android:name="com.example.androidcontact.mainactivity$myfragment"> </fragment>
also incorporate @hsigmond's suggestion since are using back upwards library (code above has change).
android classcastexception
No comments:
Post a Comment