android - 尝试运行 WebView 应用程序时出现错误

标签 android webview

我正在创建一个应用程序,我必须在其中使用 webView 并显示保存在 Assets 文件夹中的应用程序中的 HTML 文件。这是我的 mainActivity 代码。

public class MainActivity extends Activity {


WebView browser;

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

    browser = (WebView)findViewById(R.id.wv1);
    browser.loadUrl("file:///android_asset/test.htm");
    WebSettings webSettings = browser.getSettings();
    browser.getSettings().setJavaScriptEnabled(true);
      setContentView(browser);

}

这是我的xml文件

<LinearLayout 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"
android:orientation="vertical" >

 <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Test Application" 
    android:gravity="center_horizontal">

 </TextView>
<WebView 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/wv1"
    android:layout_marginTop="3dp">

</WebView>

当我试图运行这个应用程序时,它崩溃了。。this is the LogCat of errors when i run the application

谁能帮我找出我做错了什么。

最佳答案

您不能调用 setContentView(browser),因为 browser 已经是另一个 View 的 subview -- LinearLayout

如果您不需要其他 View ,只需更改布局 xml 文件并删除它们,只留下 WebView。否则,删除 setContentView(browser) 行。

关于android - 尝试运行 WebView 应用程序时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24197370/

相关文章:

android - 如何使用 rx java 的间隔进行后台任务

android - 显示 youtube 视频时 Webview 崩溃

javascript - 如何获取 Android WebView 中从顶部到 div block 的像素数量?

android: ZoomPicker 中断了 onTouchListener

android 面临在 android 2.2 中使用 fragment 的问题

android - 在 TextView 中居中文本

android - 自定义布局管理器滚动/动画

安卓棉花糖指纹

android - 如何将xml加载到webview android

iphone - 如何获取 UIWebView 上的当前滚动位置