簡體   English   中英

Xamarin System.ArgumentException:參數不能為空

[英]Xamarin System.ArgumentException: Parameter cannot be null

我仍在使用 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", "");
                }
}}}

這是我在 zone_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 調用 zone_list 布局。 編輯:在拋出新的 ArgumentException 我得到這個:

Unhandled Exception:
System.ArgumentException: Parameter cannot be null
mBlueZone = FindViewById<LinearLayout>(Resource.Id.blueZoneLayout);

在這種情況下,變量mBlueZone將始終為空。 FindViewById在已設置為Activity ContentView 的布局中查找具有Id blueZoneLayout View 您需要先設置 ContentView,然后再從中提取 Views。 你應該添加這一行

SetContentView(Resource.Layout.zones_list);

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

編輯:以上部分將解決問題(根據您的邏輯)並工作。 但這不是推薦的方法。 你應該有一個單獨的Activity for 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);
     }
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM