如何修复android webview上的错误

时间:2013-11-07 08:46:53

标签: android xml webview

所以这是我的代码:

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文件位于资产文件夹中,我还对清单文件设置了权限,但是当我运行应用程序时,我正在关闭一个强制关闭错误,它说问题出在这一行:

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

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

这是logcat错误:

enter image description here

1 个答案:

答案 0 :(得分:1)

如果此行导致NPE

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

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