FindById始终返回null

时间:2013-03-19 15:07:56

标签: c# xamarin.android

Android类库中的函数FindById()始终返回null是否正常?

我想创建一个包含一些活动的库,以便您可以在各种项目中使用。为此,我在FindById()方法中调用OnCreate()。但是,它始终返回null。

2 个答案:

答案 0 :(得分:1)

我的MonoDroid.ActionBar项目遇到了类似的问题。修复由Atsushi Enomoto提出,目前它是Xamarin.Android中的一个错误。

将此添加到您的项目中:

using System;
using System.Linq;
using System.Reflection;
using Android.Runtime;

namespace MonoDroid.ActionBar.Library
{
    //From http://forums.xamarin.com/discussion/comment/5816/#Comment_5816
    public static class ResourceIdManager
    {
        static bool _idInitialized;
        public static void UpdateIdValues ()
        {
            if (_idInitialized)
                return;
            var eass = Assembly.GetExecutingAssembly ();
            Func<Assembly,Type> f = ass =>
                ass.GetCustomAttributes (typeof (ResourceDesignerAttribute), true)
                    .Select (ca => ca as ResourceDesignerAttribute)
                    .Where (ca => ca != null && ca.IsApplication)
                    .Select (ca => ass.GetType (ca.FullName))
                    .Where (ty => ty != null)
                    .FirstOrDefault ();
            var t = f (eass);
            if (t == null)
                t = AppDomain.CurrentDomain.GetAssemblies ().Select (ass => f (ass)).Where (ty => ty != null).FirstOrDefault ();
            if (t != null)
                t.GetMethod ("UpdateIdValues").Invoke (null, new object [0]);
            _idInitialized = true;
        }
    }
}

在使用您的观点之前,请在您的图书馆中致电ResourceIdManager.UpdateIdValues();

答案 1 :(得分:0)

我想我发现了这个错误!

我的Android应用程序中的Resource.Designer.cs文件会生成我的Android类库的不同ID's Resource.designer.cs文件。

如何解决?

Android类库Resource.designer.cs

#pragma warning disable 1591
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.18033
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

[assembly: Android.Runtime.ResourceDesignerAttribute("Mono.Android.Plugins.Resource", IsApplication=false)]

namespace Mono.Android.Plugins
{


[System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Android.Build.Tasks", "1.0.0.0")]
public partial class Resource
{

    Resource()
    {
        global::Android.Runtime.ResourceIdManager.UpdateIdValues ();
    }

    public partial class Attribute
    {

        private Attribute()
        {
        }
    }

    public partial class Drawable
    {

        // aapt resource value: 0x7f020000
        public static int ic_stat_dismiss = 2130837504;

        // aapt resource value: 0x7f020001
        public static int ic_stat_ic_stat_snooze = 2130837505;

        // aapt resource value: 0x7f020002
        public static int ic_stat_notification = 2130837506;

        // aapt resource value: 0x7f020003
        public static int ic_stat_snooze = 2130837507;

        // aapt resource value: 0x7f020004
        public static int monoandroidsplash = 2130837508;

        private Drawable()
        {
        }
    }

    public partial class Id
    {

        // aapt resource value: 0x7f060002
        public static int dismiss_button = 2131099650;

        // aapt resource value: 0x7f060001
        public static int reminder_content = 2131099649;

        // aapt resource value: 0x7f060000
        public static int reminder_title = 2131099648;

        // aapt resource value: 0x7f060003
        public static int snooze_button = 2131099651;

        private Id()
        {
        }
    }

    public partial class Layout
    {

        // aapt resource value: 0x7f030000
        public static int monoandroidsplash = 2130903040;

        // aapt resource value: 0x7f030001
        public static int post_reminder = 2130903041;

        private Layout()
        {
        }
    }

    public partial class String
    {

        // aapt resource value: 0x7f040002
        public static int Hello = 2130968578;

        // aapt resource value: 0x7f040001
        public static int dismiss = 2130968577;

        // aapt resource value: 0x7f040000
        public static int snooze = 2130968576;

        private String()
        {
        }
    }

    public partial class Style
    {

        // aapt resource value: 0x7f050000
        public static int Mono_Android_Theme_Splash = 2131034112;

        private Style()
        {
        }
    }
}
}
#pragma warning restore 1591

Android应用程序Resource.designer.cs

#pragma warning disable 1591
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.18033
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

[assembly: Android.Runtime.ResourceDesignerAttribute("AndroidApplication1.Resource", IsApplication=true)]

namespace AndroidApplication1
{


