Android Facebook返回空值

Android Facebook返回空值,android,facebook,Android,Facebook,这是我的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

这是我的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) 
这是我的片段

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

    }
}


}
公共类BlankFragment扩展了Fragment{
私人CallbackManager CallbackManager;
私有文本视图文本视图;
私有ImageView imv;
私有AccessTokenTracker AccessTokenTracker;
个人档案跟踪器;
专用按钮btnVoltar;
private FacebookCallback callback=新建FacebookCallback(){
@凌驾
成功时公共无效(LoginResult LoginResult){
AccessToken AccessToken=loginResult.getAccessToken();
Profile Profile=Profile.getCurrentProfile();
显示消息(配置文件);
}
@凌驾
公开作废{
}
@凌驾
公共无效onError(FaceBook例外e){
}
};
公共空白片段(){
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
sdkinInitialize(getActivity().getApplicationContext());
callbackManager=callbackManager.Factory.create();
accessTokenTracker=新的accessTokenTracker(){
@凌驾
更改当前AccessToken时受保护的无效(AccessToken oldToken、AccessToken newToken){
}
};
profileTracker=新的profileTracker(){
@凌驾
更改当前配置文件时受保护的无效(配置文件旧配置文件、配置文件新配置文件){
显示消息(newProfile);
}
};
accessTokenTracker.startTracking();
profileTracker.startTracking();
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
返回充气机。充气(R.layout.fragment_blank,container,false);
}
@凌驾
已创建视图上的公共void(视图,捆绑保存状态){
super.onViewCreated(视图,savedInstanceState);
LoginButton LoginButton=(LoginButton)view.findViewById(R.id.login_按钮);
textView=(textView)view.findViewById(R.id.textView);
btnVoltar=(按钮)view.findViewById(R.id.btnVoltar);
setReadPermissions(“public_profile”);
setFragment(this);
registerCallback(callbackManager,callback);
}
@凌驾
ActivityResult上的公共void(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
callbackManager.onActivityResult(请求代码、结果代码、数据);
}
私有void显示消息(配置文件){
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());
}
}
@凌驾
公共void onStop(){
super.onStop();
accessTokenTracker.stopTracking();
profileTracker.stopTracking();
}
@凌驾
恢复时公开作废(){
super.onResume();
Profile Profile=Profile.getCurrentProfile();
ProfilePictureView profileImage=(ProfilePictureView)getActivity().findViewById(R.id.profilePicture);
profileImage.setProfileId(profile.getId());
显示消息(配置文件);
}
}
在我的主要活动中,我有一个指向facebook类的侧菜单。 它工作得很好。现在它开始显示这个错误。我不明白为什么


有什么帮助吗?

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

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


}







}

这意味着您尝试使用的配置文件对象为空。找出它为什么为空。不要仅仅因为它以前工作过就认为这不是问题。也许是因为某些情况,你没有真正遇到这个问题。

显然这是可行的。仍在测试中。 如果配置文件为空,则显示登录按钮

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.facebook.Profile.getId()' on a null object reference
[选中标记]:始终获取此错误。我们在这里寻找答案已经好几个小时了,但仍然不知道为什么会发生这种情况。
@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);
    }
}