Facebook返回null

时间:2016-03-03 20:50:05

标签: android facebook

03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     FATAL EXCEPTION: main
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     Process: com.example.ricardo.tcc2, PID: 31818
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     java.lang.RuntimeException: Unable to resume activity      {com.example.ricardo.tcc2/com.example.ricardo.tcc2.Facebook}:     java.lang.NullPointerException: Attempt to invoke virtual method     'java.lang.String com.facebook.Profile.getId()' on a null object reference
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4008)
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4039)
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3235)
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     at android.app.ActivityThread.access$1000(ActivityThread.java:198)
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1682)
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145)
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:6843)
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.facebook.Profile.getId()' on a null object reference
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     at com.example.ricardo.tcc2.BlankFragment.onResume(BlankFragment.java:168)
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     at android.support.v4.app.Fragment.performResume(Fragment.java:2005)
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230)
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:2052)
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     at android.support.v4.app.FragmentController.dispatchResume(FragmentController.java:187)
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     at android.support.v4.app.FragmentActivity.onResumeFragments(FragmentActivity.java:462)
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:451)
 03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     at android.app.Activity.performResume(Activity.java:6639)
 03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3997)
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4039) 
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3235) 
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     at android.app.ActivityThread.access$1000(ActivityThread.java:198) 
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1682) 
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145) 
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:6843) 
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
03-03 17:36:46.791 31818-31818/com.example.ricardo.tcc2 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 

这是我的Facebook课程。

public class Facebook extends FragmentActivity {

private BlankFragment mainFragment;
private LoginButton loginButton;
private CallbackManager callbackManager;


public String name;
public String id;
public String imageUrl;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_facebook);



    FacebookSdk.sdkInitialize(getApplicationContext());
    callbackManager = CallbackManager.Factory.create();


    loginButton = (LoginButton) findViewById(R.id.login_button);

    printHashkey();

    if (savedInstanceState == null) {
        // Add the fragment on initial activity setup
        mainFragment = new BlankFragment();
        getSupportFragmentManager().beginTransaction()
                .add(android.R.id.content, mainFragment).commit();
    } else {
        // Or set the fragment from restored state info
        mainFragment = (BlankFragment) getSupportFragmentManager()
                .findFragmentById(android.R.id.content);
    }



}


public void printHashkey(){
    try {
        PackageInfo info = getPackageManager().getPackageInfo(
                "name",
                PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
    } catch (PackageManager.NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }
}




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



public void goToAttract(View v)
{
    Profile profile = Profile.getCurrentProfile();
    getProfile(profile);

    Intent intent = new Intent(this, MainActivity.class);
    Bundle b = new Bundle();
    b.putString("Nome", name);
    b.putString("ID", id);
    intent.putExtras(b);

    setResult(1, intent);
    finish();
}



public void getProfile(Profile profile){
    if(profile != null){
        id = profile.getId().toString();
        name = profile.getName().toString();

    }
}


}

这是我的片段

public class BlankFragment extends Fragment {



private CallbackManager callbackManager;
private TextView textView;
private ImageView imv;

private AccessTokenTracker accessTokenTracker;
private ProfileTracker profileTracker;
private Button btnVoltar;

private FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(LoginResult loginResult) {
        AccessToken accessToken = loginResult.getAccessToken();
        Profile profile = Profile.getCurrentProfile();

        displayMessage(profile);

    }

    @Override
    public void onCancel() {

    }

    @Override
    public void onError(FacebookException e) {

    }
};

public BlankFragment() {

}


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



    FacebookSdk.sdkInitialize(getActivity().getApplicationContext());

    callbackManager = CallbackManager.Factory.create();

    accessTokenTracker= new AccessTokenTracker() {
        @Override
        protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {

        }
    };

    profileTracker = new ProfileTracker() {
        @Override
        protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) {
            displayMessage(newProfile);
        }
    };

    accessTokenTracker.startTracking();
    profileTracker.startTracking();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {



    return inflater.inflate(R.layout.fragment_blank, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
    textView = (TextView) view.findViewById(R.id.textView);
    btnVoltar = (Button) view.findViewById(R.id.btnVoltar);


    loginButton.setReadPermissions("public_profile");
    loginButton.setFragment(this);
    loginButton.registerCallback(callbackManager, callback);



}

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

    callbackManager.onActivityResult(requestCode, resultCode, data);


}

private void displayMessage(Profile profile){

    TextView textView = (TextView) getActivity().findViewById(R.id.textView);
    ProfilePictureView profileImage = (ProfilePictureView) getActivity().findViewById(R.id.profilePicture);
    profileImage.setProfileId(profile.getId());

    if(profile != null){
        textView.setText(profile.getName());
        profileImage.setProfileId(profile.getId());

    }

}



@Override
public void onStop() {
    super.onStop();
    accessTokenTracker.stopTracking();
    profileTracker.stopTracking();
}

@Override
public void onResume() {
    super.onResume();
    Profile profile = Profile.getCurrentProfile();
    ProfilePictureView profileImage = (ProfilePictureView) getActivity().findViewById(R.id.profilePicture);
    profileImage.setProfileId(profile.getId());
    displayMessage(profile);


}







}

在我的MainActivity上,我有一个链接到facebook类的侧边菜单。 它工作正常。现在它开始显示此错误。我不明白为什么。

有任何帮助吗?

2 个答案:

答案 0 :(得分:2)

你应该尝试阅读你的错误:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.facebook.Profile.getId()' on a null object reference

这意味着您尝试使用的Profile对象为null。找出它为空的原因。不要因为之前的工作而假设,这不是一个问题。也许这是一个你因某些条件而没有遇到过的问题。

答案 1 :(得分:0)

显然这很有效。还在测试。 如果profile为null,则显示登录按钮。

@Override
public void onResume() {
    super.onResume();

    Profile profile = Profile.getCurrentProfile();
    if (profile != null) {
        ProfilePictureView profileImage = (ProfilePictureView) getActivity().findViewById(R.id.profilePicture);
        profileImage.setProfileId(profile.getId());
        displayMessage(profile);
    }
}