0

我无法弄清楚为什么我的简单 webview 应用程序会意外关闭。即使 Eclipse 没有抛出任何错误。

这是截图http://pbrd.co/YNWFVw

MainActivity.java

package com.example.testwebview;

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

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        WebView webView1 = (WebView) findViewById(R.id.webView1);
        webView1.loadUrl("http://www.google.com/m");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

活动主.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <WebView
        android:id="@+id/webView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</RelativeLayout>

testwebview 清单

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.testwebview.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>
4

2 回答 2

5

设置内容视图后获取网页视图,即

WebView webView1 = (WebView) findViewById(R.id.webView1); // 这个返回null,因为你没有设置contentView,所以当你尝试访问它时,它会抛出NullPointerException

setContentView所以像这样获取webView参考

setContentView(R.layout.activity_main);
  WebView webView1 = (WebView) findViewById(R.id.webView1);

编辑:

对于您的第二个查询,默认浏览器中的 webview 打开网址,请使用此代码

Webview默认使用浏览器打开url,我们需要做这样的事情

webView1.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
          view.loadUrl("http://www.google.com/m");
          return true;
           }}); 
于 2013-04-11T17:06:23.273 回答
0

在你做任何其他事情之前总是调用 super 的生命周期回调,即你应该调用super.onCreate(savedInstanceState)before setContentView(...)

实际上导致崩溃的原因很可能是 NullPointerException。调用setContentView(...)是实际创建您的视图的原因。如果您findViewById(...)在此之前调用,它将返回,null因为您的布局尚未膨胀。在 Eclipse 的 DDMS 中使用 LogCat 来获取堆栈跟踪,您会发现您正在调用.loadUrl(...).null

这是您的 onCreate 方法的外观:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    WebView webView1 = (WebView) findViewById(R.id.webView1);
    webView1.loadUrl("http://www.google.com/m");
}

另一个注意事项:不要fill_parent用于您的高度和宽度——它已被弃用。改为使用match_parent

于 2013-04-11T17:20:37.717 回答