TextToSpeech 抛出 NullPointerException

TextToSpeech throws NullPointerException

我正在编写回收站视图,其中包含可编辑的单词列表。 RecyclerView 的 ViewHolder 包含 2 个对象:editText 和声音图像图标。我的想法是,当我按下声音图标时,我希望听到单词的发音,这是我在 SDK 的 TextToSpeech class 的帮助下实现的。为了减少代码量,我创建了以下 class;

public class SpeechController {

private String pronounce;
private Context context;

public TextToSpeech tts = new TextToSpeech(context,
        new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status != TextToSpeech.ERROR) {
                    tts.setLanguage(Locale.UK);
                }
            }
        });

public SpeechController(Context context, String pronounce) {
    this.context = context;
    this.pronounce = pronounce;
}

public void speakOut() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        tts.speak(pronounce, TextToSpeech.QUEUE_FLUSH, null, null);
        tts.stop();
        tts.shutdown();
    } else {
        tts.speak(pronounce, TextToSpeech.QUEUE_FLUSH, null);
        tts.stop();
        tts.shutdown();
    }
}
}

然后我在适配器的 recyclerView 的 onBindViewHolder 方法中创建此 class 的实例,如下所示:

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
    if (mGroupsVc != null) {
        GroupVc current = mGroupsVc.get(position);
        holder.nameView.setText(current.getNameGroup());
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SpeechController mSpeechController = new SpeechController(holder.nameView.getContext(),
                        holder.nameView.getText().toString());
                mSpeechController.speakOut();
            }
        });
    } else {
        // Covers the case of data not being ready yet.
        holder.nameView.setText("No Word");
    }
}

我的应用程序已编译,但当我尝试单击声音按钮时出现 NullPointerException 并在 classes:

中引用这些运算符
SpeechController mSpeechController = new SpeechController(holder.nameView.getContext(),
                        holder.nameView.getText().toString());


public TextToSpeech tts = new TextToSpeech(context,

我知道这个异常的含义,但我不知道对象初始化哪里出错了。我需要你的帮助来定义它。下面我贴上完整的错误-logcat

03-14 17:48:52.168 21719-21719/com.hfad.singleton E/AndroidRuntime: FATAL EXCEPTION: main
                                                                java.lang.NullPointerException
                                                                    at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:606)
                                                                    at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:582)
                                                                    at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:567)
                                                                    at com.hfad.singleton.groupsActivityController.SpeechController.<init>(SpeechController.java:14)
                                                                    at com.hfad.singleton.adapter.GroupsVcAdapter.onClick(GroupsVcAdapter.java:52)
                                                                    at android.view.View.performClick(View.java:4421)
                                                                    at android.view.View$PerformClick.run(View.java:17903)
                                                                    at android.os.Handler.handleCallback(Handler.java:730)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                    at android.os.Looper.loop(Looper.java:213)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5225)
                                                                    at java.lang.reflect.Method.invokeNative(Native Method)
                                                                    at java.lang.reflect.Method.invoke(Method.java:525)
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
                                                                    at dalvik.system.NativeStart.main(Native Method)

设置上下文后初始化TTS

 public TextToSpeech tts ;
private Context context;
public SpeechController(Context context, String pronounce) {
    this.context = context;
    this.pronounce = pronounce;
    initTTS();
}
private void initTTS(){
    tts = new TextToSpeech(context,
            new TextToSpeech.OnInitListener() {
                @Override
                public void onInit(int status) {
                    if (status != TextToSpeech.ERROR) {
                        tts.setLanguage(Locale.UK);
                    }
                }
            });
}

在适配器中初始化 SpeechController 对象后:

SpeechController mSpeechController = new SpeechController(holder.nameView.getContext(),
                    holder.nameView.getText().toString());
mSpeechController.speakOut();

您调用它的方法 speakout() 试图访问变量 tts

tts.speak()

此对象尚未在适配器的当前范围内初始化,因此抛出 NPE。

所以您可以做的是在 SpeechController 对象的构造函数中初始化 tts 对象:

private String pronounce;
private Context context;
public TextToSpeech tts;

public SpeechController(Context context, String pronounce) {
this.context = context;
this.pronounce = pronounce;
tts = new TextToSpeech(context,
    new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if (status != TextToSpeech.ERROR) {
                tts.setLanguage(Locale.UK);
            }
        }
    });
}

这将修复 NPE 崩溃,因为 tts 在当前上下文中不再为 null。