编译时,Eclipse IDE中没有显示错误。但是当我运行它时,它会一直崩溃。为什么?
我不知道该怎么办,因为没有错误显示。它只是在它开始之前运行和压碎。
public class MainActivity extends Activity implements OnClickListener,
OnInitListener {
private TextToSpeech tts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tts = new TextToSpeech(this, this);
this.initializeTextToSpeech("Hello World");
}
@Override
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
protected void onDestroy() {
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if (status == TextToSpeech.SUCCESS) {
tts.setLanguage(Locale.getDefault());
} else {
tts = null;
Toast.makeText(this, "Failed to initialize TTS engine.",
Toast.LENGTH_SHORT).show();
}
Log.d("onINIT", "started");
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
public void initializeTextToSpeech(String str) {
if (tts != null) {
String text = str;
if (text != null) {
if (!tts.isSpeaking()) {
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
}
}
}
这是我的LogCat:
12-21 01:53:07.698: I/Process(1467): Sending signal. PID: 1467 SIG: 9
12-21 01:53:07.870: E/Trace(1656): error opening trace file: No such file or directory (2)
12-21 01:53:07.962: D/AndroidRuntime(1656): Shutting down VM
12-21 01:53:07.962: W/dalvikvm(1656): threadid=1: thread exiting with uncaught exception (group=0xa61d2908)
12-21 01:51:16.462: E/AndroidRuntime(1467): FATAL EXCEPTION: main
12-21 01:51:16.462: E/AndroidRuntime(1467): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.testapp.testapp/com.testapp.testapp.MainActivity}: java.lang.RuntimeException: Binary XML file line #11: You must supply a layout_width attribute.
12-21 01:51:16.462: E/AndroidRuntime(1467): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-21 01:51:16.462: E/AndroidRuntime(1467): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-21 01:51:16.462: E/AndroidRuntime(1467): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-21 01:51:16.462: E/AndroidRuntime(1467): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-21 01:51:16.462: E/AndroidRuntime(1467): at android.os.Handler.dispatchMessage(Handler.java:99)
12-21 01:51:16.462: E/AndroidRuntime(1467): at android.os.Looper.loop(Looper.java:137)
12-21 01:51:16.462: E/AndroidRuntime(1467): at android.app.ActivityThread.main(ActivityThread.java:5041)
12-21 01:51:16.462: E/AndroidRuntime(1467): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 01:51:16.462: E/AndroidRuntime(1467): at java.lang.reflect.Method.invoke(Method.java:511)
12-21 01:51:16.462: E/AndroidRuntime(1467): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-21 01:51:16.462: E/AndroidRuntime(1467): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-21 01:51:16.462: E/AndroidRuntime(1467): at dalvik.system.NativeStart.main(Native Method)
12-21 01:51:16.462: E/AndroidRuntime(1467): Caused by: java.lang.RuntimeException: Binary XML file line #11: You must supply a layout_width attribute.
12-21 01:51:16.462: E/AndroidRuntime(1467): at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:491)
12-21 01:51:16.462: E/AndroidRuntime(1467): at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:5614)
12-21 01:51:16.462: E/AndroidRuntime(1467): at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:5756)
12-21 01:51:16.462: E/AndroidRuntime(1467): at android.widget.RelativeLayout$LayoutParams.<init>(RelativeLayout.java:1190)
12-21 01:51:16.462: E/AndroidRuntime(1467): at android.widget.RelativeLayout.generateLayoutParams(RelativeLayout.java:1028)
12-21 01:51:16.462: E/AndroidRuntime(1467): at android.widget.RelativeLayout.generateLayoutParams(RelativeLayout.java:70)
12-21 01:51:16.462: E/AndroidRuntime(1467): at android.view.LayoutInflater.rInflate(LayoutInflater.java:748)
12-21 01:51:16.462: E/AndroidRuntime(1467): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
12-21 01:51:16.462: E/AndroidRuntime(1467): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
12-21 01:51:16.462: E/AndroidRuntime(1467): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
12-21 01:51:16.462: E/AndroidRuntime(1467): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
12-21 01:51:16.462: E/AndroidRuntime(1467): at android.app.Activity.setContentView(Activity.java:1881)
12-21 01:51:16.462: E/AndroidRuntime(1467): at com.testapp.testapp.MainActivity.onCreate(MainActivity.java:23)
12-21 01:51:16.462: E/AndroidRuntime(1467): at android.app.Activity.performCreate(Activity.java:5104)
12-21 01:51:16.462: E/AndroidRuntime(1467): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-21 01:51:16.462: E/AndroidRuntime(1467): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-21 01:51:16.462: E/AndroidRuntime(1467): ... 11 more
我的代码基于此:
http://www.techrepublic.com/blog/software-engineer/teach-your-next-android-app-to-speak/
答案 0 :(得分:2)
您的错误原因在于LogCat:
Binary XML file line #11: You must supply a layout_width attribute.
在您在应用程序中引用的其中一个布局中,您没有定义layout_width
属性。定义它,错误就会消失。