我最近开始编程,当我执行下面显示的基本 ImageView 和 TextView 时。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/texttry" />
</TextView>
<ImageView
android:id="@+id/joe"
android:src="@drawable/joe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
模拟器崩溃了,logcat发出了这个错误:
11-24 11:09:20.938: D/AndroidRuntime(713): Shutting down VM
11-24 11:09:20.938: W/dalvikvm(713): threadid=3: thread exiting with uncaught exception (group=0x4001aa28)
11-24 11:09:20.938: E/AndroidRuntime(713): Uncaught handler: thread main exiting due to uncaught exception
11-24 11:09:20.958: E/AndroidRuntime(713): java.lang.RuntimeException: Unable to start activity ComponentInfo{tristan.wuker.wrje/tristan.wuker.wrje.MrttestActivity}: java.lang.RuntimeException: Binary XML file line #10: You must supply a layout_width attribute.
11-24 11:09:20.958: E/AndroidRuntime(713): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
11-24 11:09:20.958: E/AndroidRuntime(713): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
11-24 11:09:20.958: E/AndroidRuntime(713): at android.app.ActivityThread.access$2100(ActivityThread.java:116)
11-24 11:09:20.958: E/AndroidRuntime(713): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
11-24 11:09:20.958: E/AndroidRuntime(713): at android.os.Handler.dispatchMessage(Handler.java:99)
11-24 11:09:20.958: E/AndroidRuntime(713): at android.os.Looper.loop(Looper.java:123)
11-24 11:09:20.958: E/AndroidRuntime(713): at android.app.ActivityThread.main(ActivityThread.java:4203)
11-24 11:09:20.958: E/AndroidRuntime(713): at java.lang.reflect.Method.invokeNative(Native Method)
11-24 11:09:20.958: E/AndroidRuntime(713): at java.lang.reflect.Method.invoke(Method.java:521)
11-24 11:09:20.958: E/AndroidRuntime(713): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
11-24 11:09:20.958: E/AndroidRuntime(713): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
11-24 11:09:20.958: E/AndroidRuntime(713): at dalvik.system.NativeStart.main(Native Method)
11-24 11:09:20.958: E/AndroidRuntime(713): Caused by: java.lang.RuntimeException: Binary XML file line #10: You must supply a layout_width attribute.
11-24 11:09:20.958: E/AndroidRuntime(713): at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:438)
11-24 11:09:20.958: E/AndroidRuntime(713): at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:3433)
11-24 11:09:20.958: E/AndroidRuntime(713): at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:3513)
11-24 11:09:20.958: E/AndroidRuntime(713): at android.widget.LinearLayout$LayoutParams.<init>(LinearLayout.java:1265)
11-24 11:09:20.958: E/AndroidRuntime(713): at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:1191)
11-24 11:09:20.958: E/AndroidRuntime(713): at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:45)
11-24 11:09:20.958: E/AndroidRuntime(713): at android.view.LayoutInflater.rInflate(LayoutInflater.java:619)
11-24 11:09:20.958: E/AndroidRuntime(713): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
11-24 11:09:20.958: E/AndroidRuntime(713): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
11-24 11:09:20.958: E/AndroidRuntime(713): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
11-24 11:09:20.958: E/AndroidRuntime(713): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:313)
11-24 11:09:20.958: E/AndroidRuntime(713): at android.app.Activity.setContentView(Activity.java:1620)
11-24 11:09:20.958: E/AndroidRuntime(713): at tristan.wuker.wrje.MrttestActivity.onCreate(MrttestActivity.java:11)
11-24 11:09:20.958: E/AndroidRuntime(713): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
11-24 11:09:20.958: E/AndroidRuntime(713): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
11-24 11:09:20.958: E/AndroidRuntime(713): ... 11 more
11-24 11:09:20.978: I/dalvikvm(713): threadid=7: reacting to signal 3
11-24 11:09:20.978: E/dalvikvm(713): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
我也是这个网站的新手,所以如果我发布了这个错误,我很抱歉。
此外,如果有必要知道,当我拿出ImageView时,文字就像魅力一样。
答案 0 :(得分:2)
我相信你最终会想到,日志会告诉你问题出在哪里。特别是这条线(我缩短了它适合):
11-24 11:09:20.958 .. Caused by.. #10: You must supply a layout_width attribute.
正如您在下面的代码段中看到的那样:
<TextView>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/texttry" />
</TextView>
在指定任何属性之前,您过早关闭了&lt; TextView \&gt; 元素。记录提到的是_layout_width _。
所有你需要做的就是解决这个问题&#39;从 TextView 元素开头删除右侧shevron,如下所示:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/texttry" />
</TextView>
PS:虽然fill_parent
已弃用了一小部分细节,但您应该使用match_parent
代替:
<TextView>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/texttry" />
</TextView>
答案 1 :(得分:0)
我刚刚尝试了您的代码,并且在我取出结束</TextView>
代码时似乎有效:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/texttry" />
<ImageView
android:id="@+id/joe"
android:src="@drawable/joe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>