android webview应用程序停止工作

时间:2015-12-19 10:23:05

标签: java android webview

我正在尝试开发一个简单的webview应用程序。我的代码如下:

mainactivity.java

package com.mancohandtools.www.mancohandtools;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);


    WebView myWebView = (WebView) findViewById(R.id.myWebView);

    myWebView.loadUrl("http://www.mancohandtools.com/magento");

    myWebView.setWebViewClient(new MyWebViewClient());

    WebSettings webSettings = myWebView.getSettings();

    webSettings.setJavaScriptEnabled(true);
}

// Use When the user clicks a link from a web page in your WebView
private class MyWebViewClient extends WebViewClient {

    @Override

    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        if (Uri.parse(url).getHost().equals("www.centerend.com")) {

            return false;

        }


        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

        startActivity(intent);

        return true;
    }
}
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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: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="com.mancohandtools.www.mancohandtools.MainActivity">

<webview android:id="@+id/myWebView"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:scrollbars="none"
    xmlns:android="http://schemas.android.com/apk/res/android">
</webview>
</RelativeLayout>

AndroidMenifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mancohandtools.www.mancohandtools">
<uses-permission android:name="android.permission.INTERNET"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

</manifest>

当我构建APK和Run时,它说不幸的是你的apk已停止运行。我使用的代码似乎是正确的那么问题应该是什么?我正在使用android studio和6.0版。

1 个答案:

答案 0 :(得分:2)

xml文件中存在错误

<WebView android:id="@+id/myWebView"
   android:layout_height="fill_parent"
   android:layout_width="fill_parent"
   android:scrollbars="none"
   xmlns:android="http://schemas.android.com/apk/res/android">
</WebView>

您提到了webview,它应该是WebView