When trying to implement facebook logout, in another activity
, other than the one that logs in, the following message appears:
Error
MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener (android.view.View $ OnClickListener) ' on a null object reference
I used the native Facebook button, with the implementations of the documentation.
How can I log out? I put LoginManager.getInstance().logOut();
inside the event of a% common button%.
Main:
public class MainActivity extends AppCompatActivity{
private Button button;
private LoginActivity login;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main);
// Solicita as permissões
String[] permissoes = new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.SEND_SMS,
Manifest.permission.RECEIVE_SMS,
Manifest.permission.INTERNET,
};
Permissions.validate(this, 0, permissoes);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
LoginButton loginbutton = (LoginButton)findViewById(R.id.login_button);
loginbutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
LoginManager.getInstance().logOut();
}
});
}
}
Login: ...
callbackManager = CallbackManager.Factory.create();
// Button fb = (Button) findViewById(R.id.fb);
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
//assert loginButton != null;
loginButton.setReadPermissions("public_profile", "email", "user_friends", "user_birthday");
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
String accessToken = loginResult.getAccessToken()
.getToken();
Log.i("accessToken", accessToken);
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {@Override
public void onCompleted(JSONObject object,
GraphResponse response) {
Log.i("LoginActivity",
response.toString());
try {
id = object.getString("id");
try {
URL profile_pic = new URL(
"http://graph.facebook.com/" + id + "/picture?type=large");
Log.i("profile_pic",
profile_pic + "");
} catch (MalformedURLException e) {
e.printStackTrace();
}
name = object.getString("name");
email = object.getString("email");
gender = object.getString("gender");
birthday = object.getString("birthday");
} catch (JSONException e) {
e.printStackTrace();
}
}
});
saveSharedPreferences.loginPreferencesFb(email);
Bundle parameters = new Bundle();
parameters.putString("fields",
"id,name,email,gender,birthday");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
// App code
}
@Override
public void onError(FacebookException exception) {
// App code
}
});
startNewIntent();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
xml
<com.facebook.login.widget.LoginButton
android:id="@+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp" />