Android Studio:尝试在第0行运行App,Supply_width属性时出错?

时间:2018-01-31 06:44:32

标签: java android xml android-studio

我似乎无法弄清楚为什么我的应用程序无法运行...我已经尝试使无效并重新启动,重建项目,并将布局宽度应用于所有内容,但它仍然没有& #39;工作。有什么东西可以解决这个问题吗?我对第0行问题的layout_width属性感到困惑,到目前为止还无法弄明白。

logcat的:

01-30 21:53:01.964 4622-4622/test.pkg.miclass.decode E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: test.pkg.miclass.decode, PID: 4622
                                                                      java.lang.RuntimeException: Unable to start activity ComponentInfo{test.pkg.miclass.decode/test.pkg.miclass.decode.MainActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: You must supply a layout_width attribute.
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
                                                                          at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                          at android.os.Looper.loop(Looper.java:164)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
                                                                       Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: You must supply a layout_width attribute.
                                                                       Caused by: java.lang.UnsupportedOperationException: Binary XML file line #0: You must supply a layout_width attribute.
                                                                          at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:775)
                                                                          at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:7554)
                                                                          at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:7746)
                                                                          at android.widget.LinearLayout$LayoutParams.<init>(LinearLayout.java:1976)
                                                                          at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:1874)
                                                                          at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:1872)
                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:865)
                                                                          at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
                                                                          at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                                                                          at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
                                                                          at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                                                                          at test.pkg.miclass.decode.MainActivity.onCreate(MainActivity.java:18)
                                                                          at android.app.Activity.performCreate(Activity.java:6999)
                                                                          at android.app.Activity.performCreate(Activity.java:6990)
                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
                                                                          at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                          at android.os.Looper.loop(Looper.java:164)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

这是我到目前为止的xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="383dp"
    android:layout_height="510dp"
    tools:context="test.pkg.miclass.decode.MainActivity">

    <LinearLayout
        android:layout_width="363dp"
        android:layout_height="55dp"
        android:orientation="horizontal"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Text:"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

        <EditText
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="textPersonName" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="368dp"
        android:layout_height="54dp"
        android:orientation="horizontal"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="75dp">

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="KeyPhrase:"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

        <EditText
            android:id="@+id/keyphrase"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="textPersonName" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="368dp"
        android:layout_height="55dp"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="136dp">

        <RadioGroup
            android:id="@+id/encryptGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight=".7"
            android:gravity="center">

            <RadioButton
                android:id="@+id/encrypt"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:checked="true"
                android:text="Encrypt" />

            <RadioButton
                android:id="@+id/decrypt"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="RadioButton"
                tools:text="Decrypt" />
        </RadioGroup>

    </LinearLayout>

    <LinearLayout
        android:layout_width="368dp"
        android:layout_height="68dp"
        android:orientation="horizontal"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="207dp">

        <Button
            android:id="@+id/runButton"
            style="@style/Widget.AppCompat.Button"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Run" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="368dp"
        android:layout_height="63dp"
        android:orientation="horizontal"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="285dp">

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Results:"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

        <EditText
            android:id="@+id/result"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="textPersonName" />
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

非常感谢任何帮助!

3 个答案:

答案 0 :(得分:1)

为按钮指定宽度 - :

添加此行 - :

android:layout_width="wrap_content"

<Button
            android:id="@+id/runButton"
            style="@style/Widget.AppCompat.Button"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Run" />

答案 1 :(得分:1)

你用两个相同的id声明两个TextView:id =&#34; @ + id / textView2&#34;

 <Button
                android:id="@+id/runButton"
                style="@style/Widget.AppCompat.Button"
                android:layout_height="match_parent"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:text="Run"/>

答案 2 :(得分:0)

在视图中

 <Button
        android:id="@+id/runButton"
        style="@style/Widget.AppCompat.Button"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="Run" />

您添加了行android:layout_width="wrap_content" 例如:

<Button
            android:id="@+id/runButton"
            style="@style/Widget.AppCompat.Button"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:text="Run" />