java - 如何正确使用Handler

标签 java android runnable android-handler

我现在正在尝试创建一个调谐器应用程序,但它有两个错误,均源于处理程序。

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();
  }
}

我的错误如下:

mHandler.post(new Runnable() {

mHandler = new Handler(getMainLooper());

第一行导致无法解析方法“post(java.lang.Runnable)”,第二行导致无法解析方法“getMainLooper”。

谁能帮忙找出问题所在吗?

最佳答案

检查您的导入。您不小心导入了 java.util.logging.Handler 而不是 android.os.Handler

大多数时候,您创建的对象需要 UI 线程上的处理程序,这样您就可以安全地使用它:

private final Handler mHandler = new Handler();

更通用的方法如下所示(从其他线程发布到 UI 线程):

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

关于java - 如何正确使用Handler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27737769/

相关文章:

android - 带透明边距的相机

java - 为什么这么多 Java 教科书都有不使用 runnable 的 Swing 示例?

java - 从 JProfiler 中离线运行导出自定义探针图

java - BlockingQueue 是否有最大阻塞时间

java - 像后台进程命令行一样从 Wildfly 运行standalone.bat?

Android Json 应用程序错误

android - Android进程崩溃时如何创建崩溃日志

java - ExecutorService、Future、Threads 当所有 Runnables 必须完成时出现 Actives

java - 帮助在 Java 中实现 Runnable

java - JRuby 类和 Java 脚本引擎意外结果