在实施facebook登录时,Android应用程序正在崩溃

时间:2015-09-25 20:31:55

标签: android

我对Android很新。我试图实现facebook登录,但我面临错误

请告诉我可能是什么问题?

MainActivity.Java

package com.atpata_new;

import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookSdk;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
import com.facebook.FacebookException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends Activity {

    private TextView info;
    private LoginButton loginButton;
    private CallbackManager callbackManager;

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

        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = CallbackManager.Factory.create();
        setContentView(R.layout.activity_main);
        info = (TextView)findViewById(R.id.info);
        loginButton = (LoginButton)findViewById(R.id.login_button);
        loginButton.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.");

            }
        });

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

}
logcat中的

我得到了以下错误

09-26 01:50:00.783:E / AndroidRuntime(28704):致命异常:AsyncTask#5 09-26 01:50:00.783:E / AndroidRuntime(28704):进程:com.atpata_new,PID:28704 09-26 01:50:00.783:E / AndroidRuntime(28704):java.lang.NullPointerException 09-26 01:50:00.783:E / AndroidRuntime(28704):at java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:911) 09-26 01:50:00.783:E / AndroidRuntime(28704):at com.facebook.internal.Utility.queryAppSettings(Utility.java:822) 09-26 01:50:00.783:E / AndroidRuntime(28704):at com.facebook.login.widget.LoginButton $ 1.run(LoginButton.java:489) 09-26 01:50:00.783:E / AndroidRuntime(28704):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 09-26 01:50:00.783:E / AndroidRuntime(28704):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587) 09-26 01:50:00.783:E / AndroidRuntime(28704):at java.lang.Thread.run(Thread.java:841)

Mainfest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.atpata_new"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="23"/>
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.atpata_new.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.facebook.FacebookActivity"
  android:configChanges=
         "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
  android:theme="@android:style/Theme.Translucent.NoTitleBar"
  android:label="@string/app_name" />
    </application>
    <meta-data android:name="com.facebook.sdk.ApplicationId"
   android:value="@string/facebook_app_id"/>
</manifest>

1 个答案:

答案 0 :(得分:0)

如果您使用的是Android Studio,请使用Fragment创建空白活动。

让我们说ExampleFragment,我假设您的活动已经创建

    public class ExampleFragment extends Fragment {

    LoginButton loginButton;
    TextView mTextDetails;
    private AccessTokenTracker mTokenTracker;
    private ProfileTracker mProfileTracker;


    private CallbackManager mCallbackManager;
    private FacebookCallback<LoginResult> mCallback=new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            AccessToken accessToken = loginResult.getAccessToken();
            Profile profile = Profile.getCurrentProfile();
            Log.d("Fb Login Success ", "In OnSuccess");
            if(profile != null){
                Log.d("Message: ", "Welcome "+profile.getName());
            }
        }

        @Override
        public void onCancel() {
            Log.d("APP", "onCancel");
            Toast.makeText(getActivity(), "login via Facebook cancelled by user ", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onError(FacebookException e) {
            Log.d("APP", "onError");
            Toast.makeText(getActivity(), "Error Occurred!, Unable to login via Facebook, please try again ", Toast.LENGTH_LONG).show();
        }
    };

    //Empty constructor 
    public ExampleFragment() {
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
        mCallbackManager=CallbackManager.Factory.create();

        setupTokenTracker();
        setupProfileTracker();

        mTokenTracker.startTracking();
        mProfileTracker.startTracking();
    }

    @Override
    public void onResume() {
        super.onResume();
        Profile profile = Profile.getCurrentProfile();
        if(profile != null) {
            Log.d("Fb Login Success ", "In OnResume");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_login, container, false);
        return view;
    }

    public void onViewCreated(View view,Bundle savedInstanceState){
        super.onViewCreated(view,savedInstanceState);
        loginButton = (LoginButton) view.findViewById(R.id.login_button);
        loginButton.setFragment(this);

        //loginButton.setBackgroundResource(R.drawable.facebook);
        loginButton.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        loginButton.setCompoundDrawablePadding(0);
        loginButton.setPadding(20, 20, 20, 20);
        loginButton.setText("");
        loginButton.setWidth(250);
        loginButton.setHeight(35);


        //loginButton.setCompoundDrawables(null, null, null, null);
        loginButton.registerCallback(mCallbackManager, mCallback);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mCallbackManager.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onStop() {
        super.onStop();
        mTokenTracker.stopTracking();
        mProfileTracker.stopTracking();
    }

    private void setupTokenTracker() {
        mTokenTracker = new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
                Log.d("APP currentAccessToken", "" + currentAccessToken);
            }
        };
    }

    private void setupProfileTracker() {
        mProfileTracker = new ProfileTracker() {
            @Override
            protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
                if(currentProfile!=null)
                    Log.d("APP currentProfile", "" + currentProfile);
                    Log.d("Fb Login Success ", "In setupProfileTracker");
            }
        };
    }
    //End of file
}

在您的活动XML布局

               <fragment  
               android:id="@+id/fragment"
               android:name="com.example.ExampleFragment"
               tools:layout="@layout/fragment_example" 
               android:layout_width="match_parent"
               android:layout_height="match_parent" />

在片段xml布局中,Fragment_example.xml

<com.facebook.login.widget.LoginButton
    android:id="@+id/login_button"
    style="?android:textAppearanceSmall"
    android:layout_width="250dp"
    android:layout_height="50dp"
    android:text="Facebook" 
    android:background="#3b5998"
    android:textStyle="bold" 
    android:textColor="@android:color/white"
    android:radius="10dp"
    />