1

Can someone please let me know if they see some error on my code.

I have created an HTML5 website (multiple pages, and JS files) which I want to make part of an android app, every time I run it on my virtual device I get a message Unfortunately AppName is not responding or stop responding (WAIT/CLOSE). I have gone over my MainActivity code but can't see what is wrong.

I do not get any error in my console and No Error in my LogCat.

My code is supposed to copy files from assets folder into sdcard and then open the index.html file using webView

MainActivity.java (Code)

package com.example.html5;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.res.AssetManager;
import android.util.Log;
import android.view.Menu;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

private WebView mWebView;
public static final String TAG = "MainActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    copyAssets("ErgonomicsHelp");
    mWebView = (WebView) findViewById(R.id.activity_main_webview);
    // Enable Javascript
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    // Force links and redirects to open in the WebView instead of in a browser
    mWebView.setWebViewClient(new WebViewClient());
    mWebView.loadUrl("file:/data/data/com.example.html5/ErgonomicsHelp/data/index.html");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

private void copyAssets(String path)
{
    String[] files = null;

    try
    {
        files = getAssets().list(path);
    } catch (IOException e)
    {
        e.printStackTrace();
    }

    if (files.length == 0)
        copyFile(path);
    else
    {
        File dir = new File(getExternalFilesDir(null), path);

        if (!dir.exists())
            dir.mkdir();

        for (int i = 0; i < files.length; i++)
        {
            copyAssets(path + "/" + files[i]);
        }
    }
}

private void copyFile(String filename)
{
    InputStream in = null;

    File file;
    OutputStream out = null;

    try
    {
        in = getAssets().open(filename);
    } catch (IOException e)
    {
        Log.e(TAG, "ERROR WITH in = getAssets().open: " + filename);
        e.printStackTrace();
    }

    file = new File(getExternalFilesDir(null), filename);

    try
    {
        out = new FileOutputStream(file);
    } catch (FileNotFoundException e)
    {
        Log.e(TAG, "ERROR WITH out = new FileOutputStream(file);");
        e.printStackTrace();
    }

    byte[] data;

    try
    {
        data = new byte[in.available()];

        in.read(data);
        out.write(data);

        in.close();
        out.close();
    } catch (IOException e1)
    {
        e1.printStackTrace();
    }}
}

Any suggestions will be appreciated. Sincerely, Derek

-- Edited -- 24-05-2014 --

I have something happening which I can not explain.

I have packaged the app and run it on my device, and the copyassets works fine and the files are created. I have adjusted my code

mWebView.loadUrl("file:/mnt/sdcard/android/data/com.example.html5/ErgonomicsHelp/data/index.html");

The app installs properly, but the app does not start, the only way I cam make it work is if I navigate directly to the file folder using my file manager app, tab on the index.html files and open with "HTML Viewer" (image of a folder and a cogwheel).

Anyone knows why this could be happening

Derek
  • 175
  • 1
  • 4
  • 16

2 Answers2

2

You're copying a bunch of files in your main UI thread, which is an absolute "no" on Android. You need to learn how to use AsyncTask to perform any time consuming operations, otherwise you'll continue getting those errors.

Here is a good blog about how to do that: http://android.programmerguru.com/what-is-asynctask-in-android/

Oleg Gryb
  • 5,122
  • 1
  • 28
  • 40
  • Thank you for the Data, Will look into it. I added the hardware acceleration permission on the manifest and it stop giving me the error message on the scree. my error now is "WEB PAGE NOT AVAILABLE" does anyone know how to find the correct path where my files where copy to? I have tried different location but all comes back with the same error message "Web Page Not Found" – Derek May 23 '14 at 01:02
  • Check if you index.html file exists. – Oleg Gryb May 23 '14 at 01:29
  • I did not know I could do that on the Virtual Device, Any hints as to how to do it? – Derek May 23 '14 at 01:36
  • java.io.File.exists in your code or through adb shell commands. – Oleg Gryb May 23 '14 at 01:42
  • Hi, I have edited the original post. I installed the app on my tablet and It is not opening with the webView the only way to make it work is to navigate into the folder and open with HTML Viewer. Any suggestions? – Derek May 24 '14 at 22:01
  • What error do you get on a real device? Did you move your file copying logic to AsynTask as I've initially suggested? If you didn't, I can't help you any further. – Oleg Gryb May 24 '14 at 22:06
0

Thank you Oleg.

I worked it out.

I change my code from:

mWebView.setWebViewClient(new WebViewClient());

to - I should have used .setWebChromeClient

mWebView.setWebChromeClient(new WebChromeClient());

And change the location of my files to:

mWebView.loadUrl("file:/mnt/sdcard/android/data/com.example.html5/files/ErgonomicsHelp/index.html");

Now it is opening and running my HTML5 site in full in WebView. NOTE: Even do this has fix my problems I am not an experience programer and I am sure they are more efficient ways to code the app as suggested by Oleg post.

Derek
  • 175
  • 1
  • 4
  • 16