0

我尝试编写一个可以在 Twitter 上登录的应用程序。我的图书馆是 Twitter4j。onResume代码在调用该方法之前一直有效。这

exception(Unable to resume Activity...NullPointerException)在方法的这一行

onResume():

AccessToken acc = twitter.getOAuthAccessToken(requestToken,verifier);

但我不明白我的错误在哪里。有人可以帮助我吗??

public class MainActivity extends Activity {

    private Twitter twitter;
    private RequestToken requestToken;
    private static RequestToken re;
    final public static String CALLBACK_URL = "app://casa";
    private RequestToken priv = null;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button b = (Button) findViewById(R.id.button1);

        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                new updateTwitterStatus().execute();

            }
        });

    }

    class updateTwitterStatus extends AsyncTask<Void, Void, String> {

        @Override
        protected String doInBackground(Void... params) {
            String testStatus = "prova tweet ";

            ConfigurationBuilder cb = new ConfigurationBuilder();

            cb.setDebugEnabled(true)
                    .setOAuthConsumerKey("******")
                    .setOAuthConsumerSecret(
                            "*******");

            try {
                TwitterFactory tf = new TwitterFactory(cb.build());
                twitter = tf.getInstance();
                Log.i("bauu", "miao");
                requestToken = twitter.getOAuthRequestToken(CALLBACK_URL);
                String authUrl = requestToken.getAuthenticationURL();

                startActivity(new Intent(Intent.ACTION_VIEW,
                    Uri.parse(requestToken.getAuthenticationURL())));


                return authUrl;

            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {


        }

    }

    @Override
    public void onResume() {
        super.onResume();
        Uri uri = this.getIntent().getData();
        Log.i("callback funziona", "ciao");
        if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {
            final String verifier = uri.getQueryParameter("oauth_verifier");

            // c 
            try { // the next line is the error's line
                AccessToken acc = twitter.getOAuthAccessToken(requestToken,
                        verifier);
            } catch (TwitterException e) {

                e.printStackTrace();
            }



        }
    }
4

1 回答 1

1

Twitter您仅在用户单击按钮后创建实例,但尝试onResume()在它等于的方法中使用它null。阅读活动生命周期以避免此类错误https://developer.android.com/reference/android/app/Activity.html

于 2013-04-14T15:38:26.403 回答