c# - Xamarin 系统.ArgumentException : Parameter cannot be null

标签 c# android xamarin xamarin.android

我仍在使用 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 布局。 编辑:在 throw new ArgumentException 我得到这个:

Unhandled Exception:
System.ArgumentException: Parameter cannot be null

最佳答案

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

在这种情况下,变量 mBlueZone 将始终为 null。 FindViewById 在已设置为 的 ContentView 的布局中查找具有 Id blueZoneLayoutView > Activity 。您需要在从中引入 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);
     }
 }

关于c# - Xamarin 系统.ArgumentException : Parameter cannot be null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36997996/

相关文章:

android - 带有 Xamarin 的 Visual Studio 2015 - VS 上未显示设备

c# - 处理 asp.net C#/SQL Server 应用程序的并发问题

ios - MvvmCross 6.0 - TipCalc iOS 示例 - 无法正常工作

javascript - 在 ASP.NET 中单击提交按钮时使表单不刷新

java - ListView 和 ArrayAdapter 配置,android 应用程序

android - Android Viewpager嵌入Youtube链接

java - 在 Android Studio 中添加新资源文件夹的正确方法?

c# - Xamarin.iOS Firebase 未接收推送通知但接收 token

c# - 正则表达式 - 正确的模式

c# - Roslyn 在 ObjectCreationExpressionSyntax 中获取 IdentifierName