我在 Xamarin 中的 Resources.GetIdentifier() 不起作用

My Resources.GetIdentifier() in Xamarin doesn't work

我有这段代码(我正在使用 Visual Studio + Xamarin Android):

KeyMechanism.cs:

using Android.App;
using Android.Content;
using Android.Media;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace myApp
{
    class KeyMechanism : Activity
    {
        private string elementId;
        public KeyMechanism(string elementId)
        {
            this.elementId = elementId;
        }

        public void ClickHander()
        {
            int foo = Resources.GetIdentifier(elementId, "id", PackageName);
            var myKey = FindViewById( foo );

            myKey.Touch += (s, e) =>
            {
                //Do something
            };
        }
    }
}

myViewAct.cs:

using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace myApp
{
    public class myViewAct : Activity 
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.myView1);

            int testFoo = Resources.GetIdentifier("button1", "id", PackageName);

            KeyMechanism myKey1 = new KeyMechanism("button1");
            myKey1.ClickHander();

            /*...*/
        }
    }
}

编译时出现错误:

Java.Lang.NullPointerException: 'Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference'

在文件“KeyMechanism.cs”中符合 int foo = Resources.GetIdentifier(elementId, "id", PackageName);

我想不使用 Resourece.Id.myId 而是使用变量来捕获视图元素。我不知道我的代码有什么问题。

我能够重现这个错误。

Java.Lang.NullPointerException: 'Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference'

该错误表示您的方法用于 NULL 对象。请检查下面的代码。

 public class Activity5 : Activity
{
    Activity mActivity;
    Context context;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Create your application here

        SetContentView(Resource.Layout.layout5);
        int testFoo = Resources.GetIdentifier("button1", "id", PackageName);
        mActivity = this;
        context = Android.App.Application.Context;

        KeyMechanism myKey1 = new KeyMechanism("button1", mActivity, context);
        myKey1.ClickHander();

    }
}
class KeyMechanism : Activity
{
    private string elementId;
    private Activity mActivity;
    private Context context;
    public KeyMechanism(string elementId, Activity mActivity, Context context)
    {
        this.elementId = elementId;
        this.mActivity = mActivity;
        this.context = context;
    }

    public void ClickHander()
    {
        int foo = mActivity.Resources.GetIdentifier(elementId, "id", context.PackageName);

        var myKey = mActivity.FindViewById(foo);

        myKey.Touch += (s, e) =>
        {
            //Do something
            Console.WriteLine("-----------------");
        };
    }
}