ANDROID Rendering problems NullException

2019-05-25 09:22发布

问题:

I am a newbie in android programming. I tried to create a stopwatch application, but recently i got an error in the layout. I have been debugging this layout error for 2 days and still cannot find the solution. I do not think this is IDE problem because when i tested it using my phone, the program stopped(error).

Please take a look of the error below:

java.lang.NullPointerException   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:17430)   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)   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:17430)   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)   at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)   at android.view.View.measure(View.java:17430)   at android.widget.LinearLayout.measureVertical(LinearLayout.java:875)   at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)   at android.view.View.measure(View.java:17430)   at android.widget.LinearLayout.measureVertical(LinearLayout.java:875)   at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)   at android.view.View.measure(View.java:17430)

this is my code:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    style="@style/activity_style">

        <TableRow
            android:layout_height="0dp"
            android:layout_width="match_parent"
            android:layout_weight="2">
            <EditText
                style="@style/edit_text_timer"
                android:id="@+id/etHour"
                android:hint="@string/initial_value"
                android:layout_height="match_parent"
                android:layout_width="0dp"
                android:layout_weight="3"
                android:layout_column="6"
             />

            <TextView
                android:id="@+id/tvColon"
                android:layout_height="match_parent"
                android:layout_width="0dp"
                android:text="@string/colon_text_view"
                android:gravity="center"
                android:textColor="#e7ff00"
                android:textSize="30sp"
                android:layout_weight="1"/>

            <EditText
                style="@style/edit_text_timer"
                android:id="@+id/etMinute"
                android:hint="@string/initial_value"
                android:layout_height="match_parent"
                android:layout_width="0dp"
                android:layout_weight="3"
                android:layout_column="13" />


            <TextView
                android:id="@id/tvColon"
                android:layout_height="match_parent"
                android:layout_width="0dp"
                android:text="@string/colon_text_view"
                android:gravity="center"
                android:textColor="#e7ff00"
                android:textSize="30sp"
                android:layout_weight="1"
                android:layout_column="18" />

            <EditText
                style="@style/edit_text_timer"
                android:id="@+id/etSecond"
                android:hint="@string/initial_value"
                android:layout_height="match_parent"
                android:layout_width="0dp"
                android:layout_weight="3"
                />
        </TableRow>

        <TableRow
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_width="match_parent">
            <Button
                style="@style/button_text"
                android:id="@+id/button1"
                android:layout_width="0dp"
                android:text="@string/button_1"
                android:layout_weight="1"
                android:onClick="buttonNumber"/>
            <Button
                style="@style/button_text"
                android:id="@+id/button2"
                android:layout_width="0dp"
                android:text="@string/button_2"

                android:layout_weight="1"
                android:onClick="buttonNumber"/>
            <Button
                style="@style/button_text"
                android:id="@+id/button3"
                android:layout_width="0dp"
                android:text="@string/button_3"
                android:layout_weight="1"
                android:onClick="buttonNumber"/>

        </TableRow>

        <TableRow
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_width="match_parent">
            <Button
                style="@style/button_text"
                android:id="@+id/button4"
                android:layout_width="0dp"
                android:text="@string/button_4"
                android:layout_weight="1"
                android:onClick="buttonNumber"
                />
            <Button
                style="@style/button_text"
                android:id="@+id/button5"
                android:layout_width="0dp"
                android:text="@string/button_5"
                android:layout_weight="1"
                android:onClick="buttonNumber"/>
            <Button
                style="@style/button_text"
                android:id="@+id/button6"
                android:layout_width="0dp"
                android:text="@string/button_6"
                android:layout_weight="1"
                android:onClick="buttonNumber"/>

        </TableRow>

        <TableRow
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_width="match_parent">
            <Button
               style="@style/button_text"
                android:id="@+id/button7"
                android:layout_width="0dp"
                android:text="@string/button_7"
                android:layout_weight="1"
                android:onClick="buttonNumber"/>
            <Button
               style="@style/button_text"
                android:id="@+id/button8"
                android:layout_width="0dp"
                android:text="@string/button_8"
                android:layout_weight="1"
                android:onClick="buttonNumber"/>
            <Button
                style="@style/button_text"
                android:id="@+id/button9"
                android:layout_width="0dp"
                android:text="@string/button_9"
                android:layout_weight="1"
                android:onClick="buttonNumber"/>
        </TableRow>

        <TableRow
            android:layout_weight="1"
            android:layout_height="0dp"
            android:layout_width="match_parent">
            <Button
                style="@style/button_text"
                android:id="@+id/emptyButton"
                android:layout_width="0dp"
                android:text=""
                android:layout_weight="1"/>
            <Button
                style="@style/button_text"
                android:id="@+id/button0"
                android:layout_width="0dp"
                android:text="@string/button_0"
                android:layout_weight="1"
                android:onClick="buttonNumber"/>
            <Button
                style="@style/button_text"
                android:id="@+id/buttonReset"
                android:layout_width="0dp"
                android:text="@string/button_reset_text"
                android:layout_weight="1"/>

        </TableRow>

        <TableRow
            android:layout_weight="1"
            android:layout_height="0dp"
            android:layout_width="match_parent">
            <Button
                style="@style/button_text"
                android:layout_marginTop="10dp"
                android:id="@+id/startStopButton"
                android:layout_width="0dp"
                android:text="@string/button_start"
                android:layout_weight="1"/>

            <Button
                style="@style/button_text"
                android:layout_marginTop="10dp"
                android:id="@+id/resetButton"
                android:layout_width="0dp"
                android:text="@string/button_reset"
                android:layout_weight="1"/>

        </TableRow>




