当前位置: 首页 > 知识库问答 >
问题:

'android。所容纳之物资源android。所容纳之物上下文在空对象引用上的getResources()。将字符串数组转换为语音

宗沛
2023-03-14

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

共有1个答案

阎昌勋
2023-03-14

似乎您正试图在上下文上使用getResources()(在本例中,该上下文隐式地是MainActivity类“this”)。。。还没有初始化。。。因为onCreate还没有运行。

因此,您可能只需要:

更改:

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

收件人:

String[] Story;

然后在onCreate中,放入:

Story = getResources().getStringArray(R.array.Storytxt);
 类似资料:
  • 我已经尝试了很多方法来解决这个问题,但不幸的是没有找到解决方案。这是我呼叫寻呼机适配器的主要活动: 这是我的寻呼机适配器类。我正在尝试获取资源字符串,但上下文始终为null:

  • 我正在使用ListView在Android中动态添加复选框。我正在使用一个ContextAdapter类来添加充气列表视图。我的错误日志如下: ContextAdapter类如下: 我的ListData类: 在活动中的函数中,我正在执行以下操作: 如果有人能指出我代码中的错误,那将非常有帮助。谢谢干杯

  • 例外:android。所容纳之物ActivityNotFoundException:找不到显式活动类com。我的应用程序。个人/网络。我的应用程序。个人的照相机CameraActivityKt已在AndroidManifest中声明此活动。xml?

  • 问题内容: 您可以在android的所有内容之上叠加视图吗? 在iPhone中,我将新视图设置为(0,0)并将其宽度和高度设置为的宽度和高度。如果将其添加到,则将使其充当覆盖层,覆盖后面的内容(或者如果它具有透明背景,则显示后面的视图)。 android中有类似的技术吗?我意识到视图略有不同(相对布局,线性布局和框架布局有三种(或更多类型),但是有什么方法可以不加选择地将视图叠加在所有内容之上吗?

  • 问题内容: 我已经编写了一个android程序来将值从Web服务加载到表行。但是值变为null,因此我需要将其转换为字符串。有人可以告诉我这样做的方法吗? 现在从Web服务获取空值,因此我需要将其转换为string 。 LogCat: 问题答案: 用这个,

  • “如果一个程序只含有数量固定的对象,而且已知它们的存在时间,那么这个程序可以说是相当简单的。” 通常,我们的程序需要根据程序运行时才知道的一些标准创建新对象。若非程序正式运行,否则我们根本不知道自己到底需要多少数量的对象,甚至不知道它们的准确类型。为了满足常规编程的需要,我们要求能在任何时候、任何地点创建任意数量的对象。所以不可依赖一个已命名的句柄来容纳自己的每一个对象,就象下面这样: MyObj