【问题标题】:'android.content.res.Resources android.content.Context.getResources()' on a null object reference. Converting String array to speech'android.content.res.Resources android.content.Context.getResources()' 在空对象引用上。将字符串数组转换为语音
【发布时间】:2020-04-17 23:29:07
【问题描述】:

我正在开发一个应用程序,其中显示字符串并且我有一个字符串数组。我想将字符串数组文本转换为语音。但我有这个错误。 我正在为字符串数组提供索引,以便它可以在 OnDone UtteranceProgressListener() 上将其转换为语音。

这是我的代码:

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
TextView textView;
Button button;
private boolean initialized;
private String[] queuedText;
private String TAG = "TTS";
private TextToSpeech tts;
int i = 0;


String[] Story = getResources().getStringArray(R.array.Storytxt);

String ajay = ("Once, a wolf was very hungry. It looked for food here and there. But it couldn't get any. At last, it found a loaf of bread and piece of meat in the hole of a tree.\n" +
        "\n" +
        "The hungry wolf squeezed into the hole. It ate all the food. It was a woodcutter's lunch. He was on his way back to the tree to have lunch. But he saw there was no food in the hole, instead, a wolf.\n" +
        "\n" +
        "On seeing the woodcutter, the wolf tried to get out of the hole. But it couldn't. Its tummy was swollen.\n" +
        "\n" +
        "The woodcutter caught the wolf and gave it nice beatings.");

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = findViewById(R.id.highlightxt);
    textView.setText(ajay);
    button = findViewById(R.id.button);

    tts = new TextToSpeech(getApplicationContext() /* context */, this /* listener */);

    tts.setOnUtteranceProgressListener(mProgressListener);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            speak(Story,i);
        }
    });
}

private void speak(String[] text, int i) {
    i = 0;

    if(!initialized){
        queuedText[i] = text[i];
        return;
    }
    queuedText = null;
    setTtsListner();
    HashMap<String , String> map = new HashMap<String, String>();
    map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"Message ID");
    tts.speak(text[i],TextToSpeech.QUEUE_ADD, map);

}

private void setTtsListner() {
}

@Override
public void onInit(int status) {
    if (status == TextToSpeech.SUCCESS) {
        initialized = true;
        tts.setLanguage(Locale.ENGLISH);

        if (queuedText != null) {
            speak(queuedText,i);
        }
    }
}
private abstract class runnable implements Runnable {
}
    private UtteranceProgressListener mProgressListener = new UtteranceProgressListener() {
        @Override
        public void onStart(String utteranceId) {
        } // Do nothing

        @Override
        public void onError(String utteranceId) {
        } // Do nothing.

        @Override
        public void onDone(String utteranceId) {

            new Thread() {
                public void run() {
                    MainActivity.this.runOnUiThread(new runnable() {
                        public void run() {

                            Toast.makeText(getBaseContext(), "TTS Completed", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }.start();
            i = i+1;
            speak(Story, i);
        }
    };

}

我遇到了这个错误。

    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.gujja.ajay.texthight/com.gujja.ajay.texthight.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference

我不知道我为什么会得到这个。这是字符串数组的 bcoz 吗?

<string-array name="Storytxt">

    <item>Once, a wolf was very hungry. It looked for food here and there. But it couldn\'t get any. At last it found a loaf of bread and piece of meat in the hole of a tree.</item>
    <item>The hungry wolf squeezed into the hole. It ate all the food. It was a woodcutter\'s lunch. He was on his way back to the tree to have lunch. But he saw there was no food in the hole, instead, a wolf.</item>
    <item>On seeing the woodcutter, the wolf tried to get out of the hole. But it couldn\'t. Its tummy was swollen.</item>
    <item>The woodcutter caught the wolf and gave it nice beatings.</item>

</string-array>

【问题讨论】:

标签: java nullpointerexception text-to-speech runtimeexception


【解决方案1】:

看来您正在尝试在 Context 上使用 getResources()(在这种情况下,它隐含地是 MainActivity 类,“this”)...尚未初始化...因为 onCreate 还没有运行。

所以,您可能只需要:

变化:

String[] Story = getResources().getStringArray(R.array.Storytxt);

收件人:

String[] Story;

然后在onCreate里面,放:

Story = getResources().getStringArray(R.array.Storytxt);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    • 2020-03-08
    • 2012-12-08
    • 2021-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多