TableLayout在安卓棒棒糖上导致应用崩溃

5
我在一个活动布局中使用了TableLayout。但是,当我在Lollipop上尝试运行相同的活动时,它会崩溃。
在Marshmallow和Nougat上它能够完美运行。
当我尝试移除TableLayout(仅此一个)后,在Lollipop上也可以正常工作。
我尝试移除活动中所有Java代码,但问题依旧存在。 我的崩溃报告 E/AndroidRuntime: 致命错误: 主进程 进程: com.sovereignconsult.region.viand, PID: 19434 java.lang.NullPointerException: 尝试在空对象上调用 'int android.view.View.getVisibility()' 的虚方法 at android.widget.LinearLayout.forceUniformHeight(LinearLayout.java:1380) at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1368) at android.widget.TableRow.onMeasure(TableRow.java:114) at android.view.View.measure(View.java:17826) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5653) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436) at android.widget.TableLayout.measureChildBeforeLayout(TableLayout.java:464) at android.widget.LinearLayout.measureVertical(LinearLayout.java:722) at android.widget.TableLayout.measureVertical(TableLayout.java:476) at android.widget.TableLayout.onMeasure(TableLayout.java:439) at android.view.View.measure(View.java:17826) at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728) at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:464) at android.view.View.measure(View.java:17826) at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1269) at android.widget.FrameLayout.onMeasure(FrameLayout.java:430) at android.widget.ScrollView.onMeasure(ScrollView.java:338) at android.view.View.measure(View.java:17826) at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728) at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:464) at android.view.View.measure(View.java:17826) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5653) at android.widget.FrameLayout.onMeasure(FrameLayout.java:430) at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139) at android.view.View.measure(View.java:17826) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5653) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436) at android.widget.LinearLayout.measureVertical(LinearLayout.java:722) at android.widget.LinearLayout.onMeasure(LinearLayout.java:613) at android.view.View.measure(View.java:17826) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5653) at android.widget.FrameLayout.onMeasure(FrameLayout.java:430) at android.view.View.measure(View.java:17826) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5653) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436) at android.widget.LinearLayout.measureVertical(LinearLayout.java:722) at android.widget.LinearLayout.onMeasure(LinearLayout.java:613) at android.view.View.measure(View.java:17826) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5653) at android.widget.FrameLayout.onMeasure(FrameLayout.java:430) at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2748) at android.view.View.measure(View.java:17826) at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2030) at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1174) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1395) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1062) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5873) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767) at android.view.Choreographer.doCallbacks(Choreographer.java:580) at android.view.Choreographer.doFrame(Choreographer.java:550) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753) 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:5536) 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:1397) at com.android.internal.

布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/tools"
    xmlns:design="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<include android:id="@+id/toolbar"
    layout="@layout/top_bar"/>
