WebView.loadUrl(URL)什么都不做 [英] WebView.loadUrl(url) does nothing

查看:300
本文介绍了WebView.loadUrl(URL)什么都不做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近写了一个简单的Twitter的应用程序为Android学习Twitter的API和OAuth。

的绳索

应用程序的主要活动就是要求输入用户名跟随。然后,它调用该处理OAuth和放大器的另一个活动; Twitter的API调用。这将用户重定向到一个授权页面,然后在用户完成后返回到应用程序。

它曾经工作得很好,但现在由于某种原因,当我打电话webview.loadUrl(authorizationURL),没有任何反应。我从来没有改变任何会影响到的WebView东西虽然...这里是我的code:

  @覆盖
公共无效onResume(){
    super.onResume();
    尝试{        如果(weNeedCredentials()){
            obtainCredentials();
        }        遵循(mUsername);    }赶上(OAuthException OAE){
        //省略
    }
}
私人布尔weNeedCredentials(){
    //假定该方法返回true
}
私人无效obtainCredentials(){
    最后令牌requestToken = mOauthService.getRequestToken();
    字符串authUrl = mOauthService.getAuthorizationUrl(requestToken);
    //我核实,authUrl是正确的URL(和!= NULL)    最终的WebView oauthView =(的WebView)findViewById(R.id.oauthview);    oauthView.setWebViewClient(新WebViewClient(){
        @覆盖
        公共布尔shouldOverrideUrlLoading(的WebView视图,字符串URL){
            如果(someCondition){
                oauthView.setVisibility(View.GONE);                doOtherStuff();                返回true;
            }
            返回super.shouldOverrideUrlLoading(查看,网址);
        }
    });    oauthView.getSettings()setJavaScriptEnabled(真)。
    oauthView.loadUrl(authUrl);}

下面是我的布局xml文件:

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直>    <的WebView
        机器人:ID =@ + ID / oauthview
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
    />< / LinearLayout中>

和我也列入清单正确的Internet的权限。

一个WebViewCoreThread正在运行的应用程序的生命,以及以后WebViewWorkerThread在持久性有机污染物,但没有永远的WebView来了(甚至不是一个白色屏幕)。该应用程序不会阻塞无论是。它继续运行,如果使用loadURL()行被简单注释。

我我的手机(Droid的X2),以及一个模拟器,两者具有相同的结果上进行测试。任何帮助将大大AP preciated。

在此先感谢!


解决方案

  

它继续运行,如果使用loadURL()行被简单注释。


这将始终是继续运行,如果在使用loadURL()行被简单地注释掉。 使用loadURL()是异步的,不会阻塞。

即兴,或者:


  • weNeedCredentials()将返回

  • 遵循()正在取代UI,或

  • Twitter正在做一个重定向,和 someCondition 真正,所以你做了的WebView GONE 马上

  • 有与URL的问题,您加载

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!

解决方案

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

这篇关于WebView.loadUrl(URL)什么都不做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