1

I am still training with Xamarin. Now I got issue that I cannot set object to resource and I am getting this error: reference not set to an instance of object.

What I have done, that I created Activity class. There I create LinearLayout object. Idea is when I click on blue zone object then swipes to another page: vehicle not parked layout. But it doesn't work, I am getting errors. This is my ZonesActivity 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", "");
                }
}}}

And this is my part of zones_list.axml file:

<?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>

Any ideas why I can't set references to this object's? And also I want to mention that I am calling zones_list layout from MainActivity.cs . Edit: In throw new ArgumentException I am getting this:

Unhandled Exception:
System.ArgumentException: Parameter cannot be null

1 Answer 1

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

The variable mBlueZone will always be null in this case. FindViewById looks for View that has an Id blueZoneLayout within the layout that has been set as ContentView of the Activity. You need to set the ContentView before infalting Views from it. You should be adding this line

SetContentView(Resource.Layout.zones_list);

before

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

EDIT: The above portion will fix the problem (as per your logic) and work. But this is not the recommended approach for doing it. You should be having a separate 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);
     }
 }
Sign up to request clarification or add additional context in comments.

1 Comment

I have added a better way to do the same. Check my edit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.