2013-05-13 58 views
0

Im新的android和java編程。我試圖得到啓動時加載啓動畫面,然後在後臺加載位於不同活動的webview,一旦webview頁面準備就緒,我想切換活動到webview的活動預加載web視圖,然後切換到其活動

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 
+0

郵政logcat的請 – 2013-05-13 19:44:10

+0

添加logcat的。 – EnigmaMaster 2013-05-13 20:02:35

回答

2

你不應該使用的AsyncTask在WebView中加載URL。這個方法必須在UI線程中調用。 我建議片段是您的任務的最佳選擇。您可以創建2個片段 - 首先是飛濺,其次是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>