Android 线程中的堆栈溢出异常

发布于 2024-11-08 16:21:57 字数 3570 浏览 4 评论 0原文

我正在编写用于计算媒体文件的 bpm 的代码。我的要求是我只能播放歌曲 5 秒,不能超过 5 秒。所以我使用 Thread.sleep(5000) 并调用处理程序消息。它抛出 java.long.stackoverflowException。

我的代码:

currentThread.start(); 
....    
    public void run() {
        try {
            Log.e("bpm", "in run method");
            BPM2SampleProcessor processor = new BPM2SampleProcessor();
            processor.setSampleSize(1024);
            EnergyOutputAudioDevice output = new EnergyOutputAudioDevice(processor);     
            output.setAverageLength(1024);      
            try {
                player = new Player(new FileInputStream("/sdcard/taxi.mp3"), output); // line no 40
                currentThread.run();  // line no 41
                player.play();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (JavaLayerException e) {
                e.printStackTrace();
            }
           text.setText("bpm is  "+processor.getBPM());
           Log.e("bpm","  bpm is  "+processor.getBPM());            
           Thread.sleep(5000);
       threadHandler.sendEmptyMessage(0);
                         Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }       
    }

private Handler threadHandler = new Handler() 
{
    public void handleMessage(android.os.Message msg) 
    {
     Log.e("calledThread","5seconds");  
     player.close();
    }
};  

日志猫:

  05-20 18:05:01.761: ERROR/AndroidRuntime(2777): Uncaught handler: thread Thread-8 exiting due to uncaught exception
  05-20 18:05:02.237: ERROR/AndroidRuntime(2777): java.lang.StackOverflowError
  05-20 18:05:02.237: ERROR/AndroidRuntime(2777):     at java.nio.HeapByteBuffer.<init>(HeapByteBuffer.java:49)
  05-20 18:05:02.237: ERROR/AndroidRuntime(2777):     at java.nio.HeapByteBuffer.<init>(HeapByteBuffer.java:45)
  05-20 18:05:02.237: ERROR/AndroidRuntime(2777):     at java.nio.ReadWriteHeapByteBuffer.<init>(ReadWriteHeapByteBuffer.java:49)
  05-20 18:05:02.237: ERROR/AndroidRuntime(2777):     at java.nio.BufferFactory.newByteBuffer(BufferFactory.java:51)
 05-20 18:05:02.237: ERROR/AndroidRuntime(2777):     at java.nio.ByteBuffer.allocate(ByteBuffer.java:54)
 05-20 18:05:02.237: ERROR/AndroidRuntime(2777):     at java.nio.charset.CharsetEncoder.encode(CharsetEncoder.java:351)
 05-20 18:05:02.237: ERROR/AndroidRuntime(2777):     at java.nio.charset.Charset.encode(Charset.java:711)
 05-20 18:05:02.237: ERROR/AndroidRuntime(2777):     at java.lang.String.getBytes(String.java:1022)
 05-20 18:05:02.237: ERROR/AndroidRuntime(2777):     at org.apache.harmony.luni.util.Util.getBytes(Util.java:61)
 05-20 18:05:02.237: ERROR/AndroidRuntime(2777):     at java.io.File.properPath(File.java:1362)
 05-20 18:05:02.237: ERROR/AndroidRuntime(2777):     at java.io.FileInputStream.<init>(FileInputStream.java:77)
 05-20 18:05:02.237: ERROR/AndroidRuntime(2777):     at java.io.FileInputStream.<init>(FileInputStream.java:130)
 05-20 18:05:02.237: ERROR/AndroidRuntime(2777):     at beatit.beatit.main2.run(main2.java:40)
 05-20 18:05:02.237: ERROR/AndroidRuntime(2777):     at java.lang.Thread.run(Thread.java:1096)
 05-20 18:05:02.237: ERROR/AndroidRuntime(2777):     at  beatit.beatit.main2.run(main2.java:41)
 05-20 18:05:02.237: ERROR/AndroidRuntime(2777):     at java.lang.Thread.run(Thread.java:1096)

 05-20 18:05:02.237: ERROR/AndroidRuntime(2777):     at java.la
 05-20 18:05:05.573: ERROR/JavaBinder(1276): !!! FAILED BINDER TRANSACTION !!!

I am doing code for calculating bpm for media files. my requirement is i have to play song only for 5 seconds not more than 5 seconds. so i use Thread.sleep(5000) and i call handler message. it throws java.long.stackoverflowexception.

my code:

currentThread.start(); 
....    
    public void run() {
        try {
            Log.e("bpm", "in run method");
            BPM2SampleProcessor processor = new BPM2SampleProcessor();
            processor.setSampleSize(1024);
            EnergyOutputAudioDevice output = new EnergyOutputAudioDevice(processor);     
            output.setAverageLength(1024);      
            try {
                player = new Player(new FileInputStream("/sdcard/taxi.mp3"), output); // line no 40
                currentThread.run();  // line no 41
                player.play();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (JavaLayerException e) {
                e.printStackTrace();
            }
           text.setText("bpm is  "+processor.getBPM());
           Log.e("bpm","  bpm is  "+processor.getBPM());            
           Thread.sleep(5000);
       threadHandler.sendEmptyMessage(0);
                         Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }       
    }

private Handler threadHandler = new Handler() 
{
    public void handleMessage(android.os.Message msg) 
    {
     Log.e("calledThread","5seconds");  
     player.close();
    }
};  

log cat:

  05-20 18:05:01.761: ERROR/AndroidRuntime(2777): Uncaught handler: thread Thread-8 exiting due to uncaught exception
  05-20 18:05:02.237: ERROR/AndroidRuntime(2777): java.lang.StackOverflowError
  05-20 18:05:02.237: ERROR/AndroidRuntime(2777):     at java.nio.HeapByteBuffer.<init>(HeapByteBuffer.java:49)
  05-20 18:05:02.237: ERROR/AndroidRuntime(2777):     at java.nio.HeapByteBuffer.<init>(HeapByteBuffer.java:45)
  05-20 18:05:02.237: ERROR/AndroidRuntime(2777):     at java.nio.ReadWriteHeapByteBuffer.<init>(ReadWriteHeapByteBuffer.java:49)
  05-20 18:05:02.237: ERROR/AndroidRuntime(2777):     at java.nio.BufferFactory.newByteBuffer(BufferFactory.java:51)
 05-20 18:05:02.237: ERROR/AndroidRuntime(2777):     at java.nio.ByteBuffer.allocate(ByteBuffer.java:54)
 05-20 18:05:02.237: ERROR/AndroidRuntime(2777):     at java.nio.charset.CharsetEncoder.encode(CharsetEncoder.java:351)
 05-20 18:05:02.237: ERROR/AndroidRuntime(2777):     at java.nio.charset.Charset.encode(Charset.java:711)
 05-20 18:05:02.237: ERROR/AndroidRuntime(2777):     at java.lang.String.getBytes(String.java:1022)
 05-20 18:05:02.237: ERROR/AndroidRuntime(2777):     at org.apache.harmony.luni.util.Util.getBytes(Util.java:61)
 05-20 18:05:02.237: ERROR/AndroidRuntime(2777):     at java.io.File.properPath(File.java:1362)
 05-20 18:05:02.237: ERROR/AndroidRuntime(2777):     at java.io.FileInputStream.<init>(FileInputStream.java:77)
 05-20 18:05:02.237: ERROR/AndroidRuntime(2777):     at java.io.FileInputStream.<init>(FileInputStream.java:130)
 05-20 18:05:02.237: ERROR/AndroidRuntime(2777):     at beatit.beatit.main2.run(main2.java:40)
 05-20 18:05:02.237: ERROR/AndroidRuntime(2777):     at java.lang.Thread.run(Thread.java:1096)
 05-20 18:05:02.237: ERROR/AndroidRuntime(2777):     at  beatit.beatit.main2.run(main2.java:41)
 05-20 18:05:02.237: ERROR/AndroidRuntime(2777):     at java.lang.Thread.run(Thread.java:1096)

 05-20 18:05:02.237: ERROR/AndroidRuntime(2777):     at java.la
 05-20 18:05:05.573: ERROR/JavaBinder(1276): !!! FAILED BINDER TRANSACTION !!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

↙厌世 2024-11-15 16:21:57

您的线程一遍又一遍地调用 run 方法,导致堆栈溢出:

currentThread.run();

没有条件可以停止它。 (为了明确起见,currentThread.start() 在某个阶段调用 run 方法)

Your thread calling the run method over and over again, causing a stackoverflow:

currentThread.run();

There is no condition to stop that. (Just to make it clear, currentThread.start() calls the run method in some stage)

暗喜 2024-11-15 16:21:57

您几乎回答了自己:

currentThread.run();  // line no 41

下面我写了如何处理您的问题的示例:

    AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            //put your code here
        }
    };

    try {
        task.execute((Void)null).get(5, TimeUnit.SECONDS);
    } catch(Exception e) {
        e.printStackTrace();
    }

You almost answered yourself:

currentThread.run();  // line no 41

Below I wrote example how to deal with your problem:

    AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            //put your code here
        }
    };

    try {
        task.execute((Void)null).get(5, TimeUnit.SECONDS);
    } catch(Exception e) {
        e.printStackTrace();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文