错误:java.lang.NoSuchMethodException [英] Error: java.lang.NoSuchMethodException

查看:645
本文介绍了错误:java.lang.NoSuchMethodException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

家伙,您好我需要一点点建议。我试图使用Xamarin让C#做一个Android应用程序。我做了两个布局,并在他们每个人我做拖按钮在它们之间进行导航。第一个按钮作品,但其他的则没有。我把错误java.lang.NoSuchMethodException。

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


    }
}

}

在另一布局我用的是行为的OnClick与标签BUTTONback.I试图移动段落

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".

谢谢!

推荐答案

您会混合不同的方式来装按钮和方法。

You're mixing different ways to hookup buttons and methods.

选项1 - 编程

MainActivity.cs

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

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

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

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

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

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>

这篇关于错误:java.lang.NoSuchMethodException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