</TableLayout>

    enter code here

回答1:

The children of a TableRow do not need to specify the layout_width and layout_height attributes in the XML file. TableRow always enforces those values to be respectively MATCH_PARENT and WRAP_CONTENT.

Try that:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:paddingLeft="@dimen/activity_horizontal_margin"
         android:paddingRight="@dimen/activity_horizontal_margin"
         android:paddingTop="@dimen/activity_vertical_margin"
         android:paddingBottom="@dimen/activity_vertical_margin"
         tools:context=".MainActivity"
         style="@style/activity_style">

<TableRow
    android:layout_height="0dp"
    android:layout_width="match_parent"
    android:layout_weight="2">
    <EditText
        style="@style/edit_text_timer"
        android:id="@+id/etHour"
        android:hint="@string/initial_value"
        android:layout_weight="3"
        android:layout_column="6"
        />

    <TextView
        android:id="@+id/tvColon"
        android:text="@string/colon_text_view"
        android:gravity="center"
        android:textColor="#e7ff00"
        android:textSize="30sp"
        android:layout_weight="1"/>

    <EditText
        style="@style/edit_text_timer"
        android:id="@+id/etMinute"
        android:hint="@string/initial_value"
        android:layout_weight="3"
        android:layout_column="13" />


    <TextView
        android:id="@id/tvColon"
        android:text="@string/colon_text_view"
        android:gravity="center"
        android:textColor="#e7ff00"
        android:textSize="30sp"
        android:layout_weight="1"
        android:layout_column="18" />

    <EditText
        style="@style/edit_text_timer"
        android:id="@+id/etSecond"
        android:hint="@string/initial_value"
        android:layout_weight="3"
        />
</TableRow>

<TableRow
    android:layout_height="0dp"
    android:layout_weight="1"
    android:layout_width="match_parent">
    <Button
        style="@style/button_text"
        android:id="@+id/button1"
        android:text="@string/button_1"
        android:layout_weight="1"
        android:onClick="buttonNumber"/>
    <Button
        style="@style/button_text"
        android:id="@+id/button2"
        android:text="@string/button_2"
        android:layout_weight="1"
        android:onClick="buttonNumber"/>
    <Button
        style="@style/button_text"
        android:id="@+id/button3"
        android:text="@string/button_3"
        android:layout_weight="1"
        android:onClick="buttonNumber"/>

</TableRow>

<TableRow
    android:layout_height="0dp"
    android:layout_weight="1"
    android:layout_width="match_parent">
    <Button
        style="@style/button_text"
        android:id="@+id/button4"
        android:text="@string/button_4"
        android:layout_weight="1"
        android:onClick="buttonNumber"
        />
    <Button
        style="@style/button_text"
        android:id="@+id/button5"
        android:text="@string/button_5"
        android:layout_weight="1"
        android:onClick="buttonNumber"/>
    <Button
        style="@style/button_text"
        android:id="@+id/button6"
        android:text="@string/button_6"
        android:layout_weight="1"
        android:onClick="buttonNumber"/>

</TableRow>

<TableRow
    android:layout_height="0dp"
    android:layout_weight="1"
    android:layout_width="match_parent">
    <Button
        style="@style/button_text"
        android:id="@+id/button7"
        android:text="@string/button_7"
        android:layout_weight="1"
        android:onClick="buttonNumber"/>
    <Button
        style="@style/button_text"
        android:id="@+id/button8"
        android:text="@string/button_8"
        android:layout_weight="1"
        android:onClick="buttonNumber"/>
    <Button
        style="@style/button_text"
        android:id="@+id/button9"
        android:text="@string/button_9"
        android:layout_weight="1"
        android:onClick="buttonNumber"/>
</TableRow>

<TableRow
    android:layout_weight="1"
    android:layout_height="0dp"
    android:layout_width="match_parent">
    <Button
        style="@style/button_text"
        android:id="@+id/emptyButton"
        android:text=""
        android:layout_weight="1"/>
    <Button
        style="@style/button_text"
        android:id="@+id/button0"
        android:text="@string/button_0"
        android:layout_weight="1"
        android:onClick="buttonNumber"/>
    <Button
        style="@style/button_text"
        android:id="@+id/buttonReset"
        android:text="@string/button_reset_text"
        android:layout_weight="1"/>

</TableRow>

<TableRow
    android:layout_weight="1"
    android:layout_height="0dp"
    android:layout_width="match_parent">
    <Button
        style="@style/button_text"
        android:layout_marginTop="10dp"
        android:id="@+id/startStopButton"
        android:text="@string/button_start"
        android:layout_weight="1"/>

    <Button
        style="@style/button_text"
        android:layout_marginTop="10dp"
        android:id="@+id/resetButton"
        android:text="@string/button_reset"
        android:layout_weight="1"/>

</TableRow>
</TableLayout>

Hope it helps!