<ScrollView
    android:id="@+id/productscrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="UselessParent"
    android:layout_below="@id/toolbar">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp">

        <TextView
            android:id="@+id/offeringNmae"
            android:textStyle="normal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="20dp"
            android:layout_marginBottom="20dp"
            android:textSize="23sp"
            android:textColor="@color/lildark"
            android:text="Name of Offering" />
        <TextView
            android:id="@+id/terms"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/orderTerms"
            android:layout_below="@id/offeringNmae"
            android:textSize="10sp"
            android:layout_marginBottom="5dp"/>



        <TableLayout
            android:id="@+id/orderDetails"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:stretchColumns="1"
            android:background="@drawable/cell_shape"
            android:layout_below="@id/terms"
            android:layout_marginBottom="10dp">
            <TableRow
                android:paddingTop="10dp"
                android:paddingBottom="10dp">
                <TextView
                    android:layout_column="1"
                    android:text="Details"
                    android:layout_marginStart="15dp"
                    android:textStyle="bold"/>
            </TableRow>
            <View
                android:layout_height="1dp"
                android:background="@color/lightGray"/>
            <TableRow
                android:paddingTop="10dp"
                android:paddingBottom="10dp">
                <TextView
                    android:id="@+id/orderName"
                    android:layout_column="1"
                    android:padding="3dip"
                    android:layout_marginStart="15dp"/>
                <TextView
                    android:id="@+id/quantity"
                    android:layout_column="2"
                    android:gravity="end"
                    android:padding="3dip"/>
                <TextView
                    android:id="@+id/price"
                    android:layout_column="3"
                    android:gravity="end"
                    android:padding="3dip"
                    android:layout_marginEnd="15dp"/>
            </TableRow>

            <View
                android:layout_height="1dp"
                android:background="@color/lightGray"/>
            <TableRow
                android:paddingTop="10dp"
                android:paddingBottom="10dp">
                <TextView
                    android:layout_column="1"
                    android:padding="3dip"
                    android:text="Total Amount"
                    android:layout_marginStart="15dp"/>
                <TextView
                    android:layout_column="3"
                    android:id="@+id/totalamount"
                    android:gravity="right"
                    android:padding="3dip"
                    android:layout_marginEnd="15dp"/>
            </TableRow>

        </TableLayout>
        <TableLayout
            android:id="@+id/orderAddress"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:stretchColumns="1"
            android:background="@drawable/cell_shape"
            android:layout_below="@id/orderDetails"
            android:layout_marginBottom="10dp">
            <TableRow
                android:paddingTop="10dp"
                android:paddingBottom="10dp">
                <TextView
                    android:layout_column="1"
                    android:padding="3dip"
                    android:text="Pickup Address"
                    android:layout_marginStart="15dp"
                    android:textStyle="bold"/>

            </TableRow>
            <View
                android:layout_height="1dp"
                android:background="@color/lightGray"/>
            <TableRow
                android:paddingTop="10dp"
                android:paddingBottom="10dp">

                <TextView
                    android:layout_column="1"
                    android:id="@+id/address"
                    android:layout_height="fill_parent"
                    android:layout_width="wrap_content"
                    android:layout_marginLeft="15dp"
                    android:layout_marginRight="15dp"/>
            </TableRow>
        </TableLayout>
        <TableLayout
            android:id="@+id/chefNote"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:stretchColumns="1"
            android:background="@drawable/cell_shape"
            android:layout_below="@id/orderAddress">
            <TableRow
                android:paddingTop="10dp"
                android:paddingBottom="10dp">
                <TextView
                    android:layout_column="1"
                    android:padding="3dip"
                    android:text="Note For Chef"
                    android:layout_marginStart="15dp"
                    android:textStyle="bold"/>

            </TableRow>
            <View
                android:layout_height="1dp"
                android:background="@color/lightGray"/>
            <TableRow
                android:paddingTop="10dp"
                android:paddingBottom="10dp">

                <TextView
                    android:layout_column="1"
                    android:id="@+id/ChefNote"
                    android:layout_height="fill_parent"
                    android:layout_width="wrap_content"
                    android:layout_marginLeft="15dp"
                    android:layout_marginRight="15dp"/>
            </TableRow>
        </TableLayout>


    </RelativeLayout>
</ScrollView>
<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_height="60dp"
    android:layout_width="match_parent"
    android:layout_alignParentBottom="true"
    app:itemBackground="@color/defaultText"
    android:background="@color/defaultText"
    android:paddingRight="10dp"
    android:paddingLeft="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:gravity="center_horizontal"
        android:weightSum="2">

        <Button
            android:id="@+id/bookbottom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/PayNow"
            android:layout_weight="1"
            android:background="@drawable/buttonstyledefaultcolor"
            android:layout_gravity="center"
            android:textColor="@color/colorPrimary"
            android:padding="0dp"
            android:layout_marginTop="2dp"/>
    </LinearLayout>


</android.support.design.widget.BottomNavigationView>

public class order_verification_activity extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_verification);
    }
}

您提供的错误表明您有一个java.lang.NullPointerException,请查看此链接以修复它 https://dev59.com/GnVC5IYBdhLWcg3wqzLV - Yamen Nassif
@YamenNassif 只有在没有 TableLayout 的情况下才能正常工作。 - parish
我知道你说你已经删除了Java代码,但是必须有一些代码来启动该活动。你能提供它吗? - Yamen Nassif
我已经更新了我的问题。 - parish
@parish 我已经将其恢复,随意编辑以匹配解决方案。 - Yamen Nassif
显示剩余10条评论
2个回答

2
在表格行内的文本视图中,删除layout_width和layout_height或将layout_height设置为"wrap_content"。
 <TableRow
            android:paddingTop="10dp"
            android:paddingBottom="10dp">

            <TextView
                android:layout_column="1"
                android:id="@+id/address"
                android:layout_height="fill_parent"
                android:layout_width="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"/>
        </TableRow>

此解决方案也适用于嵌入在表格中的按钮,在API 22上测试成功。 - rndStr

1

既然您说已经移除了所有的Java代码,那么我建议做些调整(我在答案中写了),您的代码中有一个 <TableLayout> 内部有一个 <View>,我怀疑这是错误的,请尝试调整您的代码以满足此要求(您在更多地方也有相同的情况,请全部更改):

