【问题标题】:Why can't I send an array of 1000000 ints from one Activity to another?为什么我不能将 1000000 个整数的数组从一个 Activity 发送到另一个?
【发布时间】:2025-12-17 22:15:02
【问题描述】:

我正在尝试将 1000000 ints 的数组从一个 Activity 秒到另一个。它适用于较小的数字,但是当我尝试 1000000 时,startActivity 什么都不做,并导致它显示在 logcat 中:

E/JavaBinder(2239): !!! FAILED BINDER TRANSACTION !!!

为什么?

这里有一些代码演示了这个问题:

MainActivity.java

package com.example.a;
import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.content.Intent;
public class MainActivity extends Activity {
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }
  public void startSecond(View v) {
    startActivity(new Intent(this, SecondActivity.class).putExtra(
        "a", new int[1000000]));
  }
}

SecondActivity.java

package com.example.a;
import android.os.Bundle;
import android.app.Activity;
public class SecondActivity extends Activity {
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
  }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="56dp"
        android:layout_marginTop="31dp"
        android:onClick="startSecond"
        android:text="click clack" />
</RelativeLayout>

activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.a"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.a.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.example.a.SecondActivity"></activity>
    </application>
</manifest>

【问题讨论】:

  • *.com/questions/3528735/failed-binder-transaction .. 特别是告诉你大小有限的答案。
  • 这个问题怎么是-1,而另一个问题是+26?
  • 我猜是因为 3 年前它不是一个已经在 SO 上得到回答的问题,因此如果你搜索那个错误,它也不是谷歌的第一结果。 ..
  • 如果您真的需要做类似的事情,请使用带有静态字段的单例模式来共享如此大量的数据。或者,您可以将数据存储在文件、sqlite 数据库或持久存储中,并在活动之间共享 URI。
  • @BrianRoach:这个问题难以理解;您可以判断的唯一方法是与此相关的是他有相同的症状之一(日志消息)。现在我阅读了 CommonsWare 的答案,我知道 Binder(不管是什么)在某种程度上与 Intent 相关,并且 Binder 有 1MB 的限制。该问题的最佳答案无济于事,因为它谈论的是Bundle,而不是Intent。很抱歉没有神奇地偶然发现这个问题并猜测它回答了我的问题。请注意该页面一次没有提到Intent这个词?我正在努力不直截了当地叫你白痴。

标签: java android android-activity


【解决方案1】:

为什么?

因为每个 Binder 事务限制为 1MB,而 Binder 是 Intent 系统的基础。 Intent 的大小(包括所有额外内容)必须小于 1MB。

【讨论】:

  • 1,000,000 个整数的数组远远超过 1MB。
  • 只是为了让这个答案更简洁,Array 的 1,000,000 ints 是多少 MB?
  • @tolgap: 1 int = 4 字节,所以 +- 3.8MB。给或取几个字节。这只是 int 分配,不包括任何引用。我的分配知识不是很广泛,但我相信内存地址之间也可能存在一些开销。总之,你应该看向 4MB 的方向。
  • 什么是“Binder 交易”?为什么Intent 的大小是有限的?我可以关闭它吗?
  • @Dog:Binder 是 Android 核心使用的一种进程间通信 (IPC) 机制。 Intent 的大小是有限的,因为设备 RAM 是有限的,并且 IPC 倾向于制作多个传输缓冲区的副本。此外,缓冲区越大,IPC 越慢,因为制作这些副本的 CPU 时间。您不能“关闭它”,除非您可以构建自己的 ROM 模块并将其安装在您可以安装的任何设备上。 “这个问题的最佳答案没有帮助,因为它谈论的是 Bundle,而不是 Intent。” -- Intent extras 通过 Bundle 实现。
最近更新 更多