错误:java.lang.NoSuchMethodException

时间:2014-08-08 12:28:16

标签: c# android xamarin

大家好我需要一些建议。我试图使用Xamarin创建一个Android应用程序,所以C#。我做了两个布局,在每一个布局中,我制作了两个按钮来在它们之间导航。第一个按钮有效,但另一个没有。我带错了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); };


    }
}

}

在另一个布局中,我使用带有标签BUTTONback的行为OnClick。我试图移动段落

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

向下,我带来错误&#34;已被抛出。对象引用未设置为对象的实例&#34;。

谢谢!

1 个答案:

答案 0 :(得分:0)

您正在混合不同方式来连接按钮和方法。

选项1 - 以编程方式

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>

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