'android.content.res.Resources android.content.Context.getResources()' 空对象引用。将字符串数组转换为语音

'android.content.res.Resources android.content.Context.getResources()' on a null object reference. Converting String array to speech

我正在开发一个应用程序,其中我有显示字符串和一个字符串数组。我想将字符串数组文本转换为语音。但我有这个错误。 我正在为字符串数组提供索引,以便它可以在 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>

您似乎正试图在 Context 上使用 getResources()(在本例中隐式是 MainActivity class、'this')...尚未初始化... 因为 onCreate 还没有 运行。

因此,您可能只需要:

变化:

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

收件人:

String[] Story;

然后在onCreate里面,输入:

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