gpt4 book ai didi

java - 预加载 webview 然后切换到其 Activity

转载 作者:行者123 更新时间:2023-12-02 07:02:02 24 4
gpt4 key购买 nike

我是 android 和 java 编程新手。我试图在启动时加载一个启动屏幕,然后在后台加载位于不同 Activity 上的 WebView ,一旦 WebView 页面准备好,我想将 Activity 切换到带有 WebView 的 Activity

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 View 。我怎样才能完成这项工作,以便一旦加载 web View 中的页面,它就会切换到 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

最佳答案

您不应该使用 AsyncTask 在 WebView 中加载 URL。该方法必须在UI线程中调用。我建议 Fragments 是完成您任务的最佳选择。您可以创建 2 个 fragment - 第一个使用splash,第二个使用WebView。在 Activity 的 onCreate(...) 方法中,您可以显示启动屏幕并启动加载网页。加载完成后 - 只需删除启动屏幕并显示 WebView fragment 。

这里有一些工作代码:

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>

关于java - 预加载 webview 然后切换到其 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16529815/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com