errors for simple webview app in android studio

2020-05-09 22:37发布

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.

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

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.

here is what i did by now:

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");
/>

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"/>

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

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

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.

3条回答
在下西门庆
2楼-- · 2020-05-09 23:18

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

This code:

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

myWebView.loadUrl("file:///android_asset/interactive/map.html");
webSettings.setJavaScriptEnabled(true);
查看更多
ら.Afraid
3楼-- · 2020-05-09 23:19
别忘想泡老子
4楼-- · 2020-05-09 23:27

In your activity_home.xml file must be only this:

<?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"/>
查看更多
登录 后发表回答