【问题标题】:How to fix error on android webview如何修复android webview上的错误
【发布时间】:2013-11-07 08:46:52
【问题描述】:

这是我的代码:

public class AboutActivity extends Activity{

    WebView aboutwebview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.edit_profile_settings);

        aboutwebview = (WebView) findViewById(R.id.aboutwebview);
        aboutwebview.setHorizontalScrollBarEnabled(false);
        aboutwebview.loadUrl("file:///android_asset/pickld.html");
    } 

}

这里是xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="#7fad33"
        android:gravity="center"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tv0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Our Team"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#fff" />
    </LinearLayout>

    <WebView
        android:id="@+id/aboutwebview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

我很确定html 文件位于 assets 文件夹中,我还在清单文件上设置了权限,但是当我运行应用程序时,我遇到了强制关闭错误说问题出在这一行:

aboutwebview.loadUrl("file:///android_asset/pickld.html");

我想知道可能是什么问题。

这是 logcat 错误:

【问题讨论】:

  • 有什么问题?发布错误日志
  • 请发布 logcat 错误。
  • 看起来webview 为空。那是你有 webview 的 xml 吗?
  • 您确定您发布了edit_profile_settings.xml 代码吗?检查您是否使用正确的布局。你正在获得 NPE。
  • pickld.html 文件是否正确?

标签: android xml webview


【解决方案1】:

如果此行导致 NPE

 aboutwebview.loadUrl("file:///android_asset/pickld.html");

表示您的 aboutwebview 为空。要么您引用了错误的 ID,要么您使用 setContentView(R.layout.edit_profile_settings); 为您的活动设置了错误的布局。确保你这样做是为了避免 NPE。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多