尝试在 TabHost 中打开 WebView

发布于 2024-12-10 09:09:50 字数 2209 浏览 1 评论 0原文

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

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教程

Ok so i'm trying to create a small app that opens 3 different webviews within 3 different tabs. At the moment I Have my tabhost created ok and a separate class for my webview but when I open the app it doesn't display.

Tabhost code

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 code

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());
    }
}

I can't see a problem and I have read other peoples posts regarding TabHostContentFactory but I have no idea how to make it load the webview when the app opens. Any help would be greatly received.

The above code is based on the HelloWebView tutorial and the HelloTabWidget tutorial

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你是我的挚爱i 2024-12-17 09:09:50

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

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

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

I had tried the same things sometime back and later figured out that I did not declare appropraite Internet access permissions for the app. That was the reason, webview in the tabhost was getting blank and there was no error in the Logcat either.

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

Adding above lines to the AndroidManifest.xml had resolved my problem.

咿呀咿呀哟 2024-12-17 09:09:50

我认为问题本身出在主布局中,因为我通过唯一的修改很好地运行了您的代码:

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


        mWebView = new WebView(this);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl("http://www.google.com");
        mWebView.setWebViewClient(new HelloWebViewClient());

        setContentView(mWebView);
    }
}

这是为了让您开始,

我建议仅将 Tabhost 布局文件用于选项卡和 webview 控制器(以及其他意图布局) )应该在其他布局文件中。为此,您可以使用教程:http://joshclemm.com/blog/?p=136

我希望这会有所帮助

I assume the problem itself it's in the main layout, because I run your code fine wiht the only modification:

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


        mWebView = new WebView(this);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl("http://www.google.com");
        mWebView.setWebViewClient(new HelloWebViewClient());

        setContentView(mWebView);
    }
}

this is to get you starting,

I suggest to use the Tabhost layout file for only the tab and the webview controller (and other intent layouts) should be in other layout files. For this you can use the tutorial:http://joshclemm.com/blog/?p=136

I hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文