简体   繁体   English

WebView中的网页不可用错误

[英]Web Page is not Available error in WebView

I am creating the application using the following tutorial 我正在使用以下教程创建应用程序

http://www.coderzheaven.com/2011/05/09/android-tabbars-example/ http://www.coderzheaven.com/2011/05/09/android-tabbars-example/

For every tab i have added one layout with webview. 对于每个标签,我都使用webview添加了一种布局。 I want to check Network availability for every tab changing, and i have redirected the page to errorActivity if net connection is not available. 我想检查每个选项卡更改的网络可用性,如果网络连接不可用,我已将页面重定向到errorActivity。

import android.app.TabActivity;
import android.content.*;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;

public class MyView extends TabActivity implements OnTabChangeListener
{   
    TabHost tabHost;
    String value;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Resources res = getResources(); 
        tabHost = (TabHost)findViewById(android.R.id.tabhost);


        TabSpec FirstTabSpec = tabHost.newTabSpec("tab1");
        TabSpec SecondTabSpec = tabHost.newTabSpec("tab2");
        TabSpec ThirdTabSpec = tabHost.newTabSpec("tab3");
        TabSpec FourthTabSpec = tabHost.newTabSpec("tab4");


        FirstTabSpec.setIndicator("Tab1",res.getDrawable(R.drawable.tab1)).setContent(new Intent(this,FirstView.class));
        secondTabSpec.setIndicator("Tab2 ",res.getDrawable(R.drawable.tab2)).setContent(new Intent(this,SecondView.class));
        ThirdTabSpec.setIndicator("Tab3",res.getDrawable(R.drawable.tab3)).setContent(new Intent(this,ThirdView.class));
        FourthTabSpec.setIndicator("Tab4",res.getDrawable(R.drawable.tab4)).setContent(new Intent(this,FourthView.class));


        tabHost.addTab(FirstTabSpec);
        tabHost.addTab(SecondTabSpec);
        tabHost.addTab(ThirdTabSpec);
        tabHost.addTab(FourthTabSpec);
        tabHost.setOnTabChangedListener(this);
    }

    public void onTabChanged(String tabId) 
    {   
        if(!IsNetworkAvaialble())
        {

                webview.stopLoading();
                Intent myIntent = new Intent((Activity)MyView.this, NetErrorPage.class);   
            myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            ((Activity)MyView.this).startActivity(myIntent);
            finish();
        }

    }

}

But clicking on the tab, page is redirected to the error page before that 2 or 3 seconds the "Web Page Not Available" is displaying. 但是,在该选项卡上单击后,在“ Web页面不可用”显示2或3秒钟之前,页面将重定向到错误页面。 How to stop this? 如何停止呢?

为访问网络添加此权限:

<uses-permission android:name="android.permission.INTERNET" />

I have solved this issue as below. 我已经解决了以下问题。

In OnTabchanged event, i have set one static boolean value as true. 在OnTabchanged事件中,我将一个静态布尔值设置为true。 In each activity class i have included loadurl method. 在每个活动类中,我都包含了loadurl方法。 Now i have changed this if the netconnection is failed and the static boolean value is not equal to true, then i have stopped the loading and displayed the alert. 现在,如果netconnection失败并且静态布尔值不等于true,我已经更改了此设置,那么我已经停止加载并显示了警报。 Otherwise loaded the url. 否则加载URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM