简体   繁体   English

'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

I'm developing an app in which I've Display strings and I've got a String Array.我正在开发一个应用程序,其中我有显示字符串和字符串数组。 I want to convert String Array text to speech .我想将字符串数组文本转换为语音 But I'm having this error.但我有这个错误。 I'm giving index to the String array so it can get converted it into speech on OnDone UtteranceProgressListener().我正在为 String 数组提供索引,以便它可以在 OnDone UtteranceProgressListener() 上将其转换为语音。

here's my code:这是我的代码:

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

} }

I'm having this error.我有这个错误。

    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

I don't know why I'm getting this.我不知道为什么我会得到这个。 is this bcoz of the String array?这是字符串数组的 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>

It appears you're attempting to use getResources() on a Context (which in this case is implicitly the MainActivity class, 'this')... which hasn't been initialized yet... because onCreate hasn't run yet.看来您正在尝试在 Context 上使用 getResources() (在这种情况下,它是隐式的 MainActivity 类,'this')...尚未初始化...因为 onCreate 尚未运行。

So, you probably just need to:所以,你可能只需要:

Change:改变:

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

To:至:

String[] Story;

Then inside onCreate, put:然后在onCreate里面,输入:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 例外:尝试在空对象引用上调用虚拟方法“android.content.res.Resources android.content.Context.getResources()” - Exception: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference android.content.Context.getResources() 在空对象引用上 - android.content.Context.getResources() on a null object reference 尝试在 null object 参考上调用虚拟方法“android.content.Context.getResources()” - Attempt to invoke virtual method 'android.content.Context.getResources()' on a null object reference 尝试在空对象引用上调用虚拟方法&#39;android.content.res.Resources $ Theme android.content.Context.getTheme()&#39; - Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference JPE:尝试在空对象引用上调用虚拟方法“android.content.res.Resources” - JPE: Attempt to invoke virtual method 'android.content.res.Resources' on a null object reference android.content.res.Resources$NotFoundException:Android - android.content.res.Resources$NotFoundException : Android 奥利奥:android.content.res.Resources $ NotFoundException - Oreo: android.content.res.Resources$NotFoundException android.content.res.Resources$NotFoundException: 在 setImageResource 中 - android.content.res.Resources$NotFoundException: in setImageResource android.content.res.Resources $ NotFoundException - android.content.res.Resources$NotFoundException android.content.res.Resources无法解析 - android.content.res.Resources cannot be resolved
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM