-3
Я пытаюсь разработать простое приложение для веб-просмотра. Мой код, как показано ниже:приложение для Android-приложений перестало работать
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 и беги, он говорит, к сожалению, ваш апк не работает. Код, который я использую, кажется правильным, в чем проблема? Я использую студию Android и версию 6.0.
'webview' в макете должен быть' WebView'. –
Кроме этого, все остальное верно? –
Возможно, вы захотите установить «WebViewClient» перед загрузкой URL-адреса. –