【问题标题】:WebView Inside TabView failTabView 内的 WebView 失败
【发布时间】:2010-08-28 14:58:57
【问题描述】:

我一直试图在 TabView 的一个选项卡内添加一个 WebView,但它不起作用。 当我转到其他选项卡时,我只有 TextViews 并且它们工作正常,但是一旦我单击其中包含 Web 的选项卡,就会出现错误,它会意外关闭。 这是我的 search_result.java 有标签视图

package supa.mack.doppler;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;

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

     TabHost mTabHost;
     mTabHost = getTabHost();

     mTabHost.addTab(mTabHost.newTabSpec("Results").setIndicator("Results").setContent(R.id.textview1));
     mTabHost.addTab(mTabHost.newTabSpec("Location").setIndicator("Location").setContent(R.id.textview2));
     mTabHost.addTab(mTabHost.newTabSpec("Facebook")
       .setIndicator("Facebook")
       .setContent(new Intent(this, BioActivity.class)));

     mTabHost.setCurrentTab(0);


 Button searchButton=(Button) findViewById(R.id.return_button);
    searchButton.setOnClickListener(new View.OnClickListener(){
   public void onClick(View v) {
   Intent intent=new Intent(
     search_result.this,
     doppler_test.class
     );

    startActivity(intent);
   }
});  
}
}

现在这里是定义 tabView 的 search_result.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" android:layout_gravity="center_horizontal">
        <TableLayout 
   android:id="@+id/TableLayout01" 
   android:layout_width="fill_parent" 
   android:layout_height="fill_parent" android:layout_gravity="center_horizontal" android:gravity="center_horizontal">

         <Button
      android:id="@+id/return_button"
      android:layout_height="wrap_content" 
      android:text="Return" 
      android:layout_width="fill_parent"/>  

          <TableRow>
           <TextView 
                android:id="@+id/textview1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="Here is the list of people in your location" />
            <TextView 
                android:id="@+id/textview2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" 
                android:text="Here you will see your exact location to bookmark it" />
            <TextView 
                android:id="@+id/textview3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" 
                android:text="Reference Facebook To Check The One You Seek" />
        </TableRow>
      </TableLayout>
    </FrameLayout>
   </LinearLayout>
</TabHost>

这是定义 webView 的 BioActivity.java

package supa.mack.doppler;

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

public class BioActivity extends Activity {
 WebView browser;

 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.web_view);

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

最后这是放置 webView 的 web_view.xml

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>

哦,差点忘了添加我的清单......

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="supa.mack.doppler"
      android:versionCode="1"
      android:versionName="1.0">
        <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
        <activity android:name=".doppler_test"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
  <activity android:name=".search_result"> 
  <activity android:name=".search_result" android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar"/>
        <activity android:name=".BioActivity" android:label="@string/app_name"
      android:theme="@android:style/Theme.NoTitleBar"/>
  </activity>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.INTERNET" />
    </application>
        <uses-sdk android:minSdkVersion="7" />


</manifest> 

感谢您查看我的代码,这真的毁了我的一天,无法让它工作......

【问题讨论】:

    标签: android webview


    【解决方案1】:

    我感觉您的 web_view.xml 格式不正确,需要线性布局或相对布局。我的程序几乎完全相同,我的 web_view.xml 文件看起来像这样:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainlayout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <WebView android:id="@+id/webview"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"/>
    </RelativeLayout>
    

    您也可以尝试通过代码创建新的 web 视图,而不必为 XML 布局而烦恼。如果您计划拥有的只是一个 web 视图并且不需要重用该文件,则可以使用它。

    编辑:我刚刚测试了您的 XML 方法。它似乎是有效的并且工作得很好。然而,我注意到调用 BioActivity 的方法可能不正确。例如我使用这样的东西:

    intent = new Intent().setClass(this, BioActivity.class);  
    mTabHost.addTab(mTabHost.newTabSpec("Facebook") 
           .setIndicator("Facebook") 
           .setContent(intent)); 
    

    试一试,让我们知道它是如何工作的。我也相信你的按钮监听器应该在 onCreate 方法之外。

    【讨论】:

    • 好吧,我在上面添加了您的意图代码,但仍然有相同的错误......至于清单应该是什么样子,因为我没有从中得到任何错误......谢谢你的帮助远
    • 如果我的所有代码都与您一起工作,这似乎是一个明显的错误......只是似乎无法弄清楚如何在不出现应用程序错误或语法错误的情况下重新输入它......
    • 您的清单现在更正确了。 “使用”应该在应用程序标签之外,并且您仍然有两个重复的活动。
    【解决方案2】:

    您的清单也可能不正确。您的应用程序标签未关闭,您有重复的活动,并且每个活动上的标签和主题不是必需的。为了让互联网正常工作,您还需要添加此权限:

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

    缺少权限当然不会导致你的程序崩溃,只是确保你已经实现了清理和格式化你的清单。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-12
      • 1970-01-01
      • 1970-01-01
      • 2019-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多