gpt4 book ai didi

java - 不幸的是,(应用程序名称)已停止 - Android 开发错误

转载 作者:行者123 更新时间:2023-12-02 06:47:48 26 4
gpt4 key购买 nike

我正在学习开发 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

最佳答案

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

您为 Activity 设置的 XML 布局文件包含一个没有 layout_height 属性的 View 。每个 View 都需要 layout_heightlayout_width

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

08-23 17:51:36.196: E/AndroidRuntime(800): 引起原因:java.lang.RuntimeException: 二进制 XML 文件第 #12 行:您必须提供 layout_height 属性。

关于java - 不幸的是,(应用程序名称)已停止 - Android 开发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18412526/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com