<TableLayout
                android:id="@+id/orderDetails"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:stretchColumns="1"
                android:background="@drawable/cell_shape"
                android:layout_below="@id/terms"
                android:layout_marginBottom="10dp">
                <TableRow
                    android:paddingTop="10dp"
                    android:paddingBottom="10dp">
                    <TextView
                        android:layout_column="1"
                        android:text="Details"
                        android:layout_marginStart="15dp"
                        android:textStyle="bold"/>
                </TableRow>
                <View
                    android:layout_height="1dp"
                    android:background="@color/lightGray"/>
                <TableRow
                    android:paddingTop="10dp"
                    android:paddingBottom="10dp">
                    <TextView
                        android:id="@+id/orderName"
                        android:layout_column="1"
                        android:padding="3dip"
                        android:layout_marginStart="15dp"/>
                    <TextView
                        android:id="@+id/quantity"
                        android:layout_column="2"
                        android:gravity="end"
                        android:padding="3dip"/>
                    <TextView
                        android:id="@+id/price"
                        android:layout_column="3"
                        android:gravity="end"
                        android:padding="3dip"
                        android:layout_marginEnd="15dp"/>
                </TableRow>
                <TableRow
                    android:paddingTop="10dp"
                    android:paddingBottom="10dp">
                    <TextView
                        android:layout_column="1"
                        android:padding="3dip"
                        android:text="Total Amount"
                        android:layout_marginStart="15dp"/>
                    <TextView
                        android:layout_column="3"
                        android:id="@+id/totalamount"
                        android:gravity="right"
                        android:padding="3dip"
                        android:layout_marginEnd="15dp"/>
                </TableRow>

            </TableLayout>
            <TableLayout
                android:id="@+id/orderAddress"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:stretchColumns="1"
                android:background="@drawable/cell_shape"
                android:layout_below="@id/orderDetails"
                android:layout_marginBottom="10dp">
                <TableRow
                    android:paddingTop="10dp"
                    android:paddingBottom="10dp">
                    <TextView
                        android:layout_column="1"
                        android:text="Pickup Address"
                        android:layout_marginStart="15dp"
                        android:textStyle="bold"/>
                </TableRow>
                <View
                    android:layout_height="1dp"
                    android:background="@color/lightGray"/>
                <TableRow
                    android:paddingTop="10dp"
                    android:paddingBottom="10dp">
                    <TextView
                        android:id="@+id/address"
                        android:layout_column="1"
                        android:padding="3dip"
                        android:layout_marginStart="15dp"/>
                </TableRow>
            </TableLayout>
            <TableLayout
                android:id="@+id/noteForCheifa"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:stretchColumns="1"
                android:background="@drawable/cell_shape"
                android:layout_below="@id/orderAddress"
                android:layout_marginBottom="10dp">
                <TableRow
                    android:paddingTop="10dp"
                    android:paddingBottom="10dp">
                    <TextView
                        android:layout_column="1"
                        android:text="Note For Chef"
                        android:layout_marginStart="15dp"
                        android:textStyle="bold"/>
                </TableRow>
                <View
                    android:layout_height="1dp"
                    android:background="@color/lightGray"/>
                <TableRow
                    android:paddingTop="10dp"
                    android:paddingBottom="10dp">
                    <TextView
                        android:id="@+id/ChefNote"
                        android:layout_column="1"
                        android:padding="3dip"
                        android:layout_marginStart="15dp"/>
                </TableRow>
            </TableLayout>



            <TableLayout
                android:id="@+id/paymentmode"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:stretchColumns="1"
                android:background="@drawable/cell_shape"
                android:layout_below="@id/noteForCheifa"
                android:layout_marginBottom="90dp">
                <TableRow
                    android:paddingTop="10dp"
                    android:paddingBottom="10dp">
                    <TextView
                        android:layout_column="1"
                        android:text="Select Payment Method"
                        android:layout_marginStart="15dp"
                        android:textStyle="bold"/>
                </TableRow>
                <View
                    android:layout_height="1dp"
                    android:background="@color/lightGray"/>
                <TableRow
                    android:paddingTop="10dp"
                    android:paddingBottom="10dp">
                    <RadioGroup
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/RGroup">
                        <RadioButton
                            android:id="@+id/SelectTezUpi"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="@string/Tez"
                            android:buttonTint="@color/colorPrimary"/>
                        <RadioButton
                            android:id="@+id/paytm"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="Paytm"
                            android:buttonTint="@color/colorPrimary"/>
                    </RadioGroup>

                </TableRow>
            </TableLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接