I have an application using facebook post. I am using a force login
mFacebook.authorize(this, PERMS, Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener());
instead of
mFacebook.authorize(this, PERMS, new LoginDialogListener());
because with the standard authorize method I couldn’t post to facebook. Now the post works, but I get this error:
08-06 20:35:52.814: W/System.err(682): java.lang.NullPointerException
08-06 20:35:52.824: W/System.err(682): at com.b2creativedesigns.b2lovecalculator.FacebookActivity$IDRequestListener$1.run(FacebookActivity.java:147)
08-06 20:35:52.824: W/System.err(682): at android.os.Handler.handleCallback(Handler.java:587)
08-06 20:35:52.824: W/System.err(682): at android.os.Handler.dispatchMessage(Handler.java:92)
08-06 20:35:52.824: W/System.err(682): at android.os.Looper.loop(Looper.java:123)
08-06 20:35:52.824: W/System.err(682): at android.app.ActivityThread.main(ActivityThread.java:4363)
08-06 20:35:52.824: W/System.err(682): at java.lang.reflect.Method.invokeNative(Native Method)
08-06 20:35:52.824: W/System.err(682): at java.lang.reflect.Method.invoke(Method.java:521)
08-06 20:35:52.824: W/System.err(682): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-06 20:35:52.824: W/System.err(682): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-06 20:35:52.824: W/System.err(682): at dalvik.system.NativeStart.main(Native Method)
However, despite the error, I get no visible error (force close) in the app:
This is the facebook activity:
public abstract class FacebookActivity extends Activity {
public static final String TAG = "FACEBOOK";
private Facebook mFacebook;
public static final String APP_ID = "blablabla"; //the API Key for your Facebook APPs
private AsyncFacebookRunner mAsyncRunner;
private static final String[] PERMS = new String[] { "publish_stream" };
private SharedPreferences sharedPrefs;
private Context mContext;
private TextView username;
private ProgressBar pb;
public void setConnection() {
mContext = this;
mFacebook = new Facebook(APP_ID);
mAsyncRunner = new AsyncFacebookRunner(mFacebook);
}
public void getID(TextView txtUserName, ProgressBar progbar) {
username = txtUserName;
pb = progbar;
if (isSession()) {
Log.d(TAG, "sessionValid");
mAsyncRunner.request("me", new IDRequestListener());
} else {
// no logged in, so relogin
Log.d(TAG, "sessionNOTValid, relogin");
//mFacebook.authorize(this, PERMS, new LoginDialogListener());
mFacebook.authorize(this, PERMS, Facebook.FORCE_DIALOG_AUTH,
new LoginDialogListener());
}
}
public void logout() {
mContext = this;
try {
mFacebook = new Facebook(APP_ID);
mFacebook.logout(mContext);
//mFacebook.authorize(this, PERMS, -1, new LoginDialogListener());
Log.d(TAG, "ssucesfull logout");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public boolean isSession() {
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
String access_token = sharedPrefs.getString("access_token", "x");
Long expires = sharedPrefs.getLong("access_expires", -1);
Log.d(TAG, access_token);
if (access_token != null && expires != -1) {
mFacebook.setAccessToken(access_token);
mFacebook.setAccessExpires(expires);
}
return mFacebook.isSessionValid();
}
private class LoginDialogListener implements DialogListener {
@Override
public void onComplete(Bundle values) {
Log.d(TAG, "LoginONComplete");
String token = mFacebook.getAccessToken();
long token_expires = mFacebook.getAccessExpires();
Log.d(TAG, "AccessToken: " + token);
Log.d(TAG, "AccessExpires: " + token_expires);
sharedPrefs = PreferenceManager
.getDefaultSharedPreferences(mContext);
sharedPrefs.edit().putLong("access_expires", token_expires)
.commit();
sharedPrefs.edit().putString("access_token", token).commit();
mAsyncRunner.request("me", new IDRequestListener());
}
@Override
public void onFacebookError(FacebookError e) {
Log.d(TAG, "FacebookError: " + e.getMessage());
}
@Override
public void onError(DialogError e) {
Log.d(TAG, "Error: " + e.getMessage());
}
@Override
public void onCancel() {
Log.d(TAG, "OnCancel");
}
}
private class IDRequestListener implements RequestListener {
@Override
public void onComplete(String response, Object state) {
try {
Log.d(TAG, "IDRequestONComplete");
Log.d(TAG, "Response: " + response.toString());
JSONObject json = Util.parseJson(response);
final String id = json.getString("id");
final String name = json.getString("name");
FacebookActivity.this.runOnUiThread(new Runnable() {
public void run() {
try{
username.setText("Welcome: " + name+"\n ID: "+id); //the error points to this line
pb.setVisibility(ProgressBar.GONE);
}catch(Exception e){
e.printStackTrace();
}
}
});
postOnWall("The possibility of love between " + GlobalVars.getName1() + " and " + GlobalVars.getName2() + " is " + GlobalVars.getPercent() + "%!");
} catch (JSONException e) {
Log.d(TAG, "JSONException: " + e.getMessage());
} catch (FacebookError e) {
Log.d(TAG, "FacebookError: " + e.getMessage());
}
}
@Override
public void onIOException(IOException e, Object state) {
Log.d(TAG, "IOException: " + e.getMessage());
}
@Override
public void onFileNotFoundException(FileNotFoundException e,
Object state) {
Log.d(TAG, "FileNotFoundException: " + e.getMessage());
}
@Override
public void onMalformedURLException(MalformedURLException e,
Object state) {
Log.d(TAG, "MalformedURLException: " + e.getMessage());
}
@Override
public void onFacebookError(FacebookError e, Object state) {
Log.d(TAG, "FacebookError: " + e.getMessage());
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
mFacebook.authorizeCallback(requestCode, resultCode, data);
}
public void postOnWall(String msg) {
Log.d("Tests graph API %%%%%$$$$%%%", msg);
try {
String response = mFacebook.request("me");
Bundle parameters = new Bundle();
parameters.putString("message", msg);
parameters.putString("picture", "https://lh3.ggpht.com/f79UCpnLisZxO2P2C43f55YLvFpNco_cTcC-t9Ck-Qmqe5jwKbfnUvCh5N6-Te-mOw=w124");
parameters.putString("link", "https://play.google.com/store/apps/details?id=com.b2creativedesigns.b2lovecalculator");
response = mFacebook.request("me/feed", parameters,"POST");
Log.d("Tests", "got response: " + response);
if (response == null || response.equals("") ||
response.equals("false")) {
Log.v("Error", "Blank response");
}
} catch(Exception e) {
e.printStackTrace();
}
finally {
FacebookActivity.this.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(FacebookActivity.this, "Posted to Facebook", Toast.LENGTH_SHORT).show();
}
});
}
}
}
The error points to the username.setText("Welcome: " + name+"\n ID: "+id); line, however in the stactrace I see both the name and id, printed by this line: Log.d(TAG, "Response: " + response.toString());
Why do I get this error?
Edit:
This is how I call the facebook post process by clicking on a button:
btnFB = (Button)findViewById(R.id.btnFB);
btnFB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setConnection();
getID(txtUserName, pbLogin);
}
});
The variables are defined like this, but there are no value assigned (from the tutorial):
TextView txtUserName;
ProgressBar pbLogin;
As you stated above, when
getID(txtUserName, pbLogin);is called, bothtxtUserNameandpbLoginarenull. This will cause yourNullPointerExceptionwhen you try to access them.Regardless of what the tutorial says, you need to have these fields assigned. You must have
TextViewandProgressBarobjects for whenIDRequestListeneris complete, or else your program will force close.So, to fix this, you must create your
TextViewandProgressBarobjects (through XML or through code). I would suspect that the tutorial you are using does say to do this. I would guess that you are supposed to define them usingfindViewByIdabove the line where you assignbtnFB.