0

我已经使用 twitter4j 登录和发推文。到目前为止一切正常。我已经从设备上卸载了应用程序并重新安装了应用程序,但是当我运行应用程序时出现空指针异常。下面是代码

prefs = PreferenceManager.getDefaultSharedPreferences(MyActivity.this);

if (TwitterUtils.isAuthenticated(prefs)) {//这是我得到一个空指针的地方

public static boolean isAuthenticated(SharedPreferences prefs) {

    String token = prefs.getString(OAuth.OAUTH_TOKEN, "");
    String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");

    AccessToken a = new AccessToken(token,secret);
    Twitter twitter = new TwitterFactory().getInstance();
    twitter.setOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
    twitter.setOAuthAccessToken(a);

    try {
        twitter.getAccountSettings(); //error at this line 
        return true;
    } catch (TwitterException e) {
        return false;
    }
}

这是日志

09-06 11:38:48.138: E/AndroidRuntime(868): Caused by: java.lang.NullPointerException

09-06 12:59:49.943: E/AndroidRuntime(929): 在 libcore.net.UriCodec.encode(UriCodec.java:132)

09-06 11:38:48.138: E/AndroidRuntime(868):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.readln(HttpURLConnectionImpl.java:1279)
09-06 11:38:48.138: E/AndroidRuntime(868):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl$ChunkedInputStream.readChunkSize(HttpURLConnectionImpl.java:404)
09-06 11:38:48.138: E/AndroidRuntime(868):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl$ChunkedInputStream.<init>(HttpURLConnectionImpl.java:340)
09-06 11:38:48.138: E/AndroidRuntime(868):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getContentStream(HttpURLConnectionImpl.java:1175)
09-06 11:38:48.138: E/AndroidRuntime(868):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequestInternal(HttpURLConnectionImpl.java:1754)
09-06 11:38:48.138: E/AndroidRuntime(868):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequest(HttpURLConnectionImpl.java:1649)
09-06 11:38:48.138: E/AndroidRuntime(868):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:1374)
09-06 11:38:48.138: E/AndroidRuntime(868):  at twitter4j.internal.http.HttpResponseImpl.<init>(HttpResponseImpl.java:45)
09-06 11:38:48.138: E/AndroidRuntime(868):  at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:178)
09-06 11:38:48.138: E/AndroidRuntime(868):  at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:75)
09-06 11:38:48.138: E/AndroidRuntime(868):  at twitter4j.internal.http.HttpClientWrapper.get(HttpClientWrapper.java:103)
09-06 11:38:48.138: E/AndroidRuntime(868):  at twitter4j.Twitter.getAccountSettings(Twitter.java:1440)
09-06 11:38:48.138: E/AndroidRuntime(868):  at com.android.twitter.TwitterUtils.isAuthenticated(TwitterUtils.java:24)

任何建议都非常感谢。在此先感谢

4

1 回答 1

0

您也可以尝试将这两个值设为null

String token = prefs.getString(OAuth.OAUTH_TOKEN, null);
String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, null);

这是一个更简单的解决方案,首先尝试!

于 2012-09-06T06:41:43.003 回答