如何修复运行时错误“您必须提供layout_width属性”。在android?

时间:2016-11-22 15:22:05

标签: android runtime-error

我有一个在AndroidStudio中编译的Android应用程序,但是当我调用某个活动时,我收到以下错误:

11-22 16:08:40.461 13796-13796/com.impyiablue.stoxx E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: com.impyiablue.stoxx, PID: 13796
                                                                      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.impyiablue.stoxx/com.impyiablue.stoxx.EditEntryActivity}: java.lang.RuntimeException: Binary XML file line #25: You must supply a layout_width attribute.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
at android.app.ActivityThread.access$800(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5272)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
Caused by: java.lang.RuntimeException: Binary XML file line #25: You must supply a layout_width attribute.
at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:606)
at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:6422)
at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:6591)
at android.widget.LinearLayout$LayoutParams.<init>(LinearLayout.java:1866)
at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:1778)
at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:62)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:808)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.impyiablue.stoxx.EditEntryActivity.onCreate(EditEntryActivity.java:51)
at android.app.Activity.performCreate(Activity.java:5977)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2258)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365) 
at android.app.ActivityThread.access$800(ActivityThread.java:148) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5272) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704) 
11-22 16:10:02.231 15805-15805/com.impyiablue.stoxx:remote E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.impyiablue.stoxx:remote, PID: 15805
                                                                             java.lang.NumberFormatException: Invalid float: "N/A"
                                                                                 at java.lang.StringToReal.invalidReal(StringToReal.java:63)
                                                                                 at java.lang.StringToReal.initialParse(StringToReal.java:164)
                                                                                 at java.lang.StringToReal.parseFloat(StringToReal.java:323)
                                                                                 at java.lang.Float.parseFloat(Float.java:306)
                                                                                 at com.impyiablue.stoxx.Tools.ServiceStock$ResponseListener.onResponse(ServiceStock.java:86)
                                                                                 at com.impyiablue.stoxx.Tools.ServiceStock$ResponseListener.onResponse(ServiceStock.java:71)
                                                                                 at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:67)
                                                                                 at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
                                                                                 at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
                                                                                 at android.os.Handler.handleCallback(Handler.java:739)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                 at android.os.Looper.loop(Looper.java:135)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5272)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)

可以找到相应的xml文件here。我没有看到这个xml文件有任何问题。那么如何解决这个问题呢?在最新版本之前我也做了干净......

2 个答案:

答案 0 :(得分:4)

查看XML布局的第25行,TextView ID textTitleEdit缺少必需的layout_widthlayout_height属性。下一个TextView也缺少相同的属性。

答案 1 :(得分:3)

  

我有一个在AndroidStudio中编译的Android应用程序

项目编译保证其正确性,这也包括布局。即使ie layout_width在运行时对于对象也是强制性的,在编译期间缺少这样的不是错误,但是它通常是由于ommision /错误的结果。您可能只需要故意省略这些属性,以后愿意在运行时从运行代码中添加它们。没有编译器/静态分析器能够解决这个问题,所以lint会抱怨,但编译阶段不会失败。

为了避免此类问题,您可以尝试使用lint工具来捕获所有潜在问题,但是agaim,缺少给定属性不是错误,因此它不会导致任何编译错误。 layout_heightlayout_width在运行时是必需的,因此ViewGroup知道如何布局所有子项,并且因为大多数TextView都没有通过样式直接或间接地提供任何这些子项布局可以被视为有缺陷。