java - Android: Stuck on ClassCastException using getApplication(), trying to integrate Facebook Friendpicker sample -
i'm trying integrate facebook friendpicker sample parse.com quickstart code. create app logs in through facebook, connected parse.com , allows pick friends (all part of greater plan, ofcourse). after selecting facebook friends can click "done", @ point app store selected friends application. when clicking "done" crashes instead (but after clicking "done", selection screen works fine). me understand going wrong (why following line causes problem)?
the line causing problem is: gameplannerfriendpickerapplication application = (gameplannerfriendpickerapplication) getapplication();
the logcat:
06-02 15:25:02.176: w/dalvikvm(274): threadid=1: thread exiting uncaught exception (group=0x4001d800) 06-02 15:25:02.285: e/androidruntime(274): fatal exception: main 06-02 15:25:02.285: e/androidruntime(274): java.lang.classcastexception: com.parse.integratingfacebooktutorial.integratingfacebooktutorialapplication 06-02 15:25:02.285: e/androidruntime(274): @ com.parse.integratingfacebooktutorial.gameplannerpickfriendsactivity$2.ondonebuttonclicked(gameplannerpickfriendsactivity.java:66) 06-02 15:25:02.285: e/androidruntime(274): @ com.facebook.widget.pickerfragment$6.onclick(pickerfragment.java:650) 06-02 15:25:02.285: e/androidruntime(274): @ android.view.view.performclick(view.java:2408) 06-02 15:25:02.285: e/androidruntime(274): @ android.view.view$performclick.run(view.java:8816) 06-02 15:25:02.285: e/androidruntime(274): @ android.os.handler.handlecallback(handler.java:587) 06-02 15:25:02.285: e/androidruntime(274): @ android.os.handler.dispatchmessage(handler.java:92) 06-02 15:25:02.285: e/androidruntime(274): @ android.os.looper.loop(looper.java:123) 06-02 15:25:02.285: e/androidruntime(274): @ android.app.activitythread.main(activitythread.java:4627) 06-02 15:25:02.285: e/androidruntime(274): @ java.lang.reflect.method.invokenative(native method) 06-02 15:25:02.285: e/androidruntime(274): @ java.lang.reflect.method.invoke(method.java:521) 06-02 15:25:02.285: e/androidruntime(274): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:868) 06-02 15:25:02.285: e/androidruntime(274): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:626) 06-02 15:25:02.285: e/androidruntime(274): @ dalvik.system.nativestart.main(native method)
the annoying thing facebook example uses exact same method (line 80, pickfriendsactivity.java), different variable names.
see friendpickerfragment.setondonebuttonclickedlistener in gameplannerpickfriendsactivity.java:
public class gameplannerpickfriendsactivity extends fragmentactivity { friendpickerfragment friendpickerfragment; // helper simplify life callers want populate bundle necessary // parameters. more sophisticated activity might define own set of parameters; our needs // simple, populate want pass friendpickerfragment. public static void populateparameters(intent intent, string userid, boolean multiselect, boolean showtitlebar) { intent.putextra(friendpickerfragment.user_id_bundle_key, userid); intent.putextra(friendpickerfragment.multi_select_bundle_key, multiselect); intent.putextra(friendpickerfragment.show_title_bar_bundle_key, showtitlebar); } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.game_planner_pick_friends_activity); fragmentmanager fm = getsupportfragmentmanager(); if (savedinstancestate == null) { // first time through, create our fragment programmatically. final bundle args = getintent().getextras(); friendpickerfragment = new friendpickerfragment(args); fm.begintransaction() .add(r.id.friend_picker_fragment, friendpickerfragment) .commit(); } else { // subsequent times, our fragment recreated framework , has saved , // restored state, don't need specify args again. (in fact, might // incorrect if fragment modified programmatically since created.) friendpickerfragment = (friendpickerfragment) fm.findfragmentbyid(r.id.friend_picker_fragment); } friendpickerfragment.setonerrorlistener(new pickerfragment.onerrorlistener() { @override public void onerror(pickerfragment<?> fragment, facebookexception error) { gameplannerpickfriendsactivity.this.onerror(error); } }); friendpickerfragment.setondonebuttonclickedlistener(new pickerfragment.ondonebuttonclickedlistener() { @override public void ondonebuttonclicked(pickerfragment<?> fragment) { // store our selection in application other activities at. gameplannerfriendpickerapplication application = (gameplannerfriendpickerapplication) getapplication(); application.setselectedusers(friendpickerfragment.getselection()); setresult(result_ok, null); finish(); } }); } private void onerror(exception error) { string text = getstring(r.string.exception, error.getmessage()); toast toast = toast.maketext(this, text, toast.length_short); toast.show(); } @override protected void onstart() { super.onstart(); try { gameplannerfriendpickerapplication application = (gameplannerfriendpickerapplication) getapplication(); list<graphuser> selectedusers = application.getselectedusers(); if (selectedusers != null && !selectedusers.isempty()) { friendpickerfragment.setselection(selectedusers); } // load data, unless query has taken place. friendpickerfragment.loaddata(false); } catch (exception ex) { onerror(ex); } } }
i'll include manifest:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.parse.integratingfacebooktutorial" android:versioncode="1" android:versionname="1.0" > <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.access_network_state" /> <uses-permission android:name="android.permission.write_external_storage"/> <uses-sdk android:minsdkversion="8" android:targetsdkversion="17" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" android:name=".integratingfacebooktutorialapplication" > <activity android:name=".loginactivity" android:label="@string/app_name" android:launchmode="singletop" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name=".userdetailsactivity" android:label="@string/app_name"/> <activity android:name=".gameplannerpickfriendsactivity" android:label="pick friends"> </activity> <activity android:name=".gameplannerfriendpickerapplication" android:label="@string/app_name"/> <activity android:name=".gameplannerpickfriendsmaincode" android:label="@string/app_name" android:windowsoftinputmode="adjustresize"> </activity> <meta-data android:name="com.facebook.sdk.applicationid" android:value="@string/app_id"/> </application>
and, safe, gameplannerfriendpickerapplication.java
public class gameplannerfriendpickerapplication extends application { private list<graphuser> selectedusers; public list<graphuser> getselectedusers() { return selectedusers; } public void setselectedusers(list<graphuser> selectedusers) { this.selectedusers = selectedusers; } }
i hope can me out. i'm not sure i'm doing wrong here (still learning ropes), appreciated!
you need change application manifest tag follows:
<application android:name="full.package.name.gameplannerfriendpickerapplication"
in manifest right declaring class "integratingfacebooktutorialapplication", when application reference , try cast gameplannerfriendpickerapplication throwing exception, because instance of type "integratingfacebooktutorialapplication", make sure set proper application name (gameplannerfriendpickerapplication) , class cast exception should gone...
regards!
Comments
Post a Comment