问题描述
我是android编程的新手.我试图创建一个秒表应用程序,但是最近我在布局中出现了错误.我已经调试此布局错误2天了,但仍然找不到解决方案.我不认为这是IDE问题,因为当我使用手机对其进行测试时,程序停止了(错误).
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).
请查看以下错误:
java.lang.NullPointerException android.widget.LinearLayout.forceUniformHeight(LinearLayout.java:1380)
android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1368)在android.widget.TableRow.onMeasure(TableRow.java:114)在android.view.View.measure(View.java:17430)在android. view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)在android.widget.TableLayout.measureChildBeforeLayout(TableLayout.java:464)在android.widget.LinearLayout.measureVertical (LinearLayout.java:722)在android.widget.TableLayout.measureVertical(TableLayout.java:476)在android.widget.TableLayout.onMeasure(TableLayout.java:439)在android.view.View.measure(View.java: 17430)在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)在android.widget.FrameLayout.onMeasure(FrameLayout.java:430)在android.view.View.measure(View.java:17430)在android.在Andro的android.widget.LinearLayout.onMeasure(LinearLayout.java:613)的widget.LinearLayout.measureVertical(LinearLayout.java:875) id.view.View.measure(View.java:17430)在android.widget.LinearLayout.measureVertical(LinearLayout.java:875)在android.widget.LinearLayout.onMeasure(LinearLayout.java:613)在android.view.View .measure(View.java:17430)
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)
这是我的代码:
<?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
推荐答案
TableRow的子级不需要在XML文件中指定layout_width和layout_height属性. TableRow始终将这些值分别强制设置为MATCH_PARENT和WRAP_CONTENT.
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.
尝试:
<?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>
希望有帮助!
这篇关于ANDROID呈现问题NullException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!