2016-03-03 99 views
-4
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類。Facebook返回null

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類的側面菜單。 它工作正常。現在它開始顯示這個錯誤。我不明白爲什麼。

任何幫助嗎?

+0

[checkmark]:https://uploaddeimagens.com.br/imagens/faceerro-png--2總是得到這個錯誤。在這裏尋找答案几個小時,但仍然不能得到爲什麼發生這種情況。 –

回答

2

你應該嘗試閱讀你的錯誤:

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

這意味着你要使用的配置文件對象爲空。找出爲什麼它是空的。不要以爲它以前工作,這不是一個問題。也許這是一個你沒有遇到的問題,因爲某些條件。

+0

感謝沒有幫助 –

0

顯然這個工程。仍在測試。 如果配置文件爲空,則顯示登錄按鈕。

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