全部产品
云市场

android_os_TransactionTooLargeException

更新时间:2018-04-19 20:40:03

问题分析:

Binder传输的数据存在Parcel类中,如果Binder的参数或返回值太大(超过1MB),不适合的事务缓冲区,调用会失败,并抛出TransactionTooLargeException。

解决方案:

Binder的参数或返回值太大导致调用失败,并抛出TransactionTooLargeException,建议不要将大量数据传入Binder,避免传入大数组、大量字符串或位图。

代码示例:

  1. android.os.TransactionTooLargeException
  2. at android.os.BinderProxy.transact(Native Method)
  3. at com.android.internal.view.IInputMethodManager$Stub$Proxy.startInput(IInputMethodManager.java:604)
  4. at android.view.inputmethod.InputMethodManager.startInputInner(InputMethodManager.java:1173)
  5. at android.view.inputmethod.InputMethodManager.checkFocus(InputMethodManager.java:1282)
  6. at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3201)
  7. at android.os.Handler.dispatchMessage(Handler.java:102)
  8. at android.os.Looper.loop(Looper.java:136)
  9. at android.app.ActivityThread.main(ActivityThread.java:5017)
  10. at java.lang.reflect.Method.invokeNative(Native Method)
  11. at java.lang.reflect.Method.invoke(Method.java:515)
  12. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
  13. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
  14. at dalvik.system.NativeStart.main(Native Method)
  15. public class MainActivity extends Activity {
  16. private IDictionaryManager mDictionaryManager;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21. Intent intent = new Intent();
  22. intent.setAction("android.intent.action.DictionaryManagerService");
  23. intent.setPackage("com.wanginbeijing.dictionaryserver");
  24. bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
  25. //添加一个新单词
  26. findViewById(R.id.btn_add).setOnClickListener(new View.OnClickListener() {
  27. @Override
  28. public void onClick(View view) {
  29. try {
  30. mDictionaryManager.add("你好", "Hello");
  31. } catch (RemoteException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. });
  36. //查询单词
  37. findViewById(R.id.btn_query).setOnClickListener(new View.OnClickListener() {
  38. @Override
  39. public void onClick(View view) {
  40. try {
  41. String english = mDictionaryManager.query("你好");
  42. Toast.makeText(MainActivity.this,english, Toast.LENGTH_SHORT).show();
  43. } catch (RemoteException e) {
  44. e.printStackTrace();
  45. }
  46. }
  47. });
  48. }
  49. private ServiceConnection mConnection = new ServiceConnection() {
  50. @Override
  51. public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
  52. IDictionaryManager dictionaryManager = IDictionaryManager.Stub.asInterface(iBinder);
  53. try {
  54. mDictionaryManager = dictionaryManager;
  55. Toast.makeText(MainActivity.this, "connect success", Toast.LENGTH_SHORT).show();
  56. } catch (Exception e) {
  57. Toast.makeText(MainActivity.this, "connect failed", Toast.LENGTH_SHORT).show();
  58. e.printStackTrace();
  59. }
  60. }
  61. @Override
  62. public void onServiceDisconnected(ComponentName name) {
  63. }
  64. };
  65. }

参考文献:

1.https://developer.android.com/reference/android/os/TransactionTooLargeException.html

2.http://stackoverflow.com/questions/11451393/what-to-do-on-transactiontoolargeexception

3.https://halfstackdeveloper.github.io/2016/09/10/Android-Binder%E8%A7%A3%E5%AF%86/