本文介绍了android studio 中简单 webview 应用程序的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试为这个交互式地图制作一个简单的 webview 应用程序:http://gta5online.com/map/interactive > 地图下方还有一个全屏链接.

i try to make a simple webview app for this interactive map: http://gta5online.com/map/interactive > there is also a fullscreen link below the map.

现在我创建了一个资产文件夹,并包含了交互式"文件夹,其中包含所有文件、图标、地图图块和 html 文档.

now i created an asset folder and included the "interactive" folder which has all the files, icons, map tiles and html document in it.

我想将 html 文档从那里加载到活动中作为 web 视图.所以它是一个本地文件.我希望应用程序处理它而不是默认浏览器.

i want to load the html document from there into a activity as a webview. so its a local file. i want the app to handle it not the default browser.

这是我现在所做的:

我创建了一个新项目并将这些代码添加到 activity_home.xml 文件中:

i created a new project and added those codes into the activity_home.xml file:

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("file:///android_asset/interactive/map.html");
/>

然后我添加了此代码以启用 Internet 访问到清单,即使它是我想要加载的本地 html 文档(供以后使用):

then i added this code to enable internet access in to manifest even if its a local html doc that i want to load (for later uses):

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

如您所见,我还在第一个代码块启用了 javascript.

i also enabled javascript at the first code block as you can see.

我也应该在 home.java 文件中放入一些代码吗?

should i've put some code into the home.java file too?

在 YT 教程中,我看到他在 java 文件中使用了类似的内容:

in a YT tutorial i saw that he used something like this in the java file:

      #in mainactivity.java
    setContentView(R.layout.activity_main);
    String url ="file:///android_asset/interactive/map.html";
    WebView view=(WebView) this.findViewById(R.id.webView);
    view.getSettings() .setJavaScriptEnabled(true);
    view.loadUrl(url);

有人可以通过解释我做错了什么以及如何完成这个应用程序的简单步骤来帮助菜鸟实现这一目标吗?我相信这对你们来说很简单,即使对我来说很难.

can someone please help a noob to achieve this by explaining what i did wrong and simple steps how to complete this app?. i'm sure its simple for you guys even if its that hard for me.

推荐答案

在 Android 中要非常小心,你永远不能将 Java 代码放入 XML 代码中.

Be very careful in Android, you can never place Java code into XML code.

此代码:

WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();

myWebView.loadUrl("file:///android_asset/interactive/map.html");
webSettings.setJavaScriptEnabled(true);

这篇关于android studio 中简单 webview 应用程序的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 02:46