gpt4 book ai didi

android - WebView 不自动加载

转载 作者:数据小太阳 更新时间:2023-10-29 03:02:43 26 4
gpt4 key购买 nike

我正在尝试构建我的第一个应用程序,就这样。启动 -> 闪屏 -> 自动显示网页(全屏/无按钮触发)。但是,在启动画面之后,它只是一片空白。我猜想 MainActivity.java 中的 Intent 部分和 AndroidManifest.xml 中的 intent-filter 有问题。但我还不知道要解决这个问题。这是我的代码:

主 Activity .java

package com.myapp.splashscreen;

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;

public class MainActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent(context, WebViewActivity.class);
startActivity(intent);
}

}

启动画面.java

package com.myapp.splashscreen;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class SplashScreen extends Activity {

private static final int SPLASH_TIME = 3 * 1000;// 3 seconds

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

new Handler().postDelayed(new Runnable() {

@Override
public void run() {

Intent intent = new Intent(SplashScreen.this,
MainActivity.class);
startActivity(intent);

SplashScreen.this.finish();

overridePendingTransition(R.anim.fade_in, R.anim.fade_out);

}
}, SPLASH_TIME);

new Handler().postDelayed(new Runnable() {
@Override
public void run() {
}
}, SPLASH_TIME);

}


@Override
public void onBackPressed() {
this.finish();
super.onBackPressed();
}
}

WebViewActivity.java

package com.myapp.splashscreen;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class WebViewActivity extends Activity {

private WebView webView;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.id.webView1);

webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
}

}

主.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#d3d3d3"
android:orientation="vertical">

<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

</RelativeLayout>

启动画面.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/splash"
android:configChanges="orientation|keyboardHidden"
android:orientation="vertical"
android:screenOrientation="portrait">

</RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myapp.splashscreen"
android:versionCode="1"
android:versionName="1.0">

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

<uses-sdk android:minSdkVersion="8" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">

<activity
android:name=".SplashScreen"
android:theme="@android:style/Theme.Black.NoTitleBar">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".MainActivity"></activity>

<activity
android:name=".WebViewActivity"
android:theme="@android:style/Theme.NoTitleBar" />
</application>
</manifest>

最佳答案

如果您希望顺序为 SplashActivity -> WebViewActivity

SplashActivity 的变化

 Intent intent = new Intent(SplashScreen.this,
MainActivity.class);

 Intent intent = new Intent(SplashScreen.this,
WebViewActivity.class);

这将告诉您应用程序它应该启动 WebViewActivity 而不是 MainActivity

关于android - WebView 不自动加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22765581/

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