Получение NullPointerException из моего макета в Android - PullRequest
0 голосов
/ 01 июня 2018

У меня есть TabHostAdapter, который содержит 3 фрагмента.Но второй фрагмент всегда вылетает, когда я использую андроид болотный телефон (или эмулятор).Я обнаружил, что сбой произошел из моего макета, но я не выяснил, почему .. Если я использую последний телефон с Android, проблем нет

Я получаю следующую ошибку:

java.lang.NullPointerException: попытка вызвать виртуальный метод int android.view.View.getVisibility () для нулевой ссылки на объект.

Вот моя функция:

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater!!.inflate(R.layout.fragment_second, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        //val activitySession : ActivitySession = arguments!!.getSerializable(Constants.ACTIVITY_SESSION) as ActivitySession
        //val stats = activitySession.statistic
        //showStats(stats)
        //val laps = activitySession.laps
        //showLaps(laps)

    }

И мой макет:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingBottom="120dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp"
            android:text="@string/statistics"
            android:textAllCaps="true"
            android:textColor="@color/white"
            android:textSize="16sp" />

        <training.gutai.apps.gutai.ui.CustomGridView
            android:id="@+id/statistics_grid"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:columnWidth="80dp"
            android:gravity="fill"
            android:horizontalSpacing="10dp"
            android:numColumns="auto_fit"
            android:paddingBottom="16dp"
            android:paddingTop="16dp"
            android:stretchMode="columnWidth"
            android:textAlignment="center"
            android:verticalSpacing="16dp">

        </training.gutai.apps.gutai.ui.CustomGridView>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp"
            android:text="@string/laps"
            android:textAllCaps="true"
            android:textColor="@color/white"
            android:textSize="16sp" />

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal">

            <TableRow>

                <TextView
                    android:layout_column="1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.5"
                    android:textAlignment="center"
                    android:textColor="@color/white"/>

                <ImageView
                    android:layout_column="2"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    app:srcCompat="@drawable/ic_duration" />

                <ImageView
                    android:layout_column="3"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.75"
                    android:layout_gravity="center"
                    app:srcCompat="@drawable/ic_distance" />

                <ImageView
                    android:layout_column="4"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:layout_gravity="center"
                    app:srcCompat="@drawable/ic_hr" />
            </TableRow>
            <TableRow>

                <TextView
                    android:layout_column="1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.5"
                    android:textAlignment="center"
                    android:textColor="@color/white" />

                <TextView
                    android:layout_column="2"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/duration"
                    android:textAlignment="center"
                    android:textColor="@color/white" />

                <TextView
                    android:layout_column="3"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.75"
                    android:text="@string/distance"
                    android:textAlignment="center"
                    android:textColor="@color/white" />

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:orientation="horizontal"
                    android:layout_column="4">

                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="0.5"
                        android:text="@string/hr_avg"
                        android:textAlignment="center"
                        android:textColor="@color/white" />

                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="0.5"
                        android:text="@string/hr_max"
                        android:textAlignment="center"
                        android:textColor="@color/white" />
                </LinearLayout>

            </TableRow>
            <TableRow>

                <TextView
                    android:layout_column="1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.5"
                    android:textAlignment="center"
                    android:textColor="@color/white" />

                <TextView
                    android:layout_column="2"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/duration_unit"
                    android:textAlignment="center"
                    android:textColor="@color/white" />

                <TextView
                    android:layout_column="3"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.75"
                    android:text="@string/distance_unit"
                    android:textAlignment="center"
                    android:textColor="@color/white" />

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="@string/hr_unit"
                    android:textAlignment="center"
                    android:textColor="@color/white" />

            </TableRow>
            <TableRow
                android:gravity="center"
                android:layout_gravity="center_horizontal"
                android:paddingTop="5dp">

                <training.gutai.apps.gutai.ui.LapsListView
                    android:layout_column="1"
                    android:id="@+id/lap_id_list"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_horizontal"
                    android:gravity="center_horizontal"
                    android:layout_weight="0.5"
                    android:textAlignment="center"/>

                <View
                    android:layout_height="fill_parent"
                    android:layout_width="2dp"
                    android:background="#FFFFFF"
                    android:padding="5dp"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="5dp"/>

                <training.gutai.apps.gutai.ui.LapsListView
                    android:layout_column="2"
                    android:id="@+id/duration_list"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_horizontal"
                    android:gravity="center_horizontal"
                    android:layout_weight="1"
                    android:textAlignment="center" />
                <View
                    android:layout_height="fill_parent"
                    android:layout_width="2dp"
                    android:background="#FFFFFF"
                    android:padding="5dp"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="5dp"/>

                <training.gutai.apps.gutai.ui.LapsListView
                    android:layout_column="3"
                    android:id="@+id/distance_list"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_horizontal"
                    android:gravity="center_horizontal"
                    android:layout_weight="0.75"
                    android:textAlignment="center" />
                <View
                    android:layout_height="fill_parent"
                    android:layout_width="2dp"
                    android:background="#FFFFFF"
                    android:padding="5dp"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="5dp"/>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:gravity="center_horizontal"
                    android:layout_weight="1"
                    android:orientation="horizontal"
                    android:layout_column="4">

                    <training.gutai.apps.gutai.ui.LapsListView
                        android:id="@+id/hr_moy_list"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:gravity="center_horizontal"
                        android:layout_weight="1"
                        android:textAlignment="center" />

                    <View
                        android:layout_height="fill_parent"
                        android:layout_width="2dp"
                        android:background="@color/dark_grey"
                        android:layout_marginLeft="5dp"
                        android:layout_marginRight="5dp"/>

                    <training.gutai.apps.gutai.ui.LapsListView
                        android:id="@+id/hr_max_list"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:gravity="center_horizontal"
                        android:layout_weight="1"
                        android:textAlignment="center" />
                </LinearLayout>

            </TableRow>
        </TableLayout>
    </LinearLayout>
</ScrollView>

РЕДАКТИРОВАТЬ: полный след стека:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: training.gutai.apps.gutai, PID: 17795
                  java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getVisibility()' on a null object reference
                      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.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                      at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                      at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                      at android.view.View.measure(View.java:18788)
                      at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1283)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                      at android.widget.ScrollView.onMeasure(ScrollView.java:340)
                      at android.view.View.measure(View.java:18788)
                      at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1632)
                      at android.view.View.measure(View.java:18788)
                      at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1210)
                      at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1550)
                      at android.view.View.measure(View.java:18788)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                      at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:141)
                      at android.view.View.measure(View.java:18788)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                      at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:400)
                      at android.view.View.measure(View.java:18788)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                      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.LinearLayout.measureVertical(LinearLayout.java:748)
                      at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                      at android.view.View.measure(View.java:18788)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                      at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
                      at android.view.View.measure(View.java:18788)
                      at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
                      at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
                      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
                      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
                      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                      at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                      at android.view.Choreographer.doFrame(Choreographer.java:606)
                      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                      at java.lang.reflect.Method.invoke(Native Method)
                    at com.
Disconnected from the target VM, address: 'localhost:8601', transport: 'socket'

РЕДАКТИРОВАТЬ 2:

Я обнаружил проблему, мне пришлось заменить все "«match_parent» атрибутом wrap_content для каждого компонента внутри моего TableLayout и TableRow.Итак, я думаю, что tableRow и TableLayout должны знать высоту / ширину компонента внутри них.(Это было сбой с версиями до Android Marshmallow)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...