0

我有一个使用 facebook 帖子的应用程序。我正在使用强制登录

   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)

但是,尽管出现错误,我在应用程序中没有看到明显的错误(强制关闭):

这是脸书活动:

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 中,我看到了由该行打印的名称和 id: 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;
4

1 回答 1

0

正如你上面所说,当getID(txtUserName, pbLogin);被调用时,两者txtUserNamepbLogin都是nullNullPointerException当您尝试访问它们时,这将导致您的。

无论教程说什么,您都需要分配这些字段。完成时您必须拥有TextViewProgressBar对象IDRequestListener,否则您的程序将强制关闭。

因此,要解决此问题,您必须创建您的TextViewProgressBar对象(通过 XML 或通过代码)。我怀疑您正在使用的教程确实说要这样做。我猜你应该findViewById在你分配的行上方定义它们btnFB

于 2012-08-06T20:58:02.027 回答