Android:使用getApplication()粘贴ClassCastException,尝试集成Facebook Friendpicker示例

时间:2014-06-02 16:15:21

标签: java android eclipse facebook sample

我试图将Facebook Friendpicker Sample整合到Parse.com快速入门代码中。这将创建一个通过Facebook登录的应用程序,连接到Parse.com并允许您挑选朋友(更多计划的所有部分,当然)。选择Facebook好友后,您可以点击" done",此时应用会将所选好友存储到应用程序中。点击"完成"它会崩溃(但只有在点击"完成"之后,选择屏幕本身才能正常工作)。你能帮我理解出了什么问题吗(为什么下面这行导致了问题)?

造成问题的原因是:GamePlannerFriendPickerApplication application =(GamePlannerFriendPickerApplication)getApplication();

LogCat:

    06-02 15:25:02.176: W/dalvikvm(274): threadid=1: thread exiting with 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):  at com.parse.integratingfacebooktutorial.GamePlannerPickFriendsActivity$2.onDoneButtonClicked(GamePlannerPickFriendsActivity.java:66)
    06-02 15:25:02.285: E/AndroidRuntime(274):  at com.facebook.widget.PickerFragment$6.onClick(PickerFragment.java:650)
    06-02 15:25:02.285: E/AndroidRuntime(274):  at android.view.View.performClick(View.java:2408)
    06-02 15:25:02.285: E/AndroidRuntime(274):  at android.view.View$PerformClick.run(View.java:8816)
    06-02 15:25:02.285: E/AndroidRuntime(274):  at android.os.Handler.handleCallback(Handler.java:587)
    06-02 15:25:02.285: E/AndroidRuntime(274):  at android.os.Handler.dispatchMessage(Handler.java:92)
    06-02 15:25:02.285: E/AndroidRuntime(274):  at android.os.Looper.loop(Looper.java:123)
    06-02 15:25:02.285: E/AndroidRuntime(274):  at android.app.ActivityThread.main(ActivityThread.java:4627)
    06-02 15:25:02.285: E/AndroidRuntime(274):  at java.lang.reflect.Method.invokeNative(Native Method)
    06-02 15:25:02.285: E/AndroidRuntime(274):  at java.lang.reflect.Method.invoke(Method.java:521)
    06-02 15:25:02.285: E/AndroidRuntime(274):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    06-02 15:25:02.285: E/AndroidRuntime(274):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    06-02 15:25:02.285: E/AndroidRuntime(274):  at dalvik.system.NativeStart.main(Native Method)

令人讨厌的是,facebook示例使用完全相同的方法(第80行,PickFriendsActivity.java),只使用不同的变量名称。

请参阅GamePlannerPickFriendsActivity.java中的friendPickerFragment.setOnDoneButtonClickedListener:

    public class GamePlannerPickFriendsActivity extends FragmentActivity {
FriendPickerFragment friendPickerFragment;

// A helper to simplify life for callers who want to populate a Bundle with the necessary
// parameters. A more sophisticated Activity might define its own set of parameters; our needs
// are simple, so we just populate what we want to pass to the 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, we 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 is recreated by the framework and already has saved and
        // restored its state, so we don't need to specify args again. (In fact, this might be
        // incorrect if the fragment was modified programmatically since it was 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) {
            // We just store our selection in the Application for other activities to look 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 a query has already taken place.
        friendPickerFragment.loadData(false);
    } catch (Exception ex) {
        onError(ex);
    }
}
    }

我还会包含清单:

<?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>

而且,为了安全起见,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;
}
}

我真的希望你能帮助我。我不确定我在这里做错了什么(仍在学习绳索),所以任何帮助都将不胜感激!

1 个答案:

答案 0 :(得分:0)

您需要更改应用程序清单标记,如下所示:

<application
        android:name="full.package.name.GamePlannerFriendPickerApplication"

在您的清单中,您现在声明了“IntegratingFacebookTutorialApplication”类,因此当您获得应用程序引用并尝试将其转换为GamePlannerFriendPickerApplication时抛出异常,因为该实例的类型为“IntegratingFacebookTutorialApplication”,请确保设置正确的应用程序名称(GamePlannerFriendPickerApplication),你的类强制转换异常应该消失......

问候!