2016-05-03 13 views
1

私はまだXamarinでトレーニングを行っています。今私はリソースにオブジェクトを設定することはできませんし、このエラーが発生している問題があります:オブジェクトのインスタンスに設定されていない参照。Xamarin System.ArgumentException:パラメータをnullにすることはできません。

アクティビティクラスを作成しました。ここでLinearLayoutオブジェクトを作成します。アイデアは青いゾーンのオブジェクトをクリックした後、別のページにスワイプします:車両は駐車していないレイアウトです。しかし、それは動作しません、私はエラーを取得しています。 これは私のZonesActivityクラスです:

namespace CustomActionBarParking 
    { 
     [Activity(Label = "@+id/blueZoneLayout")] 

     public class ZonesActivity : Activity 
     LinearLayout mBlueZone; 
     protected override void OnCreate(Bundle bundle) 
      { 
       base.OnCreate(bundle); 
       mBlueZone = FindViewById<LinearLayout>(Resource.Id.blueZoneLayout); 
       if (mBlueZone != null) 
       { 
        mBlueZone.Click += (object sender, EventArgs args) => 
        { 
         SetContentView(Resource.Layout.vehicle_not_parked); 
        }; 
       } 
       else 
       { 
        throw new ArgumentException("Parameter cannot be null", ""); 
       } 
}}} 

そして、これはzones_list.axmlファイルの私の一部です:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:weightSum="100"> 
<LinearLayout 
     android:orientation="horizontal" 
     android:layout_weight="11" 
     android:minWidth="25px" 
     android:minHeight="25px" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/blueZoneLayout" 
     android:weightSum="100" 
     android:focusableInTouchMode="true" 
     android:background="@android:color/background_light"> 
     <TextView 
      android:text="M" 
      android:layout_weight="10" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:id="@+id/textView2" 
      android:textColor="@android:color/holo_blue_dark" 
      android:gravity="center" /> 
     <TextView 
      android:text="Blue zone" 
      android:layout_weight="80" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:id="@+id/textView3" 
      android:textColor="@android:color/holo_blue_dark" 
      android:gravity="center" /> 
     <TextView 
      android:text="i" 
      android:layout_weight="10" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:id="@+id/textView4" 
      android:textColor="@android:color/holo_blue_dark" 
      android:gravity="center" /> 
</LinearLayout> 

私はこのオブジェクトのへの参照を設定することはできませんなぜ任意のアイデア?また、MainActivity.csからzones_listレイアウトを呼び出すことにも言及したいと思います。 編集:

Unhandled Exception: 
System.ArgumentException: Parameter cannot be null 

答えて

1
mBlueZone = FindViewById<LinearLayout>(Resource.Id.blueZoneLayout); 

変数mBlueZoneは、常にこのような場合にはnullになります:スロー新しいArgumentExceptionが、私はこれを取得しています。 FindViewByIdIdblueZoneLayoutActivityのContentViewとして設定されたレイアウト内にあるViewを探します。ビューを作成する前にContentViewを設定する必要があります。あなたは

mBlueZone = FindViewById<LinearLayout>(Resource.Id.blueZoneLayout); 

EDIT前にこのライン

SetContentView(Resource.Layout.zones_list); 

を追加する必要があります。上記の部分は、(あなたのロジックに従って)問題を解決していきます。しかし、これはそれを行うための推奨されたアプローチではありません。ビークルパークされていないレイアウトのために別のActivityが必要です。

public class LaunchActivity:Activity 
{ 
    protected override void OnCreate (Bundle bundle) 
    { 
     base.OnCreate (bundle); 
     SetContentView(Resource.Layout.zones_list); 
     mBlueZone = FindViewById<LinearLayout>(Resource.Id.blueZoneLayout); 
      mBlueZone.Click += (object sender, EventArgs args) => 
       { 
        StartActivity(typeof(NotParkedActivity)); 
       }; 
    } 

} 

public class NotParkedActivity :Activity 
{ 
    protected override void OnCreate (Bundle bundle) 
    { 
     base.OnCreate (bundle); 
     SetContentView(Resource.Layout. vehicle_not_parked); 
    } 
} 
+0

私は同じことをするための良い方法を追加しました。私の編集をチェックする – Sreeraj

関連する問題