2015-01-02 3 views
0

지금 튜너 응용 프로그램을 만들려고하는데 두 가지 오류가 있습니다. 둘 다 처리기를 기반으로합니다.제대로 처리기를 사용하는 방법

mHandler.post(new Runnable() { 

mHandler = new Handler(getMainLooper()); 

첫 번째 줄은 방법 '포스트 (인 java.lang.Runnable)를'해결할 수있는 결과, 그리고 두 번째 라인 결과는 '방법을 확인할 수 :

public class Pitch extends Fragment { 

private static final String TAG = "Pitch"; 
private TextView tv_pitch; 
private TextView tv_note; 
View rootView; 
Context mContext; 
Tuner tn; 

private Handler mHandler; 

private final static double[] stringFreqs = {82.41, 110.0, 146.83, 196.00, 246.94, 329.63}; 

private static HashMap<Double, String> stringNames = new HashMap<>(); 

Tuner.PitchDetectedListener listener = new Tuner.PitchDetectedListener() { 
    @Override 
    public void onPitchDetected(double pitch, double amplitude) { 
     Log.d(TAG, "Pitch: " + pitch + " Amplitude: " + amplitude); 

     if (amplitude > 1000000.0) { 

      double doublePitch = pitch * 2; 
      double halfPitch = pitch/2; 

      for (double freq : stringFreqs) { 
       if (pitch > freq - 3 && pitch < freq + 3) { 
        Log.d(TAG, stringNames.get(freq) + " with pitch of " + pitch); 
        display_color(pitch, freq); 
        return; 
       } 
      } 
      for (double freq : stringFreqs) { 
       if (doublePitch > freq - 3 && doublePitch < freq + 3) { 
        Log.d(TAG, stringNames.get(freq) + " with pitch of " + pitch); 
        display_color(doublePitch, freq); 
        return; 
       } 
      } 
      for (double freq : stringFreqs) { 
       if (halfPitch > freq - 3 && halfPitch < freq + 3) { 
        Log.d(TAG, stringNames.get(freq) + " with pitch of " + pitch); 
        display_color(halfPitch, freq); 
        return; 
       } 
      } 
     } 
    } 
}; 

private void display_color(final double pitch, final double freq) { 
    mHandler.post(new Runnable() { 
     @Override 
     public void run() { 
      tv_note.setText(stringNames.get(freq)); 
      tv_pitch.setText(Double.toString(pitch)); 
     } 
    }); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    rootView = inflater.inflate(R.layout.activity_tuner, container, false); 
    final BoxInsetLayout tuner = (BoxInsetLayout) rootView.findViewById(R.id.tuner_box); 
    final ImageView start = (ImageView) rootView.findViewById(R.id.tuner); 
    final ImageView stop = (ImageView) rootView.findViewById(R.id.tuner); 

    mContext = getActivity().getApplicationContext(); 

    Typeface font = Typeface.createFromAsset(mContext.getAssets(), "fonts/Foglihten-068.otf"); 
    tv_note.setTypeface(font); 

    tv_note.setText("-"); 

    mHandler = new Handler(getMainLooper()); 

    tv_note = (TextView) rootView.findViewById(R.id.tv_note); 

    int i = 0; 
    stringNames.put(stringFreqs[i++], "E"); 
    stringNames.put(stringFreqs[i++], "A"); 
    stringNames.put(stringFreqs[i++], "D"); 
    stringNames.put(stringFreqs[i++], "G"); 
    stringNames.put(stringFreqs[i++], "B"); 
    stringNames.put(stringFreqs[i], "e"); 


    start.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      tn.run(); 
     } 
    }); 

    stop.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      tn.stop(); 
     } 
    }); 

    tv_pitch = (TextView) tuner.findViewById(R.id.tv_pitch); 
    tv_note = (TextView) tuner.findViewById(R.id.tv_note); 
    return rootView; 

    tn = new Tuner(listener); 
    } 

public void onPause() { 
    tn.stop(); 
    super.onPause(); 
    } 
} 

내 오류는이 라인에 getMainLooper. '

아무도 잘못 될지 파악할 수 있습니까?

답변

1

수입을 확인하십시오. 실수로 android.os.Handler 대신 java.util.logging.Handler을 가져 왔습니다.

이를 사용하는 것이 안전 해요 있도록 UI 스레드에서 핸들러를 필요 당신이 개체를 만드는 대부분의 시간 :

private final Handler mHandler = new Handler(); 

보다 일반적인 접근 방식은 서로 UI 스레드이 (포스트 모양을 스레드) :

private final Handler mHandler = new Handler(Looper.getMainLooper()); 
+0

고쳐! 감사! –