1

遇到错误,对不起,仅限初学者。所有的帮助都会很棒。

  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

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

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

2 回答 2

9

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

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

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

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

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

这是一个更少的方法调用,并且getView()可能会返回null,直到onCreateView()返回膨胀的视图。

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

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;
    }
于 2013-05-31T21:29:11.037 回答
0

您可以在方法内部初始化类TextToSpeech.OnInitListener()

// variable declaration
TextToSpeech tts;

// TextToSpeech initialization, must go within the onCreateView method
tts = new TextToSpeech(getActivity(), new TextToSpeech.OnInitListener() {
 @Override
 public void onInit(int i) {
  if (i == TextToSpeech.SUCCESS) {
   int result = tts.setLanguage(Locale.US);
   if (result == TextToSpeech.LANG_MISSING_DATA ||
    result == TextToSpeech.LANG_NOT_SUPPORTED) {
    Log.e("TTS", "Lenguage not supported");
   }
  } else {
   Log.e("TTS", "Initialization failed");
  }
 }
});

// method call
@Override
public void onClick(View v) {
  speak();
}

private void speak() {
 tts.speak("Text to Speech Test", TextToSpeech.QUEUE_ADD, null);
}

@Override
public void onDestroyView() {
 if (tts != null) {
  tts.stop();
  tts.shutdown();
 }
 super.onDestroyView();
}

取自:Text to Speech Youtube 教程

于 2020-01-13T20:33:34.517 回答