遇到错误,对不起,仅限初学者。所有的帮助都会很棒。
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;
}
}