java.lang.NullPointerException用facebook android studio

时间:2016-08-06 17:09:06

标签: java android facebook

在使用facebook登录时,我一次又一次地收到此错误。我无法理解。请帮助。

这是我的loginActivity代码:

public class LoginActivity extends AppCompatActivity {

    private CallbackManager callbackManager;
    private AccessTokenTracker accessTokenTracker;
    private ProfileTracker profileTracker;

    //Facebook login button
    private FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            Profile profile = Profile.getCurrentProfile();
            nextActivity(profile);
        }
        @Override
        public void onCancel() {        }
        @Override
        public void onError(FacebookException e) {      }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = CallbackManager.Factory.create();
        accessTokenTracker = new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {

            }
        };

        profileTracker = new ProfileTracker() {
            @Override
            protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) {
                nextActivity(newProfile);
            }
        };
        accessTokenTracker.startTracking();
        profileTracker.startTracking();
        LoginButton loginButton = (LoginButton)findViewById(R.id.login_button);
        callback = new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                AccessToken accessToken = loginResult.getAccessToken();
                Profile profile = Profile.getCurrentProfile();
                nextActivity(profile);
                Toast.makeText(getApplicationContext(), "Logging in...", Toast.LENGTH_SHORT).show();    }

            @Override
            public void onCancel() {
            }

            @Override
            public void onError(FacebookException e) {
            }
        };
        loginButton.setReadPermissions("user_friends");
        loginButton.registerCallback(callbackManager, callback);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //Facebook login
        Profile profile = Profile.getCurrentProfile();
        nextActivity(profile);
    }

    @Override
    protected void onPause() {

        super.onPause();
    }

    protected void onStop() {
        super.onStop();
        //Facebook login
        accessTokenTracker.stopTracking();
        profileTracker.stopTracking();
    }

    @Override
    protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
        super.onActivityResult(requestCode, responseCode, intent);
        //Facebook login
        callbackManager.onActivityResult(requestCode, responseCode, intent);

    }

    private void nextActivity(Profile profile){
        if(profile != null){
            Intent main = new Intent(LoginActivity.this, MainActivity.class);
            main.putExtra("name", profile.getFirstName());
            main.putExtra("surname", profile.getLastName());
            main.putExtra("imageUrl", profile.getProfilePictureUri(200,200).toString());
            startActivity(main);
        }
    }
}

以下是错误:

08-06 21:58:45.079 16282-16282/com.example.mabdullahali.trenditch E/AndroidRuntime: FATAL EXCEPTION: main
08-06 21:58:45.079 16282-16282/com.example.mabdullahali.trenditch E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mabdullahali.trenditch/com.example.mabdullahali.trenditch.LoginActivity}: java.lang.NullPointerException
08-06 21:58:45.079 16282-16282/com.example.mabdullahali.trenditch E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
08-06 21:58:45.079 16282-16282/com.example.mabdullahali.trenditch E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
08-06 21:58:45.079 16282-16282/com.example.mabdullahali.trenditch E/AndroidRuntime:     at android.app.ActivityThread.access$700(ActivityThread.java:159)
08-06 21:58:45.079 16282-16282/com.example.mabdullahali.trenditch E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
08-06 21:58:45.079 16282-16282/com.example.mabdullahali.trenditch E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99)
08-06 21:58:45.079 16282-16282/com.example.mabdullahali.trenditch E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:176)
08-06 21:58:45.079 16282-16282/com.example.mabdullahali.trenditch E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5419)
08-06 21:58:45.079 16282-16282/com.example.mabdullahali.trenditch E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
08-06 21:58:45.079 16282-16282/com.example.mabdullahali.trenditch E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:525)
08-06 21:58:45.079 16282-16282/com.example.mabdullahali.trenditch E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
08-06 21:58:45.079 16282-16282/com.example.mabdullahali.trenditch E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
08-06 21:58:45.079 16282-16282/com.example.mabdullahali.trenditch E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
08-06 21:58:45.079 16282-16282/com.example.mabdullahali.trenditch E/AndroidRuntime:  Caused by: java.lang.NullPointerException
08-06 21:58:45.079 16282-16282/com.example.mabdullahali.trenditch E/AndroidRuntime:     at com.example.mabdullahali.trenditch.LoginActivity.onCreate(LoginActivity.java:79)
08-06 21:58:45.079 16282-16282/com.example.mabdullahali.trenditch E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5372)
08-06 21:58:45.079 16282-16282/com.example.mabdullahali.trenditch E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
08-06 21:58:45.079 16282-16282/com.example.mabdullahali.trenditch E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
08-06 21:58:45.079 16282-16282/com.example.mabdullahali.trenditch E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349) 
08-06 21:58:45.079 16282-16282/com.example.mabdullahali.trenditch E/AndroidRuntime:     at android.app.ActivityThread.access$700(ActivityThread.java:159) 
08-06 21:58:45.079 16282-16282/com.example.mabdullahali.trenditch E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316) 
08-06 21:58:45.079 16282-16282/com.example.mabdullahali.trenditch E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99) 
08-06 21:58:45.079 16282-16282/com.example.mabdullahali.trenditch E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:176) 
08-06 21:58:45.079 16282-16282/com.example.mabdullahali.trenditch E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5419) 
08-06 21:58:45.079 16282-16282/com.example.mabdullahali.trenditch E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
08-06 21:58:45.079 16282-16282/com.example.mabdullahali.trenditch E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:525) 
08-06 21:58:45.079 16282-16282/com.example.mabdullahali.trenditch E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046) 
08-06 21:58:45.079 16282-16282/com.example.mabdullahali.trenditch E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862) 
08-06 21:58:45.079 16282-16282/com.example.mabdullahali.trenditch E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 

1 个答案:

答案 0 :(得分:0)

您尚未将自己的活动声明为清单文件。

无论何时创建新的活动类文件,都必须将其声明为清单文件。

 <activity
        android:name="LoginActivity"/>