textviews的背景不占用整个空间。我将layout_height
设置为match_parent
,但这会导致NullPointerException(Android Studio中的onMeasure错误)
造成这种情况的原因是什么?
以下是其中一个文字视图的示例:
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/highEleven"
android:id="@+id/highOneOne"
android:layout_column="2"
android:background="@color/blueRisk"
android:autoText="false"
android:clickable="false"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:paddingRight="5dp" />
整个XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_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="aetherum.com.take5init.riskAssessment"
android:minWidth="300dp"
>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/scrollView2" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/horizontalScrollView" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Severity of Incident"
android:id="@+id/textView17"
android:layout_column="1"
android:layout_span="6"
android:textAlignment="center"
android:textStyle="bold"
android:gravity="center" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Likelihood of incident"
android:id="@+id/textView2"
android:layout_column="1"
android:outlineProvider="bounds"
android:padding="5dp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Low"
android:id="@+id/textView3"
android:layout_column="2"
android:padding="5dp"
android:background="@color/lightGrey" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Minor"
android:id="@+id/textView4"
android:layout_column="3"
android:padding="5dp"
android:background="@color/lightGrey" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Moderate"
android:id="@+id/textView5"
android:layout_column="4"
android:background="@color/lightGrey"
android:padding="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Major"
android:id="@+id/textView6"
android:layout_column="5"
android:padding="5dp"
android:background="@color/lightGrey" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Critical"
android:id="@+id/textView7"
android:layout_column="6"
android:padding="5dp"
android:background="@color/lightGrey" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Almost \nCertain"
android:id="@+id/textView8"
android:layout_column="1"
android:padding="5dp"
android:background="@color/lightGrey" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/highEleven"
android:id="@+id/highOneOne"
android:layout_column="2"
android:background="@color/blueRisk"
android:autoText="false"
android:clickable="false"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:paddingRight="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/highSixteen"
android:id="@+id/highTwoOne"
android:layout_column="3"
android:background="@color/blueRisk"
android:autoText="false"
android:clickable="false"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:paddingRight="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/extremeTwenty"
android:id="@+id/extremeThreeOne"
android:layout_column="4"
android:background="@color/redRisk"
android:autoText="false"
android:clickable="false"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:paddingRight="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/extremeTwentyThree"
android:id="@+id/extremeFourOne"
android:layout_column="5"
android:background="@color/redRisk"
android:autoText="false"
android:clickable="false"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:paddingRight="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/extremeTwentyFive"
android:id="@+id/extremeFiveOne"
android:layout_column="6"
android:background="@color/redRisk"
android:autoText="false"
android:clickable="false"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:paddingRight="5dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Likely"
android:id="@+id/textView9"
android:layout_column="1"
android:padding="5dp"
android:background="@color/lightGrey" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/moderateSeven"
android:id="@+id/moderateOneTwo"
android:layout_column="2"
android:background="@color/greenRisk"
android:autoText="false"
android:clickable="false"
android:padding="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/highTwelve"
android:id="@+id/highTwoTwo"
android:layout_column="3"
android:background="@color/blueRisk"
android:autoText="false"
android:clickable="false"
android:padding="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/highSeventeen"
android:id="@+id/highThreeTwo"
android:layout_column="4"
android:background="@color/blueRisk"
android:autoText="false"
android:clickable="false"
android:padding="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/extremeTwentyOne"
android:id="@+id/extremeFourTwo"
android:layout_column="5"
android:background="@color/redRisk"
android:autoText="false"
android:clickable="false"
android:padding="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/extremeTwentyFour"
android:id="@+id/extremeFiveTwo"
android:layout_column="6"
android:background="@color/redRisk"
android:autoText="false"
android:clickable="false"
android:padding="5dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Possible"
android:id="@+id/textView10"
android:layout_column="1"
android:padding="5dp"
android:background="@color/lightGrey" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/lowFour"
android:id="@+id/lowOneThree"
android:layout_column="2"
android:background="@color/yellowRisk"
android:autoText="false"
android:clickable="false"
android:singleLine="true"
android:padding="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/moderateEight"
android:id="@+id/moderateTwoThree"
android:layout_column="3"
android:background="@color/greenRisk"
android:autoText="false"
android:clickable="false"
android:padding="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/highThirteen"
android:id="@+id/highThreeThree"
android:layout_column="4"
android:background="@color/blueRisk"
android:autoText="false"
android:clickable="false"
android:padding="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/extremeEighteen"
android:id="@+id/extremeFourThree"
android:layout_column="5"
android:background="@color/redRisk"
android:autoText="false"
android:clickable="false"
android:padding="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/extremeTwentyTwo"
android:id="@+id/extremeFiveThree"
android:layout_column="6"
android:background="@color/redRisk"
android:autoText="false"
android:clickable="false"
android:padding="5dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Unlikely"
android:id="@+id/textView11"
android:layout_column="1"
android:padding="5dp"
android:background="@color/lightGrey" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/lowTwo"
android:id="@+id/lowOneFour"
android:layout_column="2"
android:background="@color/yellowRisk"
android:autoText="false"
android:clickable="false"
android:singleLine="true"
android:padding="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/lowFive"
android:id="@+id/lowTwoFour"
android:layout_column="3"
android:background="@color/yellowRisk"
android:autoText="false"
android:clickable="false"
android:singleLine="true"
android:padding="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/moderateNine"
android:id="@+id/moderateThreeFour"
android:layout_column="4"
android:background="@color/greenRisk"
android:autoText="false"
android:clickable="false"
android:singleLine="true"
android:padding="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/highFourteen"
android:id="@+id/highFourFour"
android:layout_column="5"
android:background="@color/blueRisk"
android:autoText="false"
android:clickable="false"
android:padding="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/extremeEighteen"
android:id="@+id/extremeFiveFour"
android:layout_column="6"
android:background="@color/redRisk"
android:autoText="false"
android:clickable="false"
android:padding="5dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Rare"
android:id="@+id/textView12"
android:layout_column="1"
android:padding="5dp"
android:background="@color/lightGrey" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/lowOne"
android:id="@+id/lowOneFive"
android:layout_column="2"
android:background="@color/yellowRisk"
android:autoText="false"
android:clickable="false"
android:singleLine="true"
android:padding="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/lowThree"
android:id="@+id/lowTwoFive"
android:layout_column="3"
android:background="@color/yellowRisk"
android:autoText="false"
android:clickable="false"
android:singleLine="true"
android:padding="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/moderateSix"
android:id="@+id/moderateThreeFive"
android:layout_column="4"
android:background="@color/greenRisk"
android:autoText="false"
android:clickable="false"
android:singleLine="true"
android:padding="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/highTen"
android:id="@+id/highFourFive"
android:layout_column="5"
android:background="@color/blueRisk"
android:autoText="false"
android:clickable="false"
android:padding="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/highFifteen"
android:id="@+id/highFiveFive"
android:layout_column="6"
android:background="@color/blueRisk"
android:autoText="false"
android:clickable="false"
android:padding="5dp" />
</TableRow>
</TableLayout>
</HorizontalScrollView>
</RelativeLayout>
</ScrollView>
StackTrace:
java.lang.NullPointerException
at android.widget.LinearLayout.forceUniformHeight(LinearLayout.java:1409)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1397)
at android.widget.TableRow.onMeasure(TableRow.java:113)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.TableLayout.measureChildBeforeLayout(TableLayout.java:461)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.TableLayout.measureVertical(TableLayout.java:473)
at android.widget.TableLayout.onMeasure(TableLayout.java:436)
at android.view.View.measure(View.java:18788)
at android.widget.HorizontalScrollView.measureChildWithMargins(HorizontalScrollView.java:1259)