WebView.loadUrl(url) does nothing

五迷三道 提交于 2020-01-02 09:58:54

问题


I recently wrote up a simple Twitter app for Android to learn the ropes of the Twitter API and OAuth.

The app's main activity simply asks for a username to follow. It then calls another activity which handles the OAuth & Twitter API calls. It redirects the user to an authorization page, which then returns to the app after the user finishes.

It used to work just fine, but now for some reason when I call webview.loadUrl(authorizationURL), NOTHING happens. I never changed anything that would affect the WebView stuff though... Here's my code:

@Override
public void onResume() {
    super.onResume();
    try {

        if(weNeedCredentials()) {
            obtainCredentials();
        }

        follow(mUsername);

    } catch (OAuthException oae) {
        // omitted
    }
}


private boolean weNeedCredentials() {
    // assume the method returns true
}


private void obtainCredentials() {
    final Token requestToken = mOauthService.getRequestToken();
    String authUrl = mOauthService.getAuthorizationUrl(requestToken);
    // I verified that authUrl is the correct url (and != null)

    final WebView oauthView = (WebView) findViewById(R.id.oauthview);

    oauthView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if(someCondition) {
                oauthView.setVisibility(View.GONE);

                doOtherStuff();

                return true;
            }
            return super.shouldOverrideUrlLoading(view, url);
        }
    });

    oauthView.getSettings().setJavaScriptEnabled(true);
    oauthView.loadUrl(authUrl);

}

Here's my layout xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <WebView 
        android:id="@+id/oauthview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    />

</LinearLayout>

and also I included the proper Internet permissions in the Manifest.

A WebViewCoreThread is running for the life of the app, and a WebViewWorkerThread pops in later, but no WebView ever comes up (not even a white screen). The app never blocks either. It continues running as if the loadUrl() line were simply commented out.

I've tested on my phone (Droid X2) as well as an emulator, both with the same results. Any help would be greatly appreciated.

Thanks in advance!


回答1:


It continues running as if the loadUrl() line were simply commented out.

It will always "continue running as if the loadUrl() line were simply commented out". loadUrl() is asynchronous and does not block.

Off the cuff, either:

  • weNeedCredentials() is returning false, or
  • follow() is replacing the UI, or
  • Twitter is doing a redirect, and someCondition is true, so you are making the WebView be GONE right away
  • there are issues with the URL that you are loading


来源:https://stackoverflow.com/questions/6771074/webview-loadurlurl-does-nothing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!