1
When trying to implement the facebook logout, in another activity
, other than the one who logs in, I see the following message:
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 logout? I put the LoginManager.getInstance().logOut();
within the event of a button (Button)
common.
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" />
Opa meu xm, o id é estel:
 <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" />
– Bob
Is the loginbutton object null? if you are not trying to call Facebooksdk.sdkInitialize(context); from setOnClickListener
– Luciano Marqueto
I couldn’t debug it, actually. The flow is as follows, Loginactivity -> No onSuccess, on the login Activity, I call the Mainactivity, I’m trying to put the Logout on this Activity, and before it can be called it gives this problem.
– Bob
@Bob puts the rest of the code of the two activities
– Luciano Marqueto
the code is now in the body of the question, with a comment on the issue. Thank you!
– Bob
I believe that if you use a common <Button> button it should work the way you did, but I think if you remove the . setOnClickListener() of the facebook button it would already work automatically.
– Luciano Marqueto