簡體   English   中英

使用線程時Android應用程序崩潰

[英]Android app crashes when using thread

發現我的錯誤。 我在我的MutantSound方法上缺少View參數。

我正在創建一個應用程序來播放inudge.net之類的音樂。 我已經能夠遍歷該專欄並播放聲音並實現播放/暫停功能。 但是,如果我在線程運行時嘗試使用任何其他UI組件(例如按鈕),則應用程序將崩潰。 例如:當我按下靜音按鈕並調用MutentSound方法時,該應用程序停止工作並崩潰。 該線程位於playSound()方法內部。

package com.example.tunegrid;

import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    private SoundPool soundPool;
    private AudioManager audioManager;
    boolean loadComplete = false;

    private GridButton grid[][];
    Button btnPlay;
    boolean pause = true;
    private int tempo = 1000;
    private int r = -1;
    private Integer sound[][];
    private TextView txt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.setVolumeControlStream(AudioManager.STREAM_MUSIC);

        soundPool = new SoundPool(100, AudioManager.STREAM_MUSIC, 0);

        soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
            public void onLoadComplete(SoundPool arg0, int arg1, int arg2) {
                loadComplete = true;
                txt.setText("SoundPool load complete");
            }
        });     

        txt = (TextView) findViewById(R.id.textView1);
        btnPlay = (Button) findViewById(R.id.btnPlay);

        createDummyGrid();
    }

    public void onClick(View v)
    {
        Button btn = (Button)v;

        int id = btn.getId();
        int r = id / 4;
        int c = id - 4 * r;

        grid[r][c].toggleEnable();
    }

    public void playSound(View v)
    {       
        if(pause) { pause = false; btnPlay.setText("Pause"); }
        else { pause = true; btnPlay.setText("Play"); }

        final Handler handler = new Handler();

        Thread thread = new Thread() 
        {
            public void run()
            {
                while (true)
                {       
                    if (pause) { break; }

                    handler.post(new Runnable() {
                        public void run() 
                        {
                            int col = getNextCol();
                            sequence(col);
                            txt.setText("Playing column - " + col);
                        }
                    });

                    try { sleep(tempo); } catch (Exception e) {}
                }
            }
        };

        thread.start();
    }

    public void muteSound()
    {

    }

    private int getNextCol() {
        if (r == 3) { r = 0; }
        else { r++; }

        return r;
    }

    private void sequence(int r)
    {
        audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        float streamVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

        for (int c=0; c < 4; c++)
        {
            if (grid[c][r].isEnabled())
            {
                soundPool.play(sound[c][r], streamVolume, streamVolume, 1, 0, 1f);
            }
        }
    }

    private void createDummyGrid() {
        grid = new GridButton[4][4];
        sound = new Integer[4][4];

        for (int r=0; r < 4; r++)
        {
            for (int c=0; c < 4; c++)
            {
                grid[r][c] = new GridButton(this);
                grid[r][c].setId(4*r+c);
//              grid[r][c].setOnClickListener((OnClickListener) this); 

                if (c == 0) { sound[r][c] = soundPool.load(this, R.raw.s1, 1); }
                else if (c == 1) { sound[r][c] = soundPool.load(this, R.raw.s2, 1); }
                else if (c == 2) { sound[r][c] = soundPool.load(this, R.raw.s3, 1); }
                else if (c == 3) { sound[r][c] = soundPool.load(this, R.raw.s4, 1); }
            }
        }

        grid[0][0].toggleEnable();
        grid[1][0].toggleEnable();
        grid[2][0].toggleEnable();
        grid[1][1].toggleEnable();
        grid[2][2].toggleEnable();
        grid[3][3].toggleEnable();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

LogCat

02-16 04:51:32.547: W/Trace(851): Unexpected value from nativeGetEnabledTags: 0
02-16 04:51:32.547: W/Trace(851): Unexpected value from nativeGetEnabledTags: 0
02-16 04:51:32.626: W/Trace(851): Unexpected value from nativeGetEnabledTags: 0
02-16 04:51:32.626: W/Trace(851): Unexpected value from nativeGetEnabledTags: 0
02-16 04:51:32.626: W/Trace(851): Unexpected value from nativeGetEnabledTags: 0
02-16 04:51:32.626: W/Trace(851): Unexpected value from nativeGetEnabledTags: 0
02-16 04:51:32.626: W/Trace(851): Unexpected value from nativeGetEnabledTags: 0
02-16 04:51:32.626: W/Trace(851): Unexpected value from nativeGetEnabledTags: 0
02-16 04:51:32.626: W/Trace(851): Unexpected value from nativeGetEnabledTags: 0
02-16 04:51:32.636: W/Trace(851): Unexpected value from nativeGetEnabledTags: 0
02-16 04:51:32.649: W/Trace(851): Unexpected value from nativeGetEnabledTags: 0
02-16 04:51:32.649: W/Trace(851): Unexpected value from nativeGetEnabledTags: 0
02-16 04:51:32.656: W/Trace(851): Unexpected value from nativeGetEnabledTags: 0
02-16 04:51:32.666: W/Trace(851): Unexpected value from nativeGetEnabledTags: 0
02-16 04:51:32.676: D/AndroidRuntime(851): Shutting down VM
02-16 04:51:32.676: W/dalvikvm(851): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
02-16 04:51:32.696: E/AndroidRuntime(851): FATAL EXCEPTION: main
02-16 04:51:32.696: E/AndroidRuntime(851): java.lang.IllegalStateException: Could not find a method muteSound(View) in the activity class com.example.tunegrid.MainActivity for onClick handler on view class android.widget.Button with id 'btnMute'
02-16 04:51:32.696: E/AndroidRuntime(851):  at android.view.View$1.onClick(View.java:3584)
02-16 04:51:32.696: E/AndroidRuntime(851):  at android.view.View.performClick(View.java:4202)
02-16 04:51:32.696: E/AndroidRuntime(851):  at android.view.View$PerformClick.run(View.java:17340)
02-16 04:51:32.696: E/AndroidRuntime(851):  at android.os.Handler.handleCallback(Handler.java:725)
02-16 04:51:32.696: E/AndroidRuntime(851):  at android.os.Handler.dispatchMessage(Handler.java:92)
02-16 04:51:32.696: E/AndroidRuntime(851):  at android.os.Looper.loop(Looper.java:137)
02-16 04:51:32.696: E/AndroidRuntime(851):  at android.app.ActivityThread.main(ActivityThread.java:5039)
02-16 04:51:32.696: E/AndroidRuntime(851):  at java.lang.reflect.Method.invokeNative(Native Method)
02-16 04:51:32.696: E/AndroidRuntime(851):  at java.lang.reflect.Method.invoke(Method.java:511)
02-16 04:51:32.696: E/AndroidRuntime(851):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-16 04:51:32.696: E/AndroidRuntime(851):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-16 04:51:32.696: E/AndroidRuntime(851):  at dalvik.system.NativeStart.main(Native Method)
02-16 04:51:32.696: E/AndroidRuntime(851): Caused by: java.lang.NoSuchMethodException: muteSound [class android.view.View]
02-16 04:51:32.696: E/AndroidRuntime(851):  at java.lang.Class.getConstructorOrMethod(Class.java:460)
02-16 04:51:32.696: E/AndroidRuntime(851):  at java.lang.Class.getMethod(Class.java:915)
02-16 04:51:32.696: E/AndroidRuntime(851):  at android.view.View$1.onClick(View.java:3577)
02-16 04:51:32.696: E/AndroidRuntime(851):  ... 11 more
02-16 04:51:35.496: I/Process(851): Sending signal. PID: 851 SIG: 9

根據您的崩潰,您定義了一個XML onClick來調用muteSound但是,該方法需要接受一個View參數

public void muteSound(View view)
{
  //implementation
}

您可以按照以下方式使用runOnUiThread

new Thread(new Runnable() {
        public void run() {

             if (pause) { break; }

            runOnUiThread(new Runnable() {
                public void run() {

                    int col = getNextCol();
                    sequence(col);
                    txt.setText("Playing column - " + col);
                }
            });

            try { Thread.sleep(tempo); } catch (Exception e) {}
        }
    }).start();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM