我是java和android app开发的新手。我正在尝试开发一个小型Android应用程序来从用户那里获取文本,然后点击一个按钮,将其转换为语音。我在代码中没有错误。但是,当我运行它时,显示'不幸的是应用程序停止了#34;。 我可以得到一些帮助吗?
package com.daya.wetalk;
import java.util.Locale;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
@SuppressWarnings("unused")
public class MainActivity extends Activity implements OnInitListener{
private int TYPE_TEXT_FLAG_AUTO_CORRECT=1;
private TextToSpeech tts;
private EditText edit;
private ImageButton b;
String incomingmessage;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tts= new TextToSpeech(this,(OnInitListener) this);
edit = (EditText)findViewById(R.id.mEdit);
b = (ImageButton)findViewById(R.id.mButton);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v){
incomingmessage=edit.getText().toString();
if (incomingmessage != null){
speakOut(incomingmessage);
}
}
});
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
private void speakOut(String msg){
String text = msg.toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
@Override
public void onDestroy(){
if (tts != null){
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS){
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED){
Log.e("TTS", "This Language is not supported");
}else{
speakOut(incomingmessage);
}
}else{
Log.e("TTS", "Initialization Failed!");
}
}
}
这是logcat日志:
04-28 05:41:47.455: I/TextToSpeech(17110): Sucessfully bound to com.google.android.tts
04-28 05:41:47.465: W/dalvikvm(17110): threadid=1: thread exiting with uncaught exception (group=0x418f22a0)
04-28 05:41:47.465: E/AndroidRuntime(17110): FATAL EXCEPTION: main
04-28 05:41:47.465: E/AndroidRuntime(17110): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.daya.wetalk/com.daya.wetalk.MainActivity}: java.lang.NullPointerException
04-28 05:41:47.465: E/AndroidRuntime(17110): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092)
04-28 05:41:47.465: E/AndroidRuntime(17110): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
04-28 05:41:47.465: E/AndroidRuntime(17110): at android.app.ActivityThread.access$700(ActivityThread.java:134)
04-28 05:41:47.465: E/AndroidRuntime(17110): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
04-28 05:41:47.465: E/AndroidRuntime(17110): at android.os.Handler.dispatchMessage(Handler.java:99)
04-28 05:41:47.465: E/AndroidRuntime(17110): at android.os.Looper.loop(Looper.java:137)
04-28 05:41:47.465: E/AndroidRuntime(17110): at android.app.ActivityThread.main(ActivityThread.java:4867)
04-28 05:41:47.465: E/AndroidRuntime(17110): at java.lang.reflect.Method.invokeNative(Native Method)
04-28 05:41:47.465: E/AndroidRuntime(17110): at java.lang.reflect.Method.invoke(Method.java:511)
04-28 05:41:47.465: E/AndroidRuntime(17110): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
04-28 05:41:47.465: E/AndroidRuntime(17110): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
04-28 05:41:47.465: E/AndroidRuntime(17110): at dalvik.system.NativeStart.main(Native Method)
04-28 05:41:47.465: E/AndroidRuntime(17110): Caused by: java.lang.NullPointerException
04-28 05:41:47.465: E/AndroidRuntime(17110): at com.daya.wetalk.MainActivity.onCreate(MainActivity.java:40)
04-28 05:41:47.465: E/AndroidRuntime(17110): at android.app.Activity.performCreate(Activity.java:5047)
04-28 05:41:47.465: E/AndroidRuntime(17110): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
04-28 05:41:47.465: E/AndroidRuntime(17110): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)
04-28 05:41:47.465: E/AndroidRuntime(17110): ... 11 more
答案 0 :(得分:0)
请检查您是否已声明editText&活动xml或片段xml中的按钮。如果是,那么所需的更改。
好的,那就是你出错的地方。你有你的ui组件片段,你在活动中初始化它们。放置此
tts= new TextToSpeech(this,(OnInitListener) this);
edit = (EditText)findViewById(R.id.mEdit);
b = (ImageButton)findViewById(R.id.mButton);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v){
incomingmessage=edit.getText().toString();
if (incomingmessage != null){
speakOut(incomingmessage);
}
}
});
在占位符片段的oncreate里面
如果您还不清楚Add Onclick listner to android ,请在此处查看我的答案将此变量声明为占位符类
中的全局变量private int TYPE_TEXT_FLAG_AUTO_CORRECT=1;
private TextToSpeech tts;
private EditText edit;
private ImageButton b;
String incomingmessage;