2013-02-16 5 views
0

내 오류를 발견했습니다. 내 muteSound 메서드에서 View 인수가 누락되었습니다.스레드를 사용할 때 Android 앱이 다운 됨

inudge.net과 같은 음악을 재생하는 앱을 만들고 있습니다. 나는 열을 반복하고 소리를 재생하고 재생/일시 정지 기능을 구현할 수있었습니다. 그러나 스레드가 실행될 때 단추 같은 다른 UI 구성 요소를 사용하려고하면 응용 프로그램이 충돌합니다. 예 : 음소거 버튼을 누르고 muteSound 메소드를 호출하면 앱이 작동을 멈추고 충돌합니다. thread는 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; 
    } 

} 

로그 캣 당신의 사고를 바탕으로

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 
+4

* 앱이 충돌하는 경우 항상 * LogCat 스택 추적을 게시하십시오. –

+0

내 오류를 발견했습니다. muteSound 메서드에서 View 인수가 누락되었습니다. – Lordking

답변

0

, 당신은 그러나 muteSound 를 호출하는 XML onClick를 정의하는 방법은 View 인수

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

을 할 수 있습니다 사용자 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(); 
관련 문제