我还在和Xamarin一起训练。现在我遇到的问题是我无法将对象设置为资源而且我收到此错误:引用未设置为对象的实例。
我做了什么,我创建了Activity类。在那里我创建了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布局。 编辑:抛出新的ArgumentException我得到了这个:
Unhandled Exception:
System.ArgumentException: Parameter cannot be null
答案 0 :(得分:1)
mBlueZone = FindViewById<LinearLayout>(Resource.Id.blueZoneLayout);
在这种情况下,变量mBlueZone
将始终为null。 FindViewById
在布局中查找View
Id
blueZoneLayout
,Activity
已设置为SetContentView(Resource.Layout.zones_list);
的ContentView。您需要在从中渗透视图之前设置ContentView。你应该添加这一行
mBlueZone = FindViewById<LinearLayout>(Resource.Id.blueZoneLayout);
之前
Activity
编辑:以上部分将解决问题(根据您的逻辑)和工作。但这不是推荐的做法。您应该为Vehicle not Parked Layout单独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);
}
}
。
form = SQLFORM.smartgrid(..,selectable= lambda ids: your_function_name_to_trigger(ids),...),