【发布时间】: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