我尝试使用twitter4j连接到Twitter,但显示错误如标题所述。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
twitterConsumerKey = getIntent().getStringExtra(TWITTER_CONSUMER_KEY);
twitterConsumerSecret = getIntent().getStringExtra(TWITTER_CONSUMER_SECRET);
if(twitterConsumerKey == null || twitterConsumerSecret == null){
Log.e(Constants.TAG, "ERROR: Consumer Key and Consumer Secret required!");
Login.this.setResult(TWITTER_LOGIN_RESULT_CODE_FAILURE);
Login.this.finish();
}
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Please wait...");
mProgressDialog.setCancelable(false);
mProgressDialog.setCanceledOnTouchOutside(false);
mProgressDialog.show();
twitterLoginWebView = (WebView)findViewById(R.id.twitter_login_web_view);
twitterLoginWebView.setWebViewClient( new WebViewClient()
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if( url.contains(Constants.TWITTER_CALLBACK_URL))
{
Uri uri = Uri.parse(url);
Login.this.saveAccessTokenAndFinish(uri);
return true;
}
return false;
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
if(mProgressDialog != null) mProgressDialog.dismiss();
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
if(mProgressDialog != null) mProgressDialog.show();
}
});
Log.d(Constants.TAG, "ASK OAUTH");
askOAuth();
}
这是LogCat:
FATAL EXCEPTION: main
android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@41f93f30 is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:559)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:269)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:281)
at com.moviecollection.Login$1.onPageStarted(Login.java:93)
at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:305)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5295)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
at dalvik.system.NativeStart.main(Native Method)
错误行为if(mProgressDialog != null) mProgressDialog.show();
我尝试了Unable to add window — token android.os.BinderProxy is not valid; is your activity running? 中提到的方法。但是对于我的Android版本来说太老了。