Xamarin System.ArgumentException: 参数不能为空
Xamarin System.ArgumentException: Parameter cannot be null
我仍在使用 Xamarin 进行训练。现在我遇到无法将对象设置为资源的问题,并且出现此错误:未将引用设置为对象的实例。
我所做的,我创造的 Activity class。在那里我创建了 LinearLayout 对象。想法是当我点击蓝色区域对象然后滑动到另一个页面:车辆未停放布局。但它不起作用,我收到错误。
这是我的区域Activity class:
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 布局。
编辑:在 throw new ArgumentException 我得到这个:
Unhandled Exception:
System.ArgumentException: Parameter cannot be null
mBlueZone = FindViewById<LinearLayout>(Resource.Id.blueZoneLayout);
在这种情况下,变量 mBlueZone
将始终为空。 FindViewById
查找 View
在已设置为 Activity
的 ContentView 的布局中具有 Id
blueZoneLayout
的 View
。您需要在从中引入视图之前设置 ContentView。你应该添加这一行
SetContentView(Resource.Layout.zones_list);
之前
mBlueZone = FindViewById<LinearLayout>(Resource.Id.blueZoneLayout);
编辑:以上部分将解决问题(根据您的逻辑)并起作用。但这不是推荐的方法。对于 Vehicle not Parked 布局,您应该有一个单独的 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);
}
}
我仍在使用 Xamarin 进行训练。现在我遇到无法将对象设置为资源的问题,并且出现此错误:未将引用设置为对象的实例。
我所做的,我创造的 Activity class。在那里我创建了 LinearLayout 对象。想法是当我点击蓝色区域对象然后滑动到另一个页面:车辆未停放布局。但它不起作用,我收到错误。 这是我的区域Activity class:
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 布局。 编辑:在 throw new ArgumentException 我得到这个:
Unhandled Exception:
System.ArgumentException: Parameter cannot be null
mBlueZone = FindViewById<LinearLayout>(Resource.Id.blueZoneLayout);
在这种情况下,变量 mBlueZone
将始终为空。 FindViewById
查找 View
在已设置为 Activity
的 ContentView 的布局中具有 Id
blueZoneLayout
的 View
。您需要在从中引入视图之前设置 ContentView。你应该添加这一行
SetContentView(Resource.Layout.zones_list);
之前
mBlueZone = FindViewById<LinearLayout>(Resource.Id.blueZoneLayout);
编辑:以上部分将解决问题(根据您的逻辑)并起作用。但这不是推荐的方法。对于 Vehicle not Parked 布局,您应该有一个单独的 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);
}
}