gpt4 book ai didi

android - 尝试在 TabHost 中打开 WebView

转载 作者:行者123 更新时间:2023-11-29 22:18:56 24 4
gpt4 key购买 nike

好的,所以我正在尝试创建一个小应用程序,在 3 个不同的选项卡中打开 3 个不同的 WebView 。目前我已经为我的 webview 创建了 tabhost 和一个单独的类,但是当我打开应用程序时它不显示。

主机代码

public class HelloTabWidgetActivity extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab

// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, HelloWebViewActivity.class);

// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("albums").setIndicator("News",
res.getDrawable(R.drawable.ic_tab_albums))
.setContent(intent);
tabHost.addTab(spec);

// Do the same for the other tabs
intent = new Intent().setClass(this, SongsActivity.class);
spec = tabHost.newTabSpec("songs").setIndicator("SaintsTV",
res.getDrawable(R.drawable.ic_tab_songs))
.setContent(intent);
tabHost.addTab(spec);

intent = new Intent().setClass(this, ArtistsActivity.class);
spec = tabHost.newTabSpec("artists").setIndicator("Fixtures",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);

tabHost.setCurrentTab(0);
}

WebView 代码

public class HelloWebViewActivity extends Activity {
WebView mWebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.google.com");
mWebView.setWebViewClient(new HelloWebViewClient());
}
}

我看不出问题,我已经阅读了其他人关于 TabHostContentFactory 的帖子,但我不知道如何让它在应用程序打开时加载 webview。任何帮助都会受到极大的欢迎。

以上代码基于HelloWebView教程和HelloTabWidget教程

最佳答案

我曾经尝试过同样的事情,后来发现我没有为应用程序声明适当的互联网访问权限。这就是原因,tabhost 中的 webview 变得空白,并且 Logcat 中也没有错误。

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

将以上行添加到 AndroidManifest.xml 解决了我的问题。

关于android - 尝试在 TabHost 中打开 WebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7812791/

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