预加载webview然后切换到其活动

时间:2013-05-13 19:38:03

标签: java android

我是android和java编程的新手。我试图在启动时加载启动画面,然后在后台加载位于不同活动的webview,一旦webview页面准备就绪,我想将活动切换到具有webview的活动

package com.example.troyalert;

 import android.app.Activity;
 import android.os.AsyncTask;
 import android.os.Bundle;
 import android.view.Menu;
 import android.webkit.WebSettings;
 import android.webkit.WebView;
 import android.webkit.WebViewClient;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    new DownloadWebpageTask().execute();
}

private class DownloadWebpageTask extends AsyncTask<Void, Void, Void> { 
    @Override
    protected Void doInBackground(Void... arg0) {
        WebView myWebView = (WebView) findViewById(R.id.webview);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.loadUrl("http://enigmamaster.com/raul/locator3/");
        myWebView.setWebViewClient(new WebViewClient(){
         @Override
         public void onPageFinished(WebView view, String url){
            setContentView(R.layout.activity_main);
        }
        });
        return null;
    }
 }



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

应用程序在doInBackground()崩溃,我很确定它是由于它没有在启动画面上看到webview视图。我如何使这项工作,以便加载webview中的页面后,它切换到activity_main布局。 想要这个的原因是我不希望人们在等待页面加载时出现白屏

05-13 14:34:37.174: E/Trace(1070): error opening trace file: No such file or directory (2)

05-13 14:34:37.814: W/dalvikvm(1070): threadid=11: thread exiting with uncaught exception (group=0x40a13300)

05-13 14:34:37.894: D/gralloc_goldfish(1070): Emulator without GPU emulation detected.

05-13 14:34:38.474: E/AndroidRuntime(1070): FATAL EXCEPTION: AsyncTask #1

05-13 14:34:38.474: E/AndroidRuntime(1070): java.lang.RuntimeException: An error occured while executing doInBackground()

05-13 14:34:38.474: E/AndroidRuntime(1070):     at android.os.AsyncTask$3.done(AsyncTask.java:299)

05-13 14:34:38.474: E/AndroidRuntime(1070):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)

05-13 14:34:38.474: E/AndroidRuntime(1070):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)

05-13 14:34:38.474: E/AndroidRuntime(1070):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)

05-13 14:34:38.474: E/AndroidRuntime(1070):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)

05-13 14:34:38.474: E/AndroidRuntime(1070):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)

05-13 14:34:38.474: E/AndroidRuntime(1070):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)

05-13 14:34:38.474: E/AndroidRuntime(1070):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)

05-13 14:34:38.474: E/AndroidRuntime(1070):     at java.lang.Thread.run(Thread.java:856)

05-13 14:34:38.474: E/AndroidRuntime(1070): Caused by: java.lang.NullPointerException

05-13 14:34:38.474: E/AndroidRuntime(1070):     at com.example.troyalert.MainActivity$DownloadWebpageTask.doInBackground(MainActivity.java:24)

05-13 14:34:38.474: E/AndroidRuntime(1070):     at com.example.troyalert.MainActivity$DownloadWebpageTask.doInBackground(MainActivity.java:1)

05-13 14:34:38.474: E/AndroidRuntime(1070):     at android.os.AsyncTask$2.call(AsyncTask.java:287)

05-13 14:34:38.474: E/AndroidRuntime(1070):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

05-13 14:34:38.474: E/AndroidRuntime(1070):     ... 5 more

05-13 14:39:38.700: I/Process(1070): Sending signal. PID: 1070 SIG: 9

1 个答案:

答案 0 :(得分:2)

您不应该使用AsyncTask在WebView中加载URL。必须在UI线程中调用此方法。 我建议碎片是你的任务的最佳选择。您可以创建2个片段 - 首先使用splash,第二个使用WebView。在您的活动的onCreate(...)方法中,您可以显示启动画面并启动加载网页。加载完成后 - 只需删除启动画面并显示WebView片段。

这里有一些工作代码:

<强> MainActivity.java

    package com.melnykov.testfragment;

    import android.app.Activity;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentTransaction;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;

    public class MainActivity extends FragmentActivity implements WebViewListener {

        private SplashFragment mSplashFragment;
        private WebViewFragment mWebViewFragment;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initFragments();
        }

        private void initFragments() {
            mSplashFragment = new SplashFragment();
            mWebViewFragment = new WebViewFragment();
            showSplash();
            initWebViewLoad();
        }

        private void showSplash() {
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.add(R.id.fragment_container, mSplashFragment);
            ft.commit();
        }

        private void showWebViewFragment() {
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.remove(mSplashFragment);
            ft.show(mWebViewFragment);
            ft.commit();
        }

        private void initWebViewLoad() {
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.add(R.id.fragment_container, mWebViewFragment);
            ft.hide(mWebViewFragment);
            ft.commit();
        }

        @Override
        public void onPageFinished() {
            showWebViewFragment();
        }

        public static class SplashFragment extends Fragment {
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                View view = inflater.inflate(R.layout.fragment_splash, container, false);
                return view;
            }
        }

        public static class WebViewFragment extends Fragment {

            private WebView mWebView;
            private WebViewListener mListener;

            @Override
            public void onAttach(Activity activity) {
                super.onAttach(activity);
                mListener = (WebViewListener) activity;
            }

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                View view = inflater.inflate(R.layout.fragment_web_view, container, false);
                mWebView = (WebView) view.findViewById(R.id.web_view);
                return view;
            };

            @Override
            public void onActivityCreated(Bundle savedInstanceState) {
                super.onActivityCreated(savedInstanceState);

                WebSettings webSettings = mWebView.getSettings();
                webSettings.setJavaScriptEnabled(true);
                mWebView.loadUrl("http://google.com");
                mWebView.setWebViewClient(new WebViewClient() {
                    @Override
                    public void onPageFinished(WebView view, String url) {
                        mListener.onPageFinished();
                    }
                });
            }
        }
    }

<强> WebViewListener.java

package com.melnykov.testfragment;

public interface WebViewListener {
    public void onPageFinished();
}

<强> activity_main.xml中

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

</FrameLayout>

<强> fragment_splash.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Splash Screen" />

</RelativeLayout>

<强> fragment_web_view     

    <WebView
        android:id="@+id/web_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </WebView>

</FrameLayout>