    [System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Android.Build.Tasks", "1.0.0.0")]
    public partial class Resource
    {

        Resource()
        {
            global::Android.Runtime.ResourceIdManager.UpdateIdValues ();
        }

        public static void UpdateIdValues()
        {
            Mono.Android.Plugins.Resource.Drawable.ic_stat_dismiss = AndroidApplication1.Resource.Drawable.ic_stat_dismiss;
            Mono.Android.Plugins.Resource.Drawable.ic_stat_ic_stat_snooze = AndroidApplication1.Resource.Drawable.ic_stat_ic_stat_snooze;
            Mono.Android.Plugins.Resource.Drawable.ic_stat_notification = AndroidApplication1.Resource.Drawable.ic_stat_notification;
            Mono.Android.Plugins.Resource.Drawable.ic_stat_snooze = AndroidApplication1.Resource.Drawable.ic_stat_snooze;
            Mono.Android.Plugins.Resource.Drawable.monoandroidsplash = AndroidApplication1.Resource.Drawable.monoandroidsplash;
            Mono.Android.Plugins.Resource.Id.dismiss_button = AndroidApplication1.Resource.Id.dismiss_button;
            Mono.Android.Plugins.Resource.Id.reminder_content = AndroidApplication1.Resource.Id.reminder_content;
            Mono.Android.Plugins.Resource.Id.reminder_title = AndroidApplication1.Resource.Id.reminder_title;
            Mono.Android.Plugins.Resource.Id.snooze_button = AndroidApplication1.Resource.Id.snooze_button;
            Mono.Android.Plugins.Resource.Layout.monoandroidsplash = AndroidApplication1.Resource.Layout.monoandroidsplash;
            Mono.Android.Plugins.Resource.Layout.post_reminder = AndroidApplication1.Resource.Layout.post_reminder;
            Mono.Android.Plugins.Resource.String.Hello = AndroidApplication1.Resource.String.Hello;
            Mono.Android.Plugins.Resource.String.dismiss = AndroidApplication1.Resource.String.dismiss;
            Mono.Android.Plugins.Resource.String.snooze = AndroidApplication1.Resource.String.snooze;
            Mono.Android.Plugins.Resource.Style.Mono_Android_Theme_Splash = AndroidApplication1.Resource.Style.Mono_Android_Theme_Splash;
        }

        public partial class Attribute
        {

            private Attribute()
            {
            }
        }

        public partial class Drawable
        {

            // aapt resource value: 0x7f020000
            public const int ic_stat_dismiss = 2130837504;

            // aapt resource value: 0x7f020001
            public const int ic_stat_ic_stat_snooze = 2130837505;

            // aapt resource value: 0x7f020002
            public const int ic_stat_notification = 2130837506;

            // aapt resource value: 0x7f020003
            public const int ic_stat_snooze = 2130837507;

            // aapt resource value: 0x7f020004
            public const int Icon = 2130837508;

            // aapt resource value: 0x7f020005
            public const int monoandroidsplash = 2130837509;

            private Drawable()
            {
            }
        }

        public partial class Id
        {

            // aapt resource value: 0x7f060000
            public const int button = 2131099648;

            // aapt resource value: 0x7f060003
            public const int dismiss_button = 2131099651;

            // aapt resource value: 0x7f060002
            public const int reminder_content = 2131099650;

            // aapt resource value: 0x7f060001
            public const int reminder_title = 2131099649;

            // aapt resource value: 0x7f060004
            public const int snooze_button = 2131099652;

            private Id()
            {
            }
        }

        public partial class Layout
        {

            // aapt resource value: 0x7f030000
            public const int Main = 2130903040;

            // aapt resource value: 0x7f030001
            public const int monoandroidsplash = 2130903041;

            // aapt resource value: 0x7f030002
            public const int post_reminder = 2130903042;

            private Layout()
            {
            }
        }

        public partial class String
        {

            // aapt resource value: 0x7f040003
            public const int ApplicationName = 2130968579;

            // aapt resource value: 0x7f040002
            public const int Hello = 2130968578;

            // aapt resource value: 0x7f040001
            public const int dismiss = 2130968577;

            // aapt resource value: 0x7f040000
            public const int snooze = 2130968576;

            private String()
            {
            }
        }

        public partial class Style
        {

            // aapt resource value: 0x7f050000
            public const int Mono_Android_Theme_Splash = 2131034112;

            private Style()
            {
            }
        }
    }
}
#pragma warning restore 1591