2

System.NullReferenceException: Object reference not set to an instance of an object当我尝试TextView从我的片段中引用 a 时出现错误。它发生在运行时。

分段

public class Fragment1 : Fragment
{
    private int checkNumber = 0;
    private string[] updateTitleArray = new string[10];
    private string[] updateBodyArray = new string[10];
    private TextView updatesTitle0;
    private TextView updatesTitle1;
    private TextView updatesTitle2;
    private TextView updatesTitle3;
    private TextView updatesTitle4;
    private TextView updatesTitle5;
    private TextView updatesTitle6;
    private TextView updatesTitle7;
    private TextView updatesTitle8;
    private TextView updatesTitle9;

    private TextView updateBody0;
    private TextView updateBody1;
    private TextView updateBody2;
    private TextView updateBody3;
    private TextView updateBody4;
    private TextView updateBody5;
    private TextView updateBody6;
    private TextView updateBody7;
    private TextView updateBody8;
    private TextView updateBody9;



    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        fillUpdates(updateTitleArray, updateBodyArray);
    }

    public static Fragment1 NewInstance()
    {
        var frag1 = new Fragment1 { Arguments = new Bundle() };
        return frag1;
    }


    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        var ignored = base.OnCreateView(inflater, container, savedInstanceState);
        return inflater.Inflate(Resource.Layout.fragment1, null);
    }

    public void fillUpdates(string[] updatetitlearray, string[] updatebodyarray)
    {
        //increment1 so if block skips here
        checkNumber = 1;

        //fill updatesArray
        updateTitleArray = updatetitlearray;
        updateBodyArray = updatebodyarray;

        updatesTitle0 = View.FindViewById<TextView>(Resource.Id.update_title0);
        updatesTitle1 = View.FindViewById<TextView>(Resource.Id.update_title1);
        updatesTitle2 = View.FindViewById<TextView>(Resource.Id.update_title2);
        updatesTitle3 = View.FindViewById<TextView>(Resource.Id.update_title3);
        updatesTitle4 = View.FindViewById<TextView>(Resource.Id.update_title4);
        updatesTitle5 = View.FindViewById<TextView>(Resource.Id.update_title5);
        updatesTitle6 = View.FindViewById<TextView>(Resource.Id.update_title6);
        updatesTitle7 = View.FindViewById<TextView>(Resource.Id.update_title7);
        updatesTitle8 = View.FindViewById<TextView>(Resource.Id.update_title8);
        updatesTitle9 = View.FindViewById<TextView>(Resource.Id.update_title9);

        updateBody0 = View.FindViewById<TextView>(Resource.Id.update_body0);
        updateBody1 = View.FindViewById<TextView>(Resource.Id.update_body1);
        updateBody2 = View.FindViewById<TextView>(Resource.Id.update_body2);
        updateBody3 = View.FindViewById<TextView>(Resource.Id.update_body3);
        updateBody4 = View.FindViewById<TextView>(Resource.Id.update_body4);
        updateBody5 = View.FindViewById<TextView>(Resource.Id.update_body5);
        updateBody6 = View.FindViewById<TextView>(Resource.Id.update_body6);
        updateBody7 = View.FindViewById<TextView>(Resource.Id.update_body7);
        updateBody8 = View.FindViewById<TextView>(Resource.Id.update_body8);
        updateBody9 = View.FindViewById<TextView>(Resource.Id.update_body9);


        updatesTitle0.Text = updateTitleArray[0].ToString();
        updatesTitle1.Text = updateTitleArray[1].ToString();
        updatesTitle2.Text = updateTitleArray[2].ToString();
        updatesTitle3.Text = updateTitleArray[3].ToString();
        updatesTitle4.Text = updateTitleArray[4].ToString();
        updatesTitle5.Text = updateTitleArray[5].ToString();
        updatesTitle6.Text = updateTitleArray[6].ToString();
        updatesTitle7.Text = updateTitleArray[7].ToString();
        updatesTitle8.Text = updateTitleArray[8].ToString();
        updatesTitle9.Text = updateTitleArray[9].ToString();

        updateBody0.Text = updateBodyArray[0].ToString();
        updateBody1.Text = updateBodyArray[1].ToString();
        updateBody2.Text = updateBodyArray[2].ToString();
        updateBody3.Text = updateBodyArray[3].ToString();
        updateBody4.Text = updateBodyArray[4].ToString();
        updateBody5.Text = updateBodyArray[5].ToString();
        updateBody6.Text = updateBodyArray[6].ToString();
        updateBody7.Text = updateBodyArray[7].ToString();
        updateBody8.Text = updateBodyArray[8].ToString();
        updateBody9.Text = updateBodyArray[9].ToString();
    }
}

看法

   <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dp"
        android:fillViewport="false">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
        <!--Cardview 1-->
            <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minHeight="60dp"
                card_view:cardElevation="8dp"
                card_view:cardCornerRadius="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp">
                <GridLayout
                    android:minWidth="25px"
                    android:minHeight="25px"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:columnCount="3"
                    android:rowCount="1">
                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_gravity="fill"
                        android:layout_row="0"
                        android:layout_column="0"
                        android:padding="16sp">
                        <TextView
                            android:id="@+id/update_title0"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Update 1"
                            android:padding="4sp"
                            android:textSize="24sp"
                            android:textColor="@color/primaryText" />
                        <TextView
                            android:id="@+id/update_body0"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Update Details Is Shownbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb Here"
                            android:padding="4sp"
                            android:textSize="16sp" />
                    </LinearLayout>
                </GridLayout>
            </android.support.v7.widget.CardView>
        <!--card view2-->
            <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                card_view:cardElevation="8dp"
                card_view:cardCornerRadius="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp">
                <GridLayout
                    android:minWidth="25px"
                    android:minHeight="25px"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:columnCount="3"
                    android:rowCount="1">
                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_gravity="fill"
                        android:layout_row="0"
                        android:layout_column="0"
                        android:padding="16sp">
                        <TextView
                            android:id="@+id/update_title1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Update 1"
                            android:padding="4sp"
                            android:textSize="24sp"
                            android:textColor="@color/primaryText" />
                        <TextView
                            android:id="@+id/update_body1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Update Details Is Shown Here"
                            android:padding="4sp"
                            android:textSize="16sp" />
                    </LinearLayout>
                </GridLayout>
            </android.support.v7.widget.CardView>
        <!--cardview 3-->
            <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                card_view:cardElevation="8dp"
                card_view:cardCornerRadius="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp">
                <GridLayout
                    android:minWidth="25px"
                    android:minHeight="25px"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:columnCount="3"
                    android:rowCount="1">
                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_gravity="fill"
                        android:layout_row="0"
                        android:layout_column="0"
                        android:padding="16sp">
                        <TextView
                            android:id="@+id/update_title2"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Update 1"
                            android:padding="4sp"
                            android:textSize="24sp"
                            android:textColor="@color/primaryText" />
                        <TextView
                            android:id="@+id/update_body2"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Update Details Is Shown Here"
                            android:padding="4sp"
                            android:textSize="16sp" />
                    </LinearLayout>
                </GridLayout>
            </android.support.v7.widget.CardView>
        <!--card view 4-->
            <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                card_view:cardElevation="8dp"
                card_view:cardCornerRadius="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp">
                <GridLayout
                    android:minWidth="25px"
                    android:minHeight="25px"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:columnCount="3"
                    android:rowCount="1">
                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_gravity="fill"
                        android:layout_row="0"
                        android:layout_column="0"
                        android:padding="16sp">
                        <TextView
                            android:id="@+id/update_title3"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Update 1"
                            android:padding="4sp"
                            android:textSize="24sp"
                            android:textColor="@color/primaryText" />
                        <TextView
                            android:id="@+id/update_body3"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Update Details Is Shown Here"
                            android:padding="4sp"
                            android:textSize="16sp" />
                    </LinearLayout>
                </GridLayout>
            </android.support.v7.widget.CardView>
        <!--card view 5-->
            <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                card_view:cardElevation="8dp"
                card_view:cardCornerRadius="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp">
                <GridLayout
                    android:minWidth="25px"
                    android:minHeight="25px"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:columnCount="3"
                    android:rowCount="1">
                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_gravity="fill"
                        android:layout_row="0"
                        android:layout_column="0"
                        android:padding="16sp">
                        <TextView
                            android:id="@+id/update_title4"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Update 1"
                            android:padding="4sp"
                            android:textSize="24sp"
                            android:textColor="@color/primaryText" />
                        <TextView
                            android:id="@+id/update_body4"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Update Details Is Shown Here"
                            android:padding="4sp"
                            android:textSize="16sp" />
                    </LinearLayout>
                </GridLayout>
            </android.support.v7.widget.CardView>
        <!--card view 6-->
            <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                card_view:cardElevation="8dp"
                card_view:cardCornerRadius="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp">
                <GridLayout
                    android:minWidth="25px"
                    android:minHeight="25px"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:columnCount="3"
                    android:rowCount="1">
                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_gravity="fill"
                        android:layout_row="0"
                        android:layout_column="0"
                        android:padding="16sp">
                        <TextView
                            android:id="@+id/update_title5"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Update 1"
                            android:padding="4sp"
                            android:textSize="24sp"
                            android:textColor="@color/primaryText" />
                        <TextView
                            android:id="@+id/update_body5"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Update Details Is Shown Here"
                            android:padding="4sp"
                            android:textSize="16sp" />
                    </LinearLayout>
                </GridLayout>
            </android.support.v7.widget.CardView>
        <!--card view 7-->
            <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                card_view:cardElevation="8dp"
                card_view:cardCornerRadius="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp">
                <GridLayout
                    android:minWidth="25px"
                    android:minHeight="25px"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:columnCount="3"
                    android:rowCount="1">
                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_gravity="fill"
                        android:layout_row="0"
                        android:layout_column="0"
                        android:padding="16sp">
                        <TextView
                            android:id="@+id/update_title6"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Update 1"
                            android:padding="4sp"
                            android:textSize="24sp"
                            android:textColor="@color/primaryText" />
                        <TextView
                            android:id="@+id/update_body6"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Update Details Is Shown Here"
                            android:padding="4sp"
                            android:textSize="16sp" />
                    </LinearLayout>
                </GridLayout>
            </android.support.v7.widget.CardView>
        <!--card view 8-->
            <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                card_view:cardElevation="8dp"
                card_view:cardCornerRadius="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp">
                <GridLayout
                    android:minWidth="25px"
                    android:minHeight="25px"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:columnCount="3"
                    android:rowCount="1">
                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_gravity="fill"
                        android:layout_row="0"
                        android:layout_column="0"
                        android:padding="16sp">
                        <TextView
                            android:id="@+id/update_title7"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Update 1"
                            android:padding="4sp"
                            android:textSize="24sp"
                            android:textColor="@color/primaryText" />
                        <TextView
                            android:id="@+id/update_body7"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Update Details Is Shown Here"
                            android:padding="4sp"
                            android:textSize="16sp" />
                    </LinearLayout>
                </GridLayout>
            </android.support.v7.widget.CardView>
        <!--card view 9-->
            <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                card_view:cardElevation="8dp"
                card_view:cardCornerRadius="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp">
                <GridLayout
                    android:minWidth="25px"
                    android:minHeight="25px"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:columnCount="3"
                    android:rowCount="1">
                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_gravity="fill"
                        android:layout_row="0"
                        android:layout_column="0"
                        android:padding="16sp">
                        <TextView
                            android:id="@+id/update_title8"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Update 1"
                            android:padding="4sp"
                            android:textSize="24sp"
                            android:textColor="@color/primaryText" />
                        <TextView
                            android:id="@+id/update_body8"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Update Details Is Shown Here"
                            android:padding="4sp"
                            android:textSize="16sp" />
                    </LinearLayout>
                </GridLayout>
            </android.support.v7.widget.CardView>
        <!--card view 10-->
            <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                card_view:cardElevation="8dp"
                card_view:cardCornerRadius="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp">
                <GridLayout
                    android:minWidth="25px"
                    android:minHeight="25px"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:columnCount="3"
                    android:rowCount="1">
                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_gravity="fill"
                        android:layout_row="0"
                        android:layout_column="0"
                        android:padding="16sp">
                        <TextView
                            android:id="@+id/update_title9"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Update 1"
                            android:padding="4sp"
                            android:textSize="24sp"
                            android:textColor="@color/primaryText" />
                        <TextView
                            android:id="@+id/update_body9"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Update Details Is Shown Here"
                            android:padding="4sp"
                            android:textSize="16sp" />
                    </LinearLayout>
                </GridLayout>
            </android.support.v7.widget.CardView>
        </LinearLayout>
    </ScrollView>

我知道我想要实现的目标可以用回收器视图来实现,但我还没有完全理解,因为我是一个初学者程序员。

4

1 回答 1

3

你打电话给你的功能太早了。OnCreate之前被调用OnCreateView。这意味着,当您尝试访问布局组件时,您的视图尚未创建。您必须将调用移动fillUpdatesOnStart(或OnCreateView在 OnCreateView 之后调用的另一个生命周期方法)。查看框架生命周期

public override void OnStart(Bundle bundle)
{
    base.OnStart(bundle);
    fillUpdates(updateTitleArray, updateBodyArray);
}
于 2016-04-09T12:42:23.917 回答