Error: java.lang.NoSuchMethodException

Hi guys, I need a little advice. I am trying to make an Android app using Xamarin so C #. I made two layouts and in each one I made the tow buttons to navigate between them. The first button works and the other doesn't. I am bringing in java.lang.NoSuchMethodException error.

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace SOLVER
{
    [Activity (Label = "SOLVER", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
{
    private void BUTONULback (View v)
    {
        Button ButonulBack = FindViewById<Button> (Resource.Id.Butonulback);
        ButonulBack.Click += delegate { SetContentView (Resource.Layout.Main); };
    }

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);
        SetContentView (Resource.Layout.Main);

        Button button = FindViewById<Button> (Resource.Id.butonulSolve);
        button.Click += delegate { SetContentView (Resource.Layout.Main2); };


    }
}

      

}

On another layout I am using the OnClick behavior with the BUTTONback tag. I tried to move a paragraph

Button ButonulBack = FindViewById<Button> (Resource.Id.Butonulback);
ButonulBack.Click += delegate { SetContentView (Resource.Layout.Main) ;};

      

down and I get the error "was thrown. Object reference not set to object instance".

Thank!

0


source to share


1 answer


You are mixing different ways to connect buttons and methods.

Option 1 - programmatically

MainActivity.cs

using System;

using Android.App;
using Android.OS;
using Android.Views;
using Android.Widget;


namespace Example
{
    [Activity(Label = "Example", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            this.SetContentView(Resource.Layout.Main);

            this.FindViewById<Button>(Resource.Id.ForwardButton).Click += this.Forward;
        }

        public void Forward(object sender, EventArgs e)
        {
            this.SetContentView(Resource.Layout.Main2);
            this.FindViewById<Button>(Resource.Id.BackButton).Click += this.Back;
        }

        public void Back(object sender, EventArgs e)
        {
            this.SetContentView(Resource.Layout.Main);
            this.FindViewById<Button>(Resource.Id.ForwardButton).Click += this.Forward;
        }
    }
}

      

Main.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:id="@+id/ForwardButton"
        android:text="Forward"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

      

Main2.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/CheckedTextView"
        android:layout_width="match_parent"
        android:layout_height="326dp"
        android:enabled="false" />
    <Button
        android:id="@+id/BackButton"
        android:text="Back"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

      



In XML

MainActivity.cs

using System;

using Android.App;
using Android.OS;
using Android.Views;
using Android.Widget;


namespace Example
{
    [Activity(Label = "Example", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            this.SetContentView(Resource.Layout.Main);
        }

        [Java.Interop.Export("OnClick")]
        public void OnClick (View view)
        {
            var button = (Button)view;

            if (button.Id == Resource.Id.ForwardButton) {
                this.SetContentView(Resource.Layout.Main2);
            } else if (button.Id == Resource.Id.BackButton) {
                this.SetContentView(Resource.Layout.Main);
            }
        }
    }
}

      

Main.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:id="@+id/ForwardButton"
        android:text="Forward"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="OnClick" />
</LinearLayout>

      

Main2.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/CheckedTextView"
        android:layout_width="match_parent"
        android:layout_height="326dp"
        android:enabled="false" />
    <Button
        android:id="@+id/BackButton"
        android:text="Back"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="OnClick" />
</LinearLayout>

      

0


source







All Articles