的意图putExtra方法的最大长度是多少? (强制关闭)意图、长度、方法、最大

2023-09-11 12:39:57 作者:姑娘有苦不流泪

我需要一些帮助调试我的应用程序。首先:在仿真器和我的应用程序运行良好其他一些设备。在我的设备我得到了强制关闭(不带强制关闭消息)。

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).

在死机情况,如果应用程序的活动而改变。

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

下面是MainActivity类的一些code。它只是读取网页上的WebView HTML内容。不,这是不可能做到这一点了Htt的prequest,因为我没能模拟POST请求。

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");

}

所以,在 onConsoleM​​essage()方法,我只是通过HTML code到读取另一个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.

有没有可能被作为字符串传递到HTML code中的 ReadDataActivity 是大吗?我也尝试添加HTML code作为字符串一个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.

有些想法我能做些什么来调试问题?也许我应该尝试创建一个Parceable对象?

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

在模拟器一切工作正常。

In the emulator everything is working fine.

问候, 桑德罗

推荐答案

根据我的经验(前时),你能忍受为 1MB 数据在在意图。我认为,这种限制是有效的直至升级Froyo 或姜饼

As per my experience (sometime ago), you are able to put up to 1MB of data in a Bundle encapsulated inside Intent. I think, this restriction was valid up till Froyo or GingerBread.

然而,为了克服这个问题,我建议你保存在一个临时文件内容和路径/ URI 的您的Temp文件传递给你的第二个活动。然后在你的第二个活动,阅读其内容从文件,执行所需的操作,最后删除该文件。

However, 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 operation 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.