片段中的文本到语音 [英] Text to speech in fragment

查看:69
本文介绍了片段中的文本到语音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出现错误,抱歉只是初学者.所有的帮助都会很棒.

Getting errors on, sorry only beginner. All help would be great.

  05-31 21:49:16.077: E/AndroidRuntime(655): FATAL EXCEPTION: main
  05-31 21:49:16.077: E/AndroidRuntime(655): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appal.song/com.appal.song.MainActivity}: java.lang.NullPointerException
  05-31 21:49:16.077: E/AndroidRuntime(655):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
  05-31 21:49:16.077: E/AndroidRuntime(655):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
  05-31 21:49:16.077: E/AndroidRuntime(655):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
  05-31 21:49:16.077: E/AndroidRuntime(655):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
  05-31 21:49:16.077: E/AndroidRuntime(655):    at android.os.Handler.dispatchMessage(Handler.java:99)
  05-31 21:49:16.077: E/AndroidRuntime(655):    at android.os.Looper.loop(Looper.java:137)
  05-31 21:49:16.077: E/AndroidRuntime(655):    at android.app.ActivityThread.main(ActivityThread.java:4424)
  05-31 21:49:16.077: E/AndroidRuntime(655):    at java.lang.reflect.Method.invokeNative(Native Method)
  05-31 21:49:16.077: E/AndroidRuntime(655):    at java.lang.reflect.Method.invoke(Method.java:511)
  05-31 21:49:16.077: E/AndroidRuntime(655):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
  05-31 21:49:16.077: E/AndroidRuntime(655):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
  05-31 21:49:16.077: E/AndroidRuntime(655):    at dalvik.system.NativeStart.main(Native Method)
  05-31 21:49:16.077: E/AndroidRuntime(655): Caused by: java.lang.NullPointerException
  05-31 21:49:16.077: E/AndroidRuntime(655):    at com.appal.song.Fragment_1.onCreateView(Fragment_1.java:33)
  05-31 21:49:16.077: E/AndroidRuntime(655):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
  05-31 21:49:16.077: E/AndroidRuntime(655):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
  05-31 21:49:16.077: E/AndroidRuntime(655):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
  05-31 21:49:16.077: E/AndroidRuntime(655):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
  05-31 21:49:16.077: E/AndroidRuntime(655):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
  05-31 21:49:16.077: E/AndroidRuntime(655):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551)
  05-31 21:49:16.077: E/AndroidRuntime(655):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1133)
  05-31 21:49:16.077: E/AndroidRuntime(655):    at android.app.Activity.performStart(Activity.java:4475)
  05-31 21:49:16.077: E/AndroidRuntime(655):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1929)
  05-31 21:49:16.077: E/AndroidRuntime(655):    ... 11 more

这是完整的代码,任何帮助都会很棒,谢谢,只有初学者抱歉.尝试制作文本,然后在它旁边制作一个按钮,将在语音中重复文本.

Here's full code, any help would be great, thanks, only beginner sorry. Trying to make text then next to it make a button that will repeat the text in speech.

 public class Fragment_1 extends Fragment implements OnClickListener, OnInitListener{
private TextToSpeech txts;
private static final String TAG = "TextToSpeechDemo";
private static final int MY_DATA_CHECK_CODE = 1234;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_1,
            container, false);


        Button btnAdd = (Button) getView().findViewById(R.id.button1);
        btnAdd.setOnClickListener(this);
        btnAdd.setEnabled(false);

        TextView txt = (TextView) getView().findViewById(R.id.textView1);
        txt.setText("OnCreate");

        // Fire off an intent to check if a TTS engine is installed
        Intent checkIntent = new Intent();
        checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        TextView txt = (TextView) getView().findViewById(R.id.textView1);
        if (requestCode == MY_DATA_CHECK_CODE)
        {
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS)
            {
                // success, create the TTS instance
                txt.setText("Done result");
                txts = new TextToSpeech(this, this);
                txts.setLanguage(Locale.US);
                Button btnAdd = (Button) getView().findViewById(R.id.button1);
                btnAdd.setEnabled(true);

            }
            else
            {
                txt.setText("Missing");
                // missing data, install it
                Intent installIntent = new Intent();
                installIntent.setAction(
                        TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installIntent);
            }
        }
    }

    @Override
    public void onDestroy()
    {
        // Don't forget to shutdown!
        if (txts != null)
        {
            txts.stop();
            txts.shutdown();
        }
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {

        TextView txt = (TextView) getView().findViewById(R.id.textView1);
                txt.setText("Click");

        String myText1 = "hello";
        String myText2 = "bye";
        txts.speak(myText1, TextToSpeech.QUEUE_FLUSH, null);
        txts.speak(myText2, TextToSpeech.QUEUE_ADD, null);
    }
    @Override
    public void onInit(int status) {
        TextView txt = (TextView) getView().findViewById(R.id.textView1);
        txt.setText("status 0");
        if (status == TextToSpeech.SUCCESS) {
                txt.setText("status 1");
            int result = txts.setLanguage(Locale.US);
            if (result == TextToSpeech.LANG_MISSING_DATA ||
                result == TextToSpeech.LANG_NOT_SUPPORTED) {
                txt.setText("status 2");
            } else {
                Button btnAdd = (Button) getView().findViewById(R.id.button1);
                btnAdd.setEnabled(true);
                txt.setText("status 3");
            }
            } else {
            txt.setText("status 4");
            Log.e(TAG, "Could not initialize TextToSpeech.");
        }
        return view;
    }
}

推荐答案

正如我在评论中所说:在 Fragment 中使用 this 是指该 Fragment 的实例.片段不是上下文,因此会出现编译时错误.

As I said in my comment: using this while in a Fragment refers to that Fragment's instance. Fragments aren't Contexts, hence the compile time error.

要创建一个新的 TextToSpeech 对象,您应该使用:

To make a new TextToSpeech Object, you should use:

txts = new TextToSpeech(getActivity(), this);

活动扩展了上下文,因此应该可以修复这个编译时错误.

Activities extend Context, so that should fix this compile-time error.

另一个潜在问题:对于您正在设置的按钮、文本视图等 onCreateView(),您应该使用 view.findViewById() 而不是 getView().findViewById()(但用于您在 onCreateView() 中设置的视图.getView() 的其余部分) 调用应该没问题)

Another potential problem: for the Buttons, TextViews, etc that you are setting up onCreateView() you should be using view.findViewById() instead of getView().findViewById() (but only for the Views you're setting up in onCreateView(). The rest of the getView() calls should be fine).

少了一个方法调用,getView() 可能返回 null 直到 onCreateView() 返回膨胀的视图.

It's one less method call, and getView() probably returns null until onCreateView() returns the inflated View.

最后,onCreateView() 需要返回一个视图,所以在它的右大括号之前,添加:

Lastly, onCreateView() needs to return a View, so just before its closing brace, add:

return view;

适当修改的方法:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_1,
            container, false);


        Button btnAdd = (Button) view.findViewById(R.id.button1);
        btnAdd.setOnClickListener(this);
        btnAdd.setEnabled(false);

        TextView txt = (TextView) view.findViewById(R.id.textView1);
        txt.setText("OnCreate");

        // Fire off an intent to check if a TTS engine is installed
        Intent checkIntent = new Intent();
        checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);

        return view;
    }

这篇关于片段中的文本到语音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