Java 不幸的是,(应用程序名称)已停止 - Android 开发错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18412526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unfortunately, (app name) has stopped - Android development error
提问by project_legacy
I am learning to develop Android applications and have come across a problem in my first project. I am following a tutorial (Derek Banas) to create an app that will allow the user to type in some text, hit a button and then the app will output that text. I have written the code and have recieved no errors, but whenever I run the app on the emulator, it says "Unfortunately, (app name) has stopped. Here is the log cat:
我正在学习开发 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.access0(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
采纳答案by Daniel Gabriel
The error comes from your MainActivitys
method setContentView(...).
错误来自您的MainActivitys
方法setContentView(...).
The XML layout file you are setting for your Activity contains a view that does not have a layout_height
property. layout_height
and layout_width
are required on every view.
您为活动设置的 XML 布局文件包含一个没有layout_height
属性的视图。layout_height
并且layout_width
在每个视图中都是必需的。
This information is available in the LogCat excerpt you posted:
此信息可在您发布的 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.
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.