0

我正在学习开发 Android 应用程序,并且在我的第一个项目中遇到了一个问题。我正在按照教程(Derek Banas)创建一个应用程序,该应用程序允许用户输入一些文本,点击一个按钮,然后应用程序将输出该文本。我已经编写了代码并且没有收到任何错误,但是每当我在模拟器上运行该应用程序时,它都会显示“不幸的是,(应用程序名称)已停止。这是日志猫:

08-23 17:51:36.026: D/AndroidRuntime(800): Shutting down VM
08-23 17:51:36.063: W/dalvikvm(800): threadid=1: thread exiting with uncaught exception     (group=0x414c4700)
08-23 17:51:36.196: E/AndroidRuntime(800): FATAL EXCEPTION: main
08-23 17:51:36.196: E/AndroidRuntime(800): java.lang.RuntimeException: Unable to start      activity ComponentInfo{com.nick.android.nick/com.nick.android.nick.MainActivity}:     java.lang.RuntimeException: Binary XML file line #12: You must supply a layout_height    attribute.
08-23 17:51:36.196: E/AndroidRuntime(800):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-23 17:51:36.196: E/AndroidRuntime(800):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-23 17:51:36.196: E/AndroidRuntime(800):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-23 17:51:36.196: E/AndroidRuntime(800):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-23 17:51:36.196: E/AndroidRuntime(800):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 17:51:36.196: E/AndroidRuntime(800):  at android.os.Looper.loop(Looper.java:137)
08-23 17:51:36.196: E/AndroidRuntime(800):  at android.app.ActivityThread.main(ActivityThread.java:5103)
08-23 17:51:36.196: E/AndroidRuntime(800):  at java.lang.reflect.Method.invokeNative(Native Method)
08-23 17:51:36.196: E/AndroidRuntime(800):  at java.lang.reflect.Method.invoke(Method.java:525)
08-23 17:51:36.196: E/AndroidRuntime(800):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-23 17:51:36.196: E/AndroidRuntime(800):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-23 17:51:36.196: E/AndroidRuntime(800):  at dalvik.system.NativeStart.main(Native Method)
08-23 17:51:36.196: E/AndroidRuntime(800): Caused by: java.lang.RuntimeException: Binary XML file line #12: You must supply a layout_height attribute.
08-23 17:51:36.196: E/AndroidRuntime(800):  at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:491)
08-23 17:51:36.196: E/AndroidRuntime(800):  at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:5820)
08-23 17:51:36.196: E/AndroidRuntime(800):  at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:5988)
08-23 17:51:36.196: E/AndroidRuntime(800):  at android.widget.LinearLayout$LayoutParams.<init>(LinearLayout.java:1831)
08-23 17:51:36.196: E/AndroidRuntime(800):  at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:1743)
08-23 17:51:36.196: E/AndroidRuntime(800):  at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:58)
08-23 17:51:36.196: E/AndroidRuntime(800):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:757)
08-23 17:51:36.196: E/AndroidRuntime(800):  at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
08-23 17:51:36.196: E/AndroidRuntime(800):  at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
08-23 17:51:36.196: E/AndroidRuntime(800):  at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
08-23 17:51:36.196: E/AndroidRuntime(800):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
08-23 17:51:36.196: E/AndroidRuntime(800):  at android.app.Activity.setContentView(Activity.java:1895)
08-23 17:51:36.196: E/AndroidRuntime(800):  at com.nick.android.nick.MainActivity.onCreate(MainActivity.java:17)
08-23 17:51:36.196: E/AndroidRuntime(800):  at android.app.Activity.performCreate(Activity.java:5133)
08-23 17:51:36.196: E/AndroidRuntime(800):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-23 17:51:36.196: E/AndroidRuntime(800):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-23 17:51:36.196: E/AndroidRuntime(800):  ... 11 more
4

1 回答 1

2

错误来自您的MainActivitys方法setContentView(...).

您为 Activity 设置的 XML 布局文件包含一个没有layout_height属性的视图。layout_height并且layout_width在每个视图上都需要。

此信息可在您发布的 LogCat 摘录中找到:

08-23 17:51:36.196: E/AndroidRuntime(800): Caused by: java.lang.RuntimeException: Binary XML file line #12: You must supply a layout_height attribute.

于 2013-08-23T22:02:42.187 回答