Intent putExtra 方法的最大长度?(强制关闭) [英] Maximum length of Intent putExtra method? (Force close)

查看:22
本文介绍了Intent putExtra 方法的最大长度?(强制关闭)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助来调试我的应用程序.首先:在模拟器和其他一些设备上,我的应用程序运行良好.在我的设备上,我收到了强制关闭(没有强制关闭消息).

I need some help with debugging my application. First of all: In emulator and on some other devices my app is running fine. On my device I got a force close (without a force close message).

如果应用的 Activity 发生变化,就会发生崩溃".

The "crash" happens if the Activity of the app is changed.

这是MainActivity 类的一些代码.它只是通过 webview 从网页中读取 html 内容.不,不可能通过 HttpRequest 执行此操作,因为我无法模拟发布请求.

Here is some code of the MainActivity class. It just reads html content from a web page over webview. And no, it is NOT possible to do this over HttpRequest because I was not able to simulate the post request.

public class MainActivity extends Activity {

    public final static String EXTRA_HTML = "com.example.com.test.HTML";

    private WebView mWebView;
    private ProgressDialog mDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);  
        mWebView = (WebView) findViewById(R.id.webView1);
        CookieSyncManager.createInstance(this);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.removeAllCookie();
        mWebView.setBackgroundColor(0);
        mWebView.setWebChromeClient(new WebChromeClient() {
            public boolean onConsoleMessage(ConsoleMessage cmsg) {
                if (cmsg.message().startsWith("MAGIC")) {
                    mDialog.cancel();
                    /*HashMap<String, String> message = new HashMap<String, String>();*/
                    String msg = cmsg.message().substring(5);
                    Intent intent = new Intent(MainActivity.this,
                        ReadDataActivity.class);
                    /*message.put("message", msg);*/
                    /*intent.putExtra(EXTRA_HTML, message);*/
                                    intent.putExtra(EXTRA_HTML, msg);
                    startActivity(intent);
                }
                return false;
            }
        });
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setPluginState(PluginState.OFF);
        mWebView.getSettings().setLoadsImagesAutomatically(false);
        mWebView.getSettings().setBlockNetworkImage(true);
        mWebView.getSettings().setAppCacheEnabled(true);
        mWebView.getSettings().setSavePassword(true);
        mWebView.getSettings()
                .setCacheMode(WebSettings.LOAD_NORMAL);
        mWebView.setWebViewClient(new WebViewClient() {

            public void onPageFinished(WebView view, String address) {
                if (address.indexOf("mySession") != -1) {
                    view.loadUrl("javascript:console.log('MAGIC'+document.getElementsByTagName('html')[0].innerHTML);");
                }
});

                mWebView.loadUrl("http://www.myurl.de");

}

因此,在 onConsoleMessage() 方法中,我只是将 html 代码传递给另一个读取、解析和显示内容的 Activity 类.

So, in the onConsoleMessage() method I just pass the html code to another Activity class which read, parse and display the content.

现在的问题是,当 ReadDataActivity 类应该被加载时,应用程序只需关闭并返回主屏幕,没有任何消息或用户对话框.

The problem is now that at this point when the ReadDataActivity class should be loaded the application just close and go back to the home screen without any message or user dialog.

作为字符串传递给 ReadDataActivity 的 html 代码有可能很大吗?我也尝试将 html 代码作为字符串添加到 HashMap 中,但问题是一样的.

Is it possible that the html code which is passed as a string to the ReadDataActivity is to big? I also try to add the html code as a string in a HashMap but the problem is the same.

我可以做些什么来调试问题的一些想法?也许我应该尝试创建一个 Parcelable 对象?

Some ideas what I can do to debug the problem? Maybe I should try to create a Parcelable object?

在模拟器中一切正常.

推荐答案

根据我的经验(前一段时间),您可以在 Bundle<中打包多达 1MB 的数据/code> 用于 IPC.如果在给定时间发生大量交易,则可以减少此限制.更多信息此处.

As per my experience (sometime ago), you are able to parcel up to 1MB of data in a Bundle for IPC. This limit can be reduced if a lot of transactions are happening at a given time. Further information here.

为了解决这个问题,我建议您将内容保存在临时文件中,并将临时文件的 path/URI 传递给您的第二个活动.然后在您的第二个活动中,从文件中读取内容,执行您想要的操作,最后删除该文件.

In order to overcome this issue, I would suggest you to save your content on a temp file and pass the path/URI of your temp file to your second activity. Then in your second activity, read the contents out from file, perform your desired operations and finally delete that file.

如果您愿意,也可以将 Shared_Preferences 用于此任务 - 如果您认为处理文件很麻烦.

If you want, you may also incorporate Shared_Preferences for this task - if you think handling files is cumbersome.

这篇关于Intent putExtra 方法的最大长度?(强制关闭)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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