Error: java.lang.NoSuchMethodException
本文关键字:NoSuchMethodException lang java Error | 更新日期: 2023-09-27 18:17:02
大家好,我需要一点建议。我试图使一个android应用程序使用Xamarin所以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) ;};
,我带来的错误"已被抛出。对象引用未设置为对象的实例"。
谢谢!
你混合了不同的方式来连接按钮和方法。
选项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>