简体   繁体   English

activity_main.xml未在Android中加载

[英]activity_main.xml is not loading in android

In the following code, main xml file activity_main.xml is not used. 在以下代码中,未使用主xml文件activity_main.xml。 What should be modified if I want to use it. 如果要使用它应该修改什么。 The main intention is to load the webview in background while showing splashscreen on the front. 主要目的是在正面显示启动画面时在后台加载Web视图。 Is my approach correct? 我的方法正确吗? If I want to use AsyncTask to load webview while showing splashscreen, what should I do? 如果要在显示初始屏幕时使用AsyncTask加载Web视图,该怎么办? My app is loading everytime while changing the orientation. 每次更改方向时,我的应用都会加载。 what should I do in order to fix it? 我应该怎么做才能修复它?

public class MainActivity extends Activity {
WebView webview;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_splash);

    webview = new WebView(MainActivity.this);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setLoadsImagesAutomatically(true);
    webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    webview.getSettings().setAppCacheEnabled(false);
    webview.loadUrl("http://www.nricabs.com");
    webview.setWebViewClient(new WebViewClient(){
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            if(url.startsWith("tel:")) { 
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
                startActivity(intent); 
                return true;
            }
            return false;

        }
        public void onPageFinished(WebView view,String url){
            super.onPageFinished(view, url);
            setContentView(webview);
        }
    }
            );

}
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(event.getAction() == KeyEvent.ACTION_DOWN){
        switch(keyCode)
        {
        case KeyEvent.KEYCODE_BACK:
            if(webview.canGoBack()){
                webview.goBack();
            }else{
                finish();
            }
            return true;
        }

    }
    return super.onKeyDown(keyCode, event);
}


}

About your webview question, im pretty sure that it already loads on a separate thread so you dont need to use AsyncTask. 关于您的webview问题,我非常确定它已经加载在单独的线程上,因此您无需使用AsyncTask。

About the reload on orientation change, you can restore the state via SavedInstanceState as shown here . 关于取向变化重装,可以如图所示通过SavedInstanceState恢复状态这里

Or, you can disable it as shown here 或者,您可以禁用它,如这里

About using a xml layout just call 关于使用xml布局,只需调用

 setContentView(R.layout.activity_main);

Hope this helps. 希望这可以帮助。

After the page has finished loading you can open a new intent to a new activity and there you can use a different layout. 页面加载完成后,您可以为新活动打开新的意图,然后可以使用其他布局。

Another approach could be to have one layout for both (main and progress) and then hide/show the layouts when the page has finished loading. 另一种方法可能是为(主和进度)都有一个布局,然后在页面加载完成后隐藏/显示布局。

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

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