Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用户首次登录Twitter身份验证期间的空指针-android_Android - Fatal编程技术网

用户首次登录Twitter身份验证期间的空指针-android

用户首次登录Twitter身份验证期间的空指针-android,android,Android,我使用twitter4j登录并发送文本。到目前为止,一切正常。我已经从设备上卸载了应用程序并重新安装了应用程序,但当我运行应用程序时,出现了空指针异常。下面是代码 prefs = PreferenceManager.getDefaultSharedPreferences(MyActivity.this); if(TwitterUtils.isAuthenticated(prefs)){//这就是我得到空指针的地方 public static boolean isAuthenticated(Sh

我使用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):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.readln(HttpURLConnectionImpl.java:1279)
09-06 11:38:48.138:E/AndroidRuntime(868):位于org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl$ChunkedInputStream.readChunkSize(HttpURLConnectionImpl.java:404)
09-06 11:38:48.138:E/AndroidRuntime(868):位于org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl$ChunkedInputStream。(HttpURLConnectionImpl.java:340)
09-06 11:38:48.138:E/AndroidRuntime(868):位于org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getContentStream(HttpURLConnectionImpl.java:1175)
09-06 11:38:48.138:E/AndroidRuntime(868):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequestInternal(HttpURLConnectionImpl.java:1754)
09-06 11:38:48.138:E/AndroidRuntime(868):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequest(HttpURLConnectionImpl.java:1649)
09-06 11:38:48.138:E/AndroidRuntime(868):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:1374)
09-06 11:38:48.138:E/AndroidRuntime(868):在twitter4j.internal.http.HttpResponseImpl.(HttpResponseImpl.java:45)
09-06 11:38:48.138:E/AndroidRuntime(868):在twitter4j.internal.http.httpclientmpl.request(httpclientmpl.java:178)
09-06 11:38:48.138:E/AndroidRuntime(868):在twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:75)
09-06 11:38:48.138:E/AndroidRuntime(868):位于twitter4j.internal.http.HttpClientWrapper.get(HttpClientWrapper.java:103)
09-06 11:38:48.138:E/AndroidRuntime(868):在twitter4j.Twitter.getAccountSettings(Twitter.java:1440)
09-06 11:38:48.138:E/AndroidRuntime(868):在com.android.twitter.TwitterUtils.isAuthenticated(TwitterUtils.java:24)

非常感谢您的建议。请提前感谢

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

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

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

您正在使用TwitterUtils类进行身份验证,该类会根据您的首选项检查身份验证,但在卸载过程中,这些首选项会被清除……因此请尝试使用Twitter.hasAccessToken()进行验证为了授权你的应用…浏览twitter4j文档…你可以在那里找到这个方法!!!是的,但是当我在日志中打印首选项时,它会显示“android.app.ContextImpl”$SharedPreferencesImpl@45fd4c40“这可能导致错误..通过按顺序检查jar文件和java build path的导出选项卡解决了问题..”。。
String token = prefs.getString(OAuth.OAUTH_TOKEN, null);
String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, null);