0

FindById()Android 类库上的函数总是返回 null 正常吗?

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

4

2 回答 2

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();您的库。

于 2013-03-19T19:38:44.717 回答
0

我想我找到了错误!

我的 Android 应用程序中的 Resource.Designer.cs 文件生成我的 Android 类库的不同 ID 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 应用程序资源.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
于 2013-03-19T19:28:29.107 回答