我想在android.as中顯示一個webview在asynctask中,我嘗試授權我的twitter應用程序。但是,當我點擊登錄按鈕時,它將我重定向到瀏覽器。所以,我想在我的activity上顯示一個webview。下面的代碼,但我的應用程序崩潰。 那麼,我該如何做到這一點?如何在Android的asynctask中顯示webview?
我的代碼登錄到Twitter帳戶 -
private class TwitterLoginTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
// Check if already logged in
if (!isTwitterLoggedInAlready()) {
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();
try {
requestToken = twitter.getOAuthRequestToken(TWITTER_CALLBACK_URL);
wv.loadUrl(requestToken.getAuthorizationURL());
setContentView(wv);
setContentView(R.layout.activity_main);
wv.setWebViewClient(new WebViewClient());
wv.loadUrl(requestToken.getAuthenticationURL());
wv.getSettings().setJavaScriptEnabled(true);
//redirects to browser
//MainActivity.this.startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(requestToken.getAuthenticationURL())));
} catch (TwitterException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
if (isTwitterLoggedInAlready()) {
// User already logged into twitter
Toast.makeText(MainActivity.this, "Already Logged into Twitter", Toast.LENGTH_LONG).show();
}
}
}
你沒注意到''setContentView(wv);'在'doInBackground()'中嗎?首要的是這個問題。 –
這是正確的,先生。 – overbet13
我設置了wv.setWebViewClient(新的WebViewClient()); wv.loadUrl(requestToken.getAuthenticationURL()); ()。setJavaScriptEnabled(true); setContentView(wv);在postExecute中刪除了setContentView(R.layout.activity_main);現在它的工作正常。但在webview中點擊「Authorize App」按鈕後,它會給出錯誤,因爲「標記null不適用於應用程序」,這意味着什麼? – user3851588