运行的HTML5网站withing Android应用程序 - not responding消息 [英] Running an HTML5 Site withing Android App - Not responding message

查看:125
本文介绍了运行的HTML5网站withing Android应用程序 - not responding消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可有人请让我知道,如果他们看到我的code一些错误。

我已经创建了一个HTML5的网站(多页,和JS文件),我想打一个Android应用程序的一部分,每次我在我的虚拟设备上运行它时,我得到一个消息,不幸的是appname是没有响应或停止响应( WAIT / CLOSE)。我已经在我的MainActivity code,但看不出有什么是错的。

我没有得到我的控制台任何错误,并没有错误在我的LogCat中。

我的code应该是复制文件从文件夹资产进入SD卡,然后使用web视图打开index.html文件

MainActivity.java(code)

 包com.example.html5;进口的java.io.File;
进口java.io.FileNotFoundException;
进口java.io.FileOutputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.OutputStream中;进口android.os.Bundle;
进口android.os.Environment;
进口android.app.Activity;
进口android.content.res.AssetManager;
进口android.util.Log;
进口android.view.Menu;
进口android.webkit.WebSettings;
进口android.webkit.WebView;
进口android.webkit.WebViewClient;公共类MainActivity延伸活动{私人的WebView mWebView;
公共静态最后的字符串标记=MainActivity;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    copyAssets(ErgonomicsHelp);
    mWebView =(的WebView)findViewById(R.id.activity_main_webview);
    //启用JavaScript
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(真);
    //强制链接和重定向中的WebView而不是在浏览器中打开
    mWebView.setWebViewClient(新WebViewClient());
    mWebView.loadUrl(文件:/data/data/com.example.html5/ErgonomicsHelp/data/index.html);
}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    //充气菜单;如果是present这增加了项目操作栏。
    。getMenuInflater()膨胀(R.menu.main,菜单);
    返回true;
}私人无效copyAssets(字符串路径)
{
    的String []文件= NULL;    尝试
    {
        文件= getAssets()列表(路径)。
    }赶上(IOException异常E)
    {
        e.printStackTrace();
    }    如果(files.length == 0)
        copyFile(路径);
    其他
    {
        文件DIR =新的文件(getExternalFilesDir(空),路径);        如果(!dir.exists())
            dir.mkdir();        的for(int i = 0; I< files.length;我++)
        {
            copyAssets(路径+/+文件[I]);
        }
    }
}私人无效copyFile(字符串文件名)
{
    在的InputStream = NULL;    档案文件;
    出的OutputStream = NULL;    尝试
    {
        在= getAssets()打开(文件名)。
    }赶上(IOException异常E)
    {
        Log.e(TAG,+文件名错误当= getAssets()打开。);
        e.printStackTrace();
    }    文件=新的文件(getExternalFilesDir(空),文件名);    尝试
    {
        OUT =新的FileOutputStream(文件);
    }赶上(FileNotFoundException异常E)
    {
        Log.e(TAG,与出=新的FileOutputStream(文件错误););
        e.printStackTrace();
    }    字节[]数据;    尝试
    {
        数据=新的字节[in.available()];        in.read(数据);
        out.write(数据);        附寄();
        out.close();
    }赶上(IOException异常E1)
    {
        e1.printStackTrace();
    }}
}

任何建议将AP preciated。
真诚,德里克

- 编辑 - 24-05-2014 -

我有什么事情发生,我无法解释。

我已经打包的应用程序和设备上运行它,并且copyassets工作正常,正在创建的文件。我已经调整了我的code

<$p$p><$c$c>mWebView.loadUrl(\"file:/mnt/sdcard/android/data/com.example.html5/ErgonomicsHelp/data/index.html\");

该应用程序正确安装,但应用程序无法启动,我凸轮使它工作的唯一方法是,如果我用我的文件管理器应用程序,标签上的index.html文件,并打开HTML直接导航到该文件夹Viewer(第一个文件夹和一个齿轮的形象)。

任何人知道为什么这可能是发生


解决方案

您正在复制一堆文件在你的主UI线程,这是一个绝对的不在Android上。你需要学习如何使用的AsyncTask执行任何耗时的操作,否则你会继续获得这些错误。

下面是关于如何做一个好博客:的http:// android.programmerguru.com/what-is-asynctask-in-android/

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

解决方案

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/

这篇关于运行的HTML5网站withing Android应用程序 - not responding消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