2

當我嘗試引用片段中的TextView時,出現錯誤System.NullReferenceException: Object reference not set to an instance of an object。它發生在運行時。當試圖在fragmenrt上填充Xamarin Android TextView時,對象引用未設置爲對象的實例

片段

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> 

我的理解是什麼,我想實現可以用回收的觀點來實現,但我還沒有完全理解,因爲我我是一名初學者程序員。

回答

3

你打電話給你的功能太早。 OnCreateOnCreateView之前被調用。這意味着,當您嘗試訪問佈局組件時,您的視圖尚未創建。您必須將fillUpdates的呼叫移動到OnStart(或OnCreateView或在OnCreateView之後調用的另一個生命週期方法)。 看一看Frament Lifecycle

public override void OnStart(Bundle bundle) 
{ 
    base.OnStart(bundle); 
    fillUpdates(updateTitleArray, updateBodyArray); 
} 
+0

感謝您的回覆。我會嘗試的。再次感謝很多 –

+0

你好,我還發現我沒有爲字符串數組賦值,從而試圖設置texview。文本。值爲空。這也會引發上述錯誤 –

相關問題