twitter getOAuthAccessToken给出空指针异常

时间:2014-03-14 05:48:36

标签: android twitter

一旦代码中的这一行执行

,我就会得到空指针异常
accessToken =twitter.getOAuthAccessToken(LoginThread.requestToken, verifier);

Login.Thread和verifier参数看起来很好。

这是我的课程,我收到错误

public class UserClass extends Activity {


    // Preference Constants
            static String PREFERENCE_NAME = "twitter_oauth";
            static final String PREF_KEY_OAUTH_TOKEN = "oauth_token";
            static final String PREF_KEY_OAUTH_SECRET = "oauth_token_secret";
            static final String URL_TWITTER_OAUTH_VERIFIER = "oauth_verifier";
            static final String PREF_KEY_TWITTER_LOGIN = "isTwitterLogedIn";

            static final String TWITTER_CALLBACK_URL = "oauth://t4jsample";
            static final String URL_TWITTER_AUTH = "auth_url";
            static final String URL_TWITTER_OAUTH_TOKEN = "oauth_token";
            static Twitter twitter;


          // Get the access token
                AccessToken accessToken;

    // Shared Preferences
    public static SharedPreferences mSharedPreferences;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         setContentView(R.layout.user_page);


        // Shared Preferences
        mSharedPreferences = getApplicationContext().getSharedPreferences("MyPref", 0); 



         setLoginKeysClass obj = new setLoginKeysClass();
         obj.execute();         

        }

public class setLoginKeysClass extends AsyncTask<Void, Void, AccessToken>
    {

        @Override
        protected AccessToken doInBackground(Void... params) {
            // TODO Auto-generated method stub

              Uri uri = getIntent().getData();
             if (uri != null && uri.toString().startsWith(TWITTER_CALLBACK_URL)) {

                    // oAuth verifier
                    String verifier = uri.getQueryParameter(URL_TWITTER_OAUTH_VERIFIER);


                    try {
                    accessToken =twitter.getOAuthAccessToken(LoginThread.requestToken, verifier); // LoginThread is a class

                    // Shared Preferences
                    Editor e = mSharedPreferences.edit();

                    // After getting access token, access token secret
                    // store them in application preferences
                    //e.putString(PREF_KEY_OAUTH_TOKEN, accessToken.getToken());
                    //e.putString(PREF_KEY_OAUTH_SECRET,accessToken.getTokenSecret());
                    // Store login status - true
                    e.putBoolean(PREF_KEY_TWITTER_LOGIN, true);
                    e.commit(); // save changes
                    return accessToken;
                    }
                     catch (Exception e) {
                            // TODO Auto-generated catch block
                        //  e.printStackTrace();
                            return null;
                            }

                    }
            return null;

        }

    }
}

这是 LogCat

03-13 10:04:44.623: D/AndroidRuntime(17352): Shutting down VM
03-13 10:04:44.623: W/dalvikvm(17352): threadid=1: thread exiting with uncaught exception (group=0x41a4a700)
03-13 10:04:44.638: E/AndroidRuntime(17352): FATAL EXCEPTION: main
03-13 10:04:44.638: E/AndroidRuntime(17352): java.lang.IllegalStateException: Could not execute method of the activity
03-13 10:04:44.638: E/AndroidRuntime(17352):    at android.view.View$1.onClick(View.java:3838)
03-13 10:04:44.638: E/AndroidRuntime(17352):    at android.view.View.performClick(View.java:4475)
03-13 10:04:44.638: E/AndroidRuntime(17352):    at android.view.View$PerformClick.run(View.java:18784)
03-13 10:04:44.638: E/AndroidRuntime(17352):    at android.os.Handler.handleCallback(Handler.java:730)
03-13 10:04:44.638: E/AndroidRuntime(17352):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-13 10:04:44.638: E/AndroidRuntime(17352):    at android.os.Looper.loop(Looper.java:137)
03-13 10:04:44.638: E/AndroidRuntime(17352):    at android.app.ActivityThread.main(ActivityThread.java:5414)
03-13 10:04:44.638: E/AndroidRuntime(17352):    at java.lang.reflect.Method.invokeNative(Native Method)
03-13 10:04:44.638: E/AndroidRuntime(17352):    at java.lang.reflect.Method.invoke(Method.java:525)
03-13 10:04:44.638: E/AndroidRuntime(17352):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
03-13 10:04:44.638: E/AndroidRuntime(17352):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
03-13 10:04:44.638: E/AndroidRuntime(17352):    at dalvik.system.NativeStart.main(Native Method)
03-13 10:04:44.638: E/AndroidRuntime(17352): Caused by: java.lang.reflect.InvocationTargetException
03-13 10:04:44.638: E/AndroidRuntime(17352):    at java.lang.reflect.Method.invokeNative(Native Method)
03-13 10:04:44.638: E/AndroidRuntime(17352):    at java.lang.reflect.Method.invoke(Method.java:525)
03-13 10:04:44.638: E/AndroidRuntime(17352):    at android.view.View$1.onClick(View.java:3833)
03-13 10:04:44.638: E/AndroidRuntime(17352):    ... 11 more
03-13 10:04:44.638: E/AndroidRuntime(17352): Caused by: java.lang.IllegalStateException: Cannot execute task: the task has already been executed (a task can be executed only once)
03-13 10:04:44.638: E/AndroidRuntime(17352):    at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:578)
03-13 10:04:44.638: E/AndroidRuntime(17352):    at android.os.AsyncTask.execute(AsyncTask.java:534)
03-13 10:04:44.638: E/AndroidRuntime(17352):    at pack.locationinfo.MainActivity.TwitterLogin(MainActivity.java:75)
03-13 10:04:44.638: E/AndroidRuntime(17352):    ... 14 more

1 个答案:

答案 0 :(得分:0)

这是答案.. 我没有初始化twitter对象所以修改了我的代码

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         setContentView(R.layout.user_page);




            ConfigurationBuilder builder = new ConfigurationBuilder();
            builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
            builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
            Configuration configuration = builder.build();

            TwitterFactory factory = new TwitterFactory(configuration);
            twitter = factory.getInstance();
        // Shared Preferences
        mSharedPreferences = getApplicationContext().getSharedPreferences("MyPref", 0); 

         setLoginKeysClass obj = new setLoginKeysClass();
         obj.execute();
                  .
                  .
                  .
}

现在一切正常