简体   繁体   中英

Facebook Twitter Button in same Activity using OnClickListener

I am trying to build a Login Screen for users which has Facebook and Twitter Login option in the same activity screen

I implemented the Facebook button, and Twitter button in separate activities, and it worked, but when I add the two buttons in the same activity, it crashes.

This are the errors:

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) 

This is my source code :

@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);

}

I am confused if this is the right approach or not.

this is the xml file :

         <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"
            />

Facebook SDK needs to be initialized before using any of its methods. You can do so by calling sdkInitialize and passing the application's context to it. Add the following code to the onCreate method of your Activity before Onclicklistener :

FacebookSdk.sdkInitialize(getApplicationContext());

for twitter also before setContentView add:

TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
Fabric.with(this, new Twitter(authConfig));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM