[英]Move from Fragment to Activity Causes android.os.TransactionTooLargeException
I'm trying to move from a Fragment to an Activity using Intents, and where I run my code, it causes android.os.TransactionTooLargeException
.我正在尝试使用 Intent 从 Fragment 移动到 Activity,并且在我运行代码的地方,它会导致android.os.TransactionTooLargeException
。
My Fragment code (CardFragment.java):我的片段代码(CardFragment.java):
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
txtName = view.findViewById(R.id.txt_name);
btnFood = view.findViewById(R.id.btn_food);
txtName.setText(mName);
Bitmap bm = StringToBitMap(mImage);
btnFood.setImageBitmap(bm);
btnFood.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), Recipes.class);
intent.putExtra("Name", mName);
intent.putExtra("Recipe", mRecipe);
intent.putExtra("Image", mImage);
startActivity(intent);
}
});
}
The mName
, mRecipe
and mImage
are String variables. mName
、 mRecipe
和mImage
是字符串变量。
My Activity code (Recipes.java):我的活动代码(Recipes.java):
public class Recipes extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipes);
TextView txtName = findViewById(R.id.txt_name);
TextView txtRecipe = findViewById(R.id.txt_recipe);
ImageView imgView = findViewById(R.id.img_view);
txtName.setText(getIntent().getStringExtra("Name"));
txtRecipe.setText(getIntent().getStringExtra("Recipe"));
Bitmap bm = CardFragment.StringToBitMap(getIntent().getStringExtra("Image"));
imgView.setImageBitmap(bm);
}
}
Every time I run this code, I get this error message:每次运行此代码时,都会收到此错误消息:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.company.recipeapp, PID: 12160
java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 2355168 bytes
at android.app.servertransaction.PendingTransactionActions$StopInfo.run(PendingTransactionActions.java:161)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: android.os.TransactionTooLargeException: data parcel size 2355168 bytes
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(BinderProxy.java:540)
at android.app.IActivityTaskManager$Stub$Proxy.activityStopped(IActivityTaskManager.java:4408)
at android.app.servertransaction.PendingTransactionActions$StopInfo.run(PendingTransactionActions.java:145)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
What is this error and how can I solve it?这是什么错误,我该如何解决? Maybe because there are multiple CardFragment
s in my previous view?也许是因为我之前的视图中有多个CardFragment
? Thanks for the help.谢谢您的帮助。
也许使用 getActivity().startActivity(intent) ?
This error means you sending more than 1 MB of data through intent, in this case, your bitmap is too large for intent.此错误意味着您通过 Intent 发送了超过 1 MB 的数据,在这种情况下,您的位图对于 Intent 来说太大了。
The solution is to reduce bitmap size or use different ways to pass bitmap like services, etc.解决方案是减少位图大小或使用不同的方式来传递位图,如服务等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.