【问题标题】:Why is this simple code throwing a TansactionTooLargeException?为什么这个简单的代码会抛出 TransactionTooLargeException?
【发布时间】:2015-02-19 09:23:15
【问题描述】:

所以这就是交易。我正在创建一个游戏,它有一个倒数计时器和一个随机数生成器。简单的。好吧,我让游戏 100% 运行,但是当倒数计时器到达最后一个滴答声时,我一直在崩溃。有时,我的意思是有时。我收到了 java.lang.OutOfMemory 错误,但我已将其缩小到这段代码,但它抛出了 TansactionTooLargeException 但两个错误同时发生,即最后一个滴答声。

倒数计时器运行一次或超过 500 次后可能再次发生这种情况,这是非常随机的,没有任何意义。

FirstClass.java 代码

package com.example.testingclasses;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.widget.ProgressBar;
import android.widget.TextView;

public class FirstClass extends Activity {

    TextView thenumber;
    int min, max;
    ProgressBar gameTimer;
    CountDownTimer gameCountDownTimer;
    int i1, cdt,   progress ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_class);
        FirstClass.this.random();
    }

    public void random(){ //random number generator and displayer
        TextView thenumber = (TextView)findViewById(R.id.thenumber);
            min = 1;
            max = 5;
            cdt = 2500;
            ((TextView) findViewById(R.id.thenumber)).setClickable(true);
            Random r = new Random();
            i1 = r.nextInt(max - min + 1) + min;        
            thenumber.setText(Integer.toString(i1));
            FirstClass.this.setTimer(cdt);
    }


    public void setTimer(int time) {
        gameTimer = (ProgressBar)findViewById(R.id.timetoclick);
        progress = 100;
        final int actualTime = time;
        gameTimer.setProgress(progress);
        gameCountDownTimer = new CountDownTimer(actualTime, 10) {
            int totalTime = actualTime;
            @Override
            public void onTick(long millisUntilFinished) {
                    progress = (int)((millisUntilFinished ) /(double)totalTime * 100);
                    gameTimer.setProgress(progress);
            }

            @Override
            public void onFinish() {
                //progress = 0;
                //gameTimer.setProgress(progress);
                if (progress == 0) {
                    FirstClass.this.random();   
            }   else {
                gameCountDownTimer.onFinish();
            }
          }
        }; 
        gameCountDownTimer.start();
    }   
}

activity_first_class.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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.testingclasses.FirstClass" >

    <TextView
        android:id="@+id/thenumber"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/timetoclick"
        android:layout_margin="10dp"
        android:clickable="true"
        android:gravity="center_horizontal|center_vertical"
        android:onClick="onClick"
        android:text="#"
        android:textSize="200sp"
        android:background="#00000000" />

     <ProgressBar
        android:id="@+id/timetoclick"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_margin="5dp"
        android:progress="0" />

</RelativeLayout>

和崩溃:

12-20 11:57:30.594: E/JavaBinder(18000): !!! FAILED BINDER TRANSACTION !!!
12-20 11:57:30.595: E/AndroidRuntime(18000): Error reporting crash
12-20 11:57:30.595: E/AndroidRuntime(18000): android.os.TransactionTooLargeException
12-20 11:57:30.595: E/AndroidRuntime(18000):    at android.os.BinderProxy.transactNative(Native Method)
12-20 11:57:30.595: E/AndroidRuntime(18000):    at android.os.BinderProxy.transact(Binder.java:496)
12-20 11:57:30.595: E/AndroidRuntime(18000):    at android.app.ActivityManagerProxy.handleApplicationCrash(ActivityManagerNative.java:4100)
12-20 11:57:30.595: E/AndroidRuntime(18000):    at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:89)
12-20 11:57:30.595: E/AndroidRuntime(18000):    at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
12-20 11:57:30.595: E/AndroidRuntime(18000):    at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)

我正在尝试使用 Eclipse 内存分析,但没有任何运气让它工作。

【问题讨论】:

  • 问题不大可能出在所示代码中。
  • 它是唯一运行的代码。我发布的内容崩溃了。我原来的游戏运行良好,但突然开始崩溃。我正在测试刚刚获得 5.0.1 更新的 Nexus 5。即使代码不应该崩溃。
  • 此代码中没有任何内容会生成该异常。您只能从某种进程间通信 (IPC) 中获得该异常,例如使用太大的额外内容启动活动。

标签: java android eclipse random countdowntimer


【解决方案1】:

您的代码在以下行中抛出 StackOverflowError:gameCountDownTimer.onFinish();。 这是对 onFinish 方法的递归调用。

【讨论】:

  • 由于循环问题,我添加了它。似乎有时onFinish会在progress = 0之前发生。另请注意,我删除了该行并再次运行代码,现在出现了循环问题。还剩一个刻度,循环卡住了。
  • 将代码替换为gameCountDownTimer.cancel();进度= 0; gameTimer.setProgress(进度); MainActivity.this.random();这似乎解决了我的两个问题。
【解决方案2】:

将 onFinish() 代码替换为。

gameCountDownTimer.cancel();
progress = 0;
gameTimer.setProgress(progress);
FirstClass.this.random();   

修复了我在游戏中遇到的循环问题以及此处看到的问题。

在 onFinish() 运行之前 onTick 未完成的原始问题仍然存在,但我不再收到原始问题,并且使用上面的代码在此处发布了失败。

其实很奇怪。这段代码在我的 Nexus 5 上运行良好,但在我的 Digital2 平板电脑上,我仍然得到看起来像双击/未完成 onFinish 的内容。正在调查。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多