Facebook Twitter按钮使用OnClickListener在同一个Activity中

时间:2016-03-23 14:51:52

标签: android facebook twitter callback

我正在尝试为在同一活动屏幕中具有Facebook和Twitter登录选项的用户构建登录屏幕

我在单独的活动中实现了Facebook按钮和Twitter按钮,并且它有效,但是当我在同一活动中添加两个按钮时,它会崩溃。

这是错误:

FATAL EXCEPTION: main
                                                                Process: poketpixels.reminder, PID: 7582
                                                                java.lang.ExceptionInInitializerError
                                                                    at java.lang.reflect.Constructor.newInstance(Native Method)
                                                                    at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
                                                                    at android.view.LayoutInflater.createView(LayoutInflater.java:607)
                                                                    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
                                                                    at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
                                                                    at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
                                                                    at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
                                                                    at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
                                                                    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
                                                                    at poketpixels.reminder.LoginActivity.onCreate(LoginActivity.java:118)
                                                                    at android.app.Activity.performCreate(Activity.java:5990)
                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                                                                    at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                    at android.os.Looper.loop(Looper.java:135)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                    at java.lang.reflect.Method.invoke(Method.java:372)
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                                                                 Caused by: null
                                                                    at com.facebook.internal.Validate.sdkInitialized(Validate.java:99)
                                                                    at com.facebook.FacebookSdk.getCallbackRequestCodeOffset(FacebookSdk.java:735)
                                                                    at com.facebook.internal.CallbackManagerImpl$RequestCodeOffset.toRequestCode(CallbackManagerImpl.java:109)
                                                                    at com.facebook.login.widget.LoginButton.<clinit>(LoginButton.java:58)
                                                                    at java.lang.reflect.Constructor.newInstance(Native Method) 
                                                                    at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
                                                                    at android.view.LayoutInflater.createView(LayoutInflater.java:607) 
                                                                    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) 
                                                                    at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
                                                                    at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 
                                                                    at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
                                                                    at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256) 
                                                                    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109) 
                                                                    at poketpixels.reminder.LoginActivity.onCreate(LoginActivity.java:118) 
                                                                    at android.app.Activity.performCreate(Activity.java:5990) 
                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                                                                    at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                                                                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                    at android.os.Looper.loop(Looper.java:135) 
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                    at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

这是我的源代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    {

        setContentView(R.layout.activity_login);
        info = (TextView) findViewById(R.id.info);
        FbloginButton = (LoginButton) findViewById(R.id.login_button);
        TloginButton = (TwitterLoginButton) findViewById(R.id.twitter_login_button);
        status = (TextView) findViewById(R.id.status);
        status.setText("Status: Ready");


        FbloginButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                //initialize facebook sdk
                FacebookSdk.sdkInitialize(getApplicationContext());
                callbackManager = CallbackManager.Factory.create();

                FbloginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        info.setText(
                                "User ID: "
                                        + loginResult.getAccessToken().getUserId()
                                        + "\n" +
                                        "Auth Token: "
                                        + loginResult.getAccessToken().getToken()
                        );
                    }

                    @Override
                    public void onCancel() {
                        info.setText("Login attempt canceled.");
                    }

                    @Override
                    public void onError(FacebookException e) {
                        info.setText("Login attempt failed.");
                    }
                });
            }
        });

        TloginButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                //intialize twitter sdk with fabric
                TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
                Fabric.with(getApplicationContext(), new Twitter(authConfig));


                TloginButton.setCallback(new Callback<TwitterSession>() {
                    @Override
                    public void success(Result<TwitterSession> twitterSessionResult) {
                        String output = "Status: " +
                                "Your login was successful " +
                                twitterSessionResult.data.getUserName() +
                                "\nAuth Token Received: " +
                                twitterSessionResult.data.getAuthToken().token;

                        status.setText(output);
                    }

                    @Override
                    public void failure(TwitterException e) {
                        status.setText("Status: Login Failed");
                    }
                });

            }
        });




@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
   TloginButton.onActivityResult(requestCode, resultCode, data);

}

如果这是正确的方法我很困惑。

这是xml文件:

         <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/info"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:textSize="18sp"
            />

        <com.facebook.login.widget.LoginButton
            android:id="@+id/login_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            />
        <com.twitter.sdk.android.core.identity.TwitterLoginButton
            android:id="@+id/twitter_login_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/status"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:textSize="20sp"
            />

1 个答案:

答案 0 :(得分:1)

Facebook SDK需要在使用其任何方法之前进行初始化。您可以通过调用sdkInitialize并将应用程序的上下文传递给它来完成此操作。在onCreate之前将以下代码添加到Activity的{​​{1}}方法中:

Onclicklistener

for twitter也在FacebookSdk.sdkInitialize(getApplicationContext()); 之前添加:

setContentView