尝试在空对象引用

时间:2017-12-07 20:18:12

标签: java android facebook

我尝试建立Facebook连接,但似乎我犯了一个错误,现在我的Facebook按钮崩溃了。

public class FacebookConnect extends AppCompatActivity {

    private CallbackManager callbackManager;
    private AccessTokenTracker accessTokenTracker;
    private ProfileTracker profileTracker;
    public String TAG;


    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 error) {
            System.out.println("test");
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        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 currentProfile) {
                nextActivity(currentProfile);
            }
        };
        accessTokenTracker.startTracking();
        profileTracker.startTracking();
        LoginButton loginButton = (LoginButton)findViewById(R.id.facebookSignupButton);
        loginButton.setReadPermissions("user_friends");
        loginButton.registerCallback(callbackManager, callback);
        new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                Profile profile = Profile.getCurrentProfile();
                nextActivity(profile);
                Toast.makeText(getApplicationContext(), "Log in", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onError(FacebookException error) {

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

    protected void onPause(){
        super.onPause();
    }

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

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

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

你知道怎么解决吗?

这实际上是我点击时的错误,当我尝试建立谷歌连接时,我遇到同样的问题。

  

12-07 19:54:48.717 3772-3772 / com.climb.eip.climb E / AndroidRuntime:FATAL EXCEPTION:main                                                                          过程:com.climb.eip.climb,PID:3772                                                                          java.lang.RuntimeException:无法启动活动   ComponentInfo {com.climb.eip.climb / com.climb.eip.climb.utils.FacebookConnect}:   java.lang.NullPointerException:尝试调用虚方法'void   com.facebook.login.widget.LoginButton.setReadPermissions(java.lang.String中[])”   在null对象引用上                                                                              在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)                                                                              在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)                                                                              在android.app.ActivityThread.-wrap12(ActivityThread.java)                                                                              在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460)                                                                              在android.os.Handler.dispatchMessage(Handler.java:102)                                                                              在android.os.Looper.loop(Looper.java:154)                                                                              在android.app.ActivityThread.main(ActivityThread.java:6077)                                                                              at java.lang.reflect.Method.invoke(Native Method)                                                                              在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:866)                                                                              在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)                                                                           引起:java.lang.NullPointerException:尝试调用虚拟   方法'无效   com.facebook.login.widget.LoginButton.setReadPermissions(java.lang.String中[])”   在null对象引用上                                                                              在   com.climb.eip.climb.utils.FacebookConnect.onCreate(FacebookConnect.java:76)                                                                              在android.app.Activity.performCreate(Activity.java:6662)                                                                              在   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)                                                                              在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)                                                                              在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)                                                                              在android.app.ActivityThread.-wrap12(ActivityThread.java)                                                                              在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460)                                                                              在android.os.Handler.dispatchMessage(Handler.java:102)                                                                              在android.os.Looper.loop(Looper.java:154)                                                                              在android.app.ActivityThread.main(ActivityThread.java:6077)                                                                              at java.lang.reflect.Method.invoke(Native Method)                                                                              在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:866)                                                                              在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

1 个答案:

答案 0 :(得分:0)

在哪里?

setContentView(R.layout.your_layout);
onCreate()

中的

之后,在onCreate()中添加此行与您的布局名称
super.onCreate(savedInstanceState);