简体   繁体   中英

“Webpage not found” “file:///android_asset/html-nl/index.html not found” error on android device

My android app is using phonegap 2.7 and the barcodescanner plugin. When I test it using an emulator, everything works fine.

However, when I test it on my device, the first time I hit "Scan" it redirects to a page saying:

"Webpage not available: the webpage file:///android_asset/html-nl/index.html cannot be loaded".

(I translated this so the error could be slightly different using an English device)

When I hit Continue, the app continues to work properly. The weird thing is, this only happens the first time I install the app. When I hit "Scan" a second time, everything works fine.

(a part of) my code: AppActivity.Java

package com.myapp.app;

import org.apache.cordova.DroidGap;
import android.os.Bundle;

public class AppActivity extends DroidGap {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.loadUrl("file:///android_asset/www/index.html");
    }
}

index.html (in assets/www):

<!DOCTYPE HTML>
<html>
 <head>
  <title>PhoneGap</title>
  <script type="text/javascript" charset="utf-8" src="cordova-2.7.0.js"></script>
  <script type="text/javascript" src="barcodescanner.js"></script>
  <script type="text/javascript" src="main.js"></script>

 </head>
 <body>
    <h1>Testing!</h1>

    <a href="#" onclick="scanCode();">Scan Code</a> 
  </body>
</html>

In main.js, there's the scanCode() function:

var scanCode = function() {
window.plugins.barcodeScanner.scan(
        scanSuccess,
        function(error) {
            alert("Scan failed: " + error);
        });
};

Extra info: my mother language's abbreviation is "nl"... it probably has nothing to do with it, but I'd figure I let you guys know...

Thanks in advance!

EDIT: it probably HAS something to do with the language... this topic describes the exact same problem...: http://www.anddev.org/other-coding-problems-f5/problem-with-scanner-in-motorola-xt320-t2178445.html

No solution however :(

EDIT: tested it with my language set to English. Indeed, it shows: The Web page at file:///android_asset/html-en/index.html could not be loaded. I found that the problem lies with the scanner. Everytime it's fired for the first time after install, it redirect to that page... ZXing is probably trying to reach this one: https://github.com/zxing/zxing/blob/master/android/assets/html-en/index.html . Don't know why, didn't change anything to the source code.

Solution: I found that ZXing auto-loads HelpActivity when a new version of the app is installed. In that activity, there's the redirect to html-userlanguage. Easy to fix: just tell the script (it's in com.google.zxing.client.android) not to load the HelpActivity.

The problem is the android/assets folder is not packed in the .apk file when installing the app to the device. I'm using Android Studio, so this is what I have in the build.gradle file:

android { compileSdkVersion 16 buildToolsVersion "17.0.0"

defaultConfig {
    minSdkVersion 13
    targetSdkVersion 16
}

sourceSets {
    main {
        manifest.srcFile 'AndroidManifest.xml'
        java.srcDirs = ['src']
        resources.srcDirs = ['src']
        res.srcDirs = ['res']
        assets.srcDirs = ['assets']
    }
}

}

There's no need to modify HelpActivity.java.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM