【问题标题】:App is closing Unexpectedly in Android应用程序在 Android 中意外关闭
【发布时间】:2013-04-03 23:56:13
【问题描述】:

我无法弄清楚为什么我的简单 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;
    }

}

activity_main.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>

【问题讨论】:

  • 添加您的 Logcat 输出...

标签: android web-services web-applications web


【解决方案1】:

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

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

您的 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

【讨论】:

    【解决方案2】:

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

    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;
               }}); 
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-27
    • 1970-01-01
    • 2014-03-20
    • 2016-09-07
    相关资源
    最近更新 更多