Facebook LikeView导致Android崩溃

时间:2015-01-30 10:17:42

标签: android facebook facebook-like android-dialog

我在我的应用程序中添加了一个Facebook LikeView,当我把它放在一个对话框中时,我得到以下异常:

java.lang.ClassCastException: android.view.ContextThemeWrapper cannot be cast to android.app.Activity
at com.facebook.widget.LikeView.toggleLike(LikeView.java:467)
at com.facebook.widget.LikeView.access$6(LikeView.java:465)
at com.facebook.widget.LikeView$1.onClick(LikeView.java:430)
at android.view.View.performClick(View.java:3540)
at android.view.View$PerformClick.run(View.java:14167)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4532)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)

如果我将它添加到活动中,它可以正常工作。任何想法,问题是什么?

修改

这是我正在处理的代码:

这是我的对话框的xml:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >
	<com.facebook.widget.LikeView
        android:id="@+id/btn_fb_like"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

LikeDialog代码:

    public class LikeDialog extends Dialog {

	public LikeDialog(Context context) {
		super(context);
		
		setContentView(R.layout.like_dialog);
		((LikeView) findViewById(R.id.btn_fb_like)).setObjectId("URL_TO_MY_FACEBOOK_PAGE");
	}
}

最后我的MainActivity:

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Settings.sdkInitialize(this);
		
		LikeDialog likeDialog = new LikeDialog(this);
		likeDialog.show();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		LikeView.handleOnActivityResult(this, requestCode, resultCode, data);
	}
}

1 个答案:

答案 0 :(得分:1)

@Override
public void onCreate(Bundle savedInstanceState) {
    FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
    super.onCreate(savedInstanceState);
}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mLikeOnFacebook = (LikeView) view.findViewById(R.id.b_like_facebook);
    mLikeOnFacebook.setObjectIdAndType("Link to page", LikeView.ObjectType.PAGE);
}