0

Hi guys I need a little advice. I trying to made an android app using Xamarin so C#. I made two layouts and in each one of them I made tow buttons to navigate between them. The first button works but the other don't. I bring the error java.lang.NoSuchMethodException.

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); };


    }
}

}

At the other layout I use the behavior OnClick with the tag BUTTONback.I tried to move the paragraph

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

down and I bring the error "has been thrown. Object reference not set to an instance of an object".

Thanks!

User 12345678
  • 7,714
  • 2
  • 28
  • 46
user3836246
  • 357
  • 2
  • 5
  • 13
  • Could you also provide the layouts please? In the question you're stating you're getting a Java.Lang.NoSuchMethodException. But later on you're stating it's Object reference not set to an instance of an object, which is a NullRererenceException. So which is it? Both? – Frank Aug 08 '14 at 12:37
  • I can't answer to you like question because I've got a too low reputation so I write here : http://txs.io/ZHpb – user3836246 Aug 08 '14 at 12:53

1 Answers1

0

You're mixing different ways to hookup 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>
Frank
  • 733
  • 5
  • 7
  • At first option I bring the error "has been thrown. Object reference not set to an instance of an object" and at second the app crash. Thank you anyway! – user3836246 Aug 08 '14 at 17:33
  • Here it compiles and works normally. Did you try it in a new, separate project? – Frank Aug 09 '14 at 06:22
  • I see you already started a new question. http://stackoverflow.com/questions/25209916/xamarin-findviewbyid-returns-null That's not a nice thing to do. – Frank Aug 09 '14 at 06:43
  • Sorry but none of your solutions works.And practically my question of this topic was solved. – user3836246 Aug 09 '14 at 08:14
  • It's clear from from your other question that instead of choosing between solutions you again choose both options. I clearly wrote that you use "OnClick in the layout" or setting up delegate using ".Click += ...". You made typo's (in http://en.textsave.org/2Ipb you suddenly have OnCLick instead of OnClick)... Typo's in ID's can give such errors. You claim both answers don't work. I doubt tested both, the error comes from a line in only one of them. Finally, yes, I see that the programmatic example contains an error. Which I fixed. That doesn't mean the answer didn't solve your problem. – Frank Aug 09 '14 at 11:24