无法解析postdelayed方法

时间:2015-01-31 13:57:25

标签: java android android-studio

好的,这是我的代码:

final TextToSpeech ttv = new TextToSpeech(MainActivity.this, new TextToSpeech.OnInitListener() {


        public void onInit(int status) {
            ttv.setLanguage(Locale.US);
            Log.e("Init", "Success");
            ttvInit = true;
            // wait a little for the initialization to complete
            Handler h = new Handler();
            h.postDelayed(new Runnable()
            {
            public void run() {
                ttv.speak(textSpeech.getText().toString(),TextToSpeech.QUEUE_FLUSH,null);
            }
        }, 400);}

    else {
        System.out.println("Something went wrong.");
    }
        }

但我收到错误"无法解决方法' postDelayed(java.lung.Runnable,int)'

编辑: 它现在告诉我它未被使用: http://www.igreklik.com/slike/viewer.php?file=75221279105193055086.png&file_imgur=HPjDlfa.png

2 个答案:

答案 0 :(得分:13)

检查您的导入:

import android.os.Handler;

并将您的代码更改为此(您的“else”语句不正确):

final TextToSpeech ttv = new TextToSpeech(MainActivity.this, new TextToSpeech.OnInitListener() {

    public void onInit(int status) {
        ttv.setLanguage(Locale.US);
        Log.e("Init", "Success");
        ttvInit = true;
        // wait a little for the initialization to complete
        Handler h = new Handler();
        h.postDelayed(new Runnable()
            {
            public void run() {
                ttv.speak(textSpeech.getText().toString(),TextToSpeech.QUEUE_FLUSH,null);
            }
        }, 400);
    }
 });

答案 1 :(得分:0)

我刚刚意识到 java.util.logging 中还有一个名为“Handler”的类。你可能像我一样导入了错误的。删除它并改为import android.os.Handler