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