我有一個應用程序使用Facebook發佈。我使用的是強制登錄的Facebook發佈nullpointer系統錯誤
mFacebook.authorize(this, PERMS, Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener());
代替
mFacebook.authorize(this, PERMS, new LoginDialogListener());
因爲與標準授權方法我無法發佈到Facebook。現在的崗位工作,但我得到這個錯誤:
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)
然而,儘管有錯誤,我沒有得到任何可見的錯誤(強制關閉)的應用程序:
這是Facebook的活動:
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();
}
});
}
}
}
錯誤指向username.setText("Welcome: " + name+"\n ID: "+id);
線,但是在stactrace我看到這兩個名稱和編號,通過此行打印:Log.d(TAG, "Response: " + response.toString());
爲什麼我得到這個錯誤?
編輯:
這是我通過點擊按鈕來調用Facebook發佈過程:
btnFB = (Button)findViewById(R.id.btnFB);
btnFB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setConnection();
getID(txtUserName, pbLogin);
}
});
的變量是這樣定義的,但目前還沒有分配的值(從教程):
TextView txtUserName;
ProgressBar pbLogin;
我在我的Facebook按鈕偵聽器中調用getId,請參閱編輯的問題 – erdomester 2012-08-07 21:25:08
我編輯了我的答案以反映您的更改。 – Eric 2012-08-07 21:39:13