为 Mono Android 正确扩展 EditText 类

Posted

技术标签:

【中文标题】为 Mono Android 正确扩展 EditText 类【英文标题】:Properly extending EditText class for Mono Android 【发布时间】:2012-04-22 08:36:31 【问题描述】:

我正在尝试扩展 android Mono 中的 EditText 类以实现覆盖目的。这是我到目前为止代码方面的内容。不幸的是,当我尝试运行它时它会崩溃(当我使用标准的 EditText 小部件时它当然不会崩溃)。

namespace testing

    public class SuperEditText : EditText
    
        public SuperEditText(Context context) : base(context)
        
        // Java has init() here - maybe that's an issue. If so, does C# have an equivalent?
        
    

这是 XML:

<SuperEditText
    android:id="@+id/eta"      
    android:layout_
    android:layout_
    android:gravity="top"
/>

有什么想法吗?

************编辑调试日志:

04-13 17:44:13.743 D/AndroidRuntime(  476): 
04-13 17:44:13.743 D/AndroidRuntime(  476): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
04-13 17:44:13.743 D/AndroidRuntime(  476): CheckJNI is ON
04-13 17:44:14.713 D/AndroidRuntime(  476): Calling main entry com.android.commands.pm.Pm
04-13 17:44:14.773 I/ActivityManager(   79): Start proc com.android.defcontainer for service com.android.defcontainer/.DefaultContainerService: pid=484 uid=10003 gids=1015, 2001
04-13 17:44:15.373 D/dalvikvm(  484): GC_EXPLICIT freed 286K, 54% free 2568K/5511K, external 410K/517K, paused 94ms
04-13 17:44:15.723 W/ActivityManager(   79): No content provider found for: 
04-13 17:44:15.763 W/ActivityManager(   79): No content provider found for: 
04-13 17:44:15.783 D/PackageParser(   79): Scanning package: /data/app/vmdl-636945189.tmp
04-13 17:44:16.093 D/dalvikvm(   79): GC_CONCURRENT freed 502K, 43% free 4211K/7367K, external 710K/1222K, paused 9ms+11ms
04-13 17:44:16.333 I/PackageManager(   79): Removing non-system package:HelloM4A.HelloM4A
04-13 17:44:16.333 I/ActivityManager(   79): Force stopping package HelloM4A.HelloM4A uid=10036
04-13 17:44:16.523 D/PackageManager(   79): Scanning package HelloM4A.HelloM4A
04-13 17:44:16.523 I/PackageManager(   79): Package HelloM4A.HelloM4A codePath changed from /data/app/HelloM4A.HelloM4A-2.apk to /data/app/HelloM4A.HelloM4A-1.apk; Retaining data and using new
04-13 17:44:16.523 I/PackageManager(   79): Unpacking native libraries for /data/app/HelloM4A.HelloM4A-1.apk
04-13 17:44:16.593 D/installd(   35): DexInv: --- BEGIN '/data/app/HelloM4A.HelloM4A-1.apk' ---
04-13 17:44:17.333 D/dalvikvm(  495): DexOpt: load 154ms, verify+opt 353ms
04-13 17:44:17.353 D/installd(   35): DexInv: --- END '/data/app/HelloM4A.HelloM4A-1.apk' (success) ---
04-13 17:44:17.363 W/PackageManager(   79): Code path for pkg : HelloM4A.HelloM4A changing from /data/app/HelloM4A.HelloM4A-2.apk to /data/app/HelloM4A.HelloM4A-1.apk
04-13 17:44:17.363 W/PackageManager(   79): Resource path for pkg : HelloM4A.HelloM4A changing from /data/app/HelloM4A.HelloM4A-2.apk to /data/app/HelloM4A.HelloM4A-1.apk
04-13 17:44:17.363 D/PackageManager(   79): Registered content provider: HelloM4A.HelloM4A.__mono_init__, className = mono.MonoRuntimeProvider, isSyncable = false
04-13 17:44:17.363 D/PackageManager(   79):   Providers: mono.MonoRuntimeProvider
04-13 17:44:17.363 D/PackageManager(   79):   Receivers: mono.android.Seppuku
04-13 17:44:17.363 D/PackageManager(   79):   Activities: hellom4a.Activity1
04-13 17:44:17.373 I/ActivityManager(   79): Force stopping package HelloM4A.HelloM4A uid=10036
04-13 17:44:17.543 I/installd(   35): move /data/dalvik-cache/data@app@HelloM4A.HelloM4A-1.apk@classes.dex -> /data/dalvik-cache/data@app@HelloM4A.HelloM4A-1.apk@classes.dex
04-13 17:44:17.543 D/PackageManager(   79): New package installed in /data/app/HelloM4A.HelloM4A-1.apk
04-13 17:44:17.713 I/ActivityManager(   79): Force stopping package HelloM4A.HelloM4A uid=10036
04-13 17:44:17.833 D/dalvikvm(  164): GC_EXPLICIT freed 59K, 52% free 2906K/5959K, external 1221K/1532K, paused 100ms
04-13 17:44:17.933 I/ActivityManager(   79): Start proc com.svox.pico for broadcast com.svox.pico/.VoiceDataInstallerReceiver: pid=496 uid=10009 gids=
04-13 17:44:17.993 W/RecognitionManagerService(   79): no available voice recognition services found
04-13 17:44:18.783 I/ActivityThread(  496): Pub com.svox.pico.providers.SettingsProvider: com.svox.pico.providers.SettingsProvider
04-13 17:44:18.853 D/dalvikvm(   79): GC_CONCURRENT freed 560K, 43% free 4255K/7431K, external 710K/1222K, paused 9ms+29ms
04-13 17:44:19.103 D/dalvikvm(  295): GC_CONCURRENT freed 296K, 52% free 2738K/5703K, external 555K/1024K, paused 7ms+18ms
04-13 17:44:19.153 D/dalvikvm(   79): GC_EXPLICIT freed 47K, 44% free 4209K/7431K, external 710K/1222K, paused 252ms
04-13 17:44:19.493 I/installd(   35): unlink /data/dalvik-cache/data@app@HelloM4A.HelloM4A-2.apk@classes.dex
04-13 17:44:19.513 D/AndroidRuntime(  476): Shutting down VM
04-13 17:44:19.543 D/dalvikvm(  476): GC_CONCURRENT freed 100K, 72% free 293K/1024K, external 0K/0K, paused 2ms+2ms
04-13 17:44:19.553 I/AndroidRuntime(  476): NOTE: attach of thread 'Binder Thread #3' failed
04-13 17:44:19.553 D/jdwp    (  476): adbd disconnected
04-13 17:44:20.513 D/AndroidRuntime(  509): 
04-13 17:44:20.513 D/AndroidRuntime(  509): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
04-13 17:44:20.513 D/AndroidRuntime(  509): CheckJNI is ON
04-13 17:44:21.483 D/AndroidRuntime(  509): Calling main entry com.android.commands.am.Am
04-13 17:44:21.553 I/ActivityManager(   79): Start proc HelloM4A.HelloM4A for broadcast HelloM4A.HelloM4A/mono.android.Seppuku: pid=517 uid=10036 gids=3003, 1015
04-13 17:44:21.893 I/ActivityThread(  517): Pub HelloM4A.HelloM4A.__mono_init__: mono.MonoRuntimeProvider
04-13 17:44:21.913 D/dalvikvm(  517): Trying to load lib /data/data/HelloM4A.HelloM4A/lib/libmonodroid.so 0x405135d8
04-13 17:44:21.913 D/dalvikvm(  517): Added shared lib /data/data/HelloM4A.HelloM4A/lib/libmonodroid.so 0x405135d8
04-13 17:44:22.253 I/monodroid-gc(  517): environment supports jni NewWeakGlobalRef
04-13 17:44:22.613 D/Zygote  (   33): Process 517 exited cleanly (255)
04-13 17:44:22.613 I/ActivityManager(   79): Process HelloM4A.HelloM4A (pid 517) has died.
04-13 17:44:22.623 D/AndroidRuntime(  509): Shutting down VM
04-13 17:44:22.663 I/AndroidRuntime(  509): NOTE: attach of thread 'Binder Thread #3' failed
04-13 17:44:22.663 D/dalvikvm(  509): GC_CONCURRENT freed 101K, 69% free 319K/1024K, external 0K/0K, paused 1ms+1ms
04-13 17:44:22.663 D/jdwp    (  509): adbd disconnected
04-13 17:44:23.563 D/AndroidRuntime(  531): 
04-13 17:44:23.563 D/AndroidRuntime(  531): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
04-13 17:44:23.563 D/AndroidRuntime(  531): CheckJNI is ON
04-13 17:44:24.733 D/AndroidRuntime(  531): Calling main entry com.android.commands.am.Am
04-13 17:44:24.793 I/ActivityManager(   79): Starting: Intent  act=android.intent.action.MAIN flg=0x10000000 cmp=HelloM4A.HelloM4A/hellom4a.Activity1  from pid 531
04-13 17:44:24.873 D/AndroidRuntime(  531): Shutting down VM
04-13 17:44:24.903 I/AndroidRuntime(  531): NOTE: attach of thread 'Binder Thread #3' failed
04-13 17:44:24.963 D/dalvikvm(  531): GC_CONCURRENT freed 100K, 69% free 318K/1024K, external 0K/0K, paused 2ms+2ms
04-13 17:44:24.963 D/jdwp    (  531): adbd disconnected
04-13 17:44:25.003 I/ActivityManager(   79): Start proc HelloM4A.HelloM4A for activity HelloM4A.HelloM4A/hellom4a.Activity1: pid=540 uid=10036 gids=3003, 1015
04-13 17:44:25.743 I/ActivityThread(  540): Pub HelloM4A.HelloM4A.__mono_init__: mono.MonoRuntimeProvider
04-13 17:44:25.853 D/dalvikvm(  540): Trying to load lib /data/data/HelloM4A.HelloM4A/lib/libmonodroid.so 0x40513a08
04-13 17:44:25.853 D/dalvikvm(  540): Added shared lib /data/data/HelloM4A.HelloM4A/lib/libmonodroid.so 0x40513a08
04-13 17:44:26.233 I/monodroid-gc(  540): environment supports jni NewWeakGlobalRef
04-13 17:44:30.233 I/MonoDroid(  540): UNHANDLED EXCEPTION: Android.Views.InflateException: Exception of type 'Android.Views.InflateException' was thrown.
04-13 17:44:30.283 E/mono    (  540): 
04-13 17:44:30.283 E/mono    (  540): Unhandled Exception: Android.Views.InflateException: Exception of type 'Android.Views.InflateException' was thrown.
04-13 17:44:30.343 D/Zygote  (   33): Process 540 exited cleanly (1)
04-13 17:44:30.343 I/ActivityManager(   79): Process HelloM4A.HelloM4A (pid 540) has died.
04-13 17:44:30.492 W/InputManagerService(   79): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@40676960
04-13 17:44:35.733 D/dalvikvm(  295): GC_EXPLICIT freed 57K, 53% free 2723K/5703K, external 555K/1024K, paused 86ms

【问题讨论】:

【参考方案1】:

您需要在元素名称前加上其所在名称空间的 Java 版本。例如,如果您的名称空间是 testing,则 XML 应如下所示:

<testing.SuperEditText
    android:id="@+id/eta"      
    android:layout_
    android:layout_
    android:gravity="top" />

有关查找/设置 Java 命名空间的更多详细信息,请参见 this answer。

编辑

为了得到异常,我在本地复制了这个,结果是:

System.NotSupportedException:无法在 MonoAndroidApplication23.SuperEditText 类型上找到具有签名(Android.Content.Context、Android.Util.IAttributeSet)的构造函数。请提供缺少的构造函数。

您可以通过将构造函数更新为如下所示来解决此问题:

public SuperEditText(Context context, IAttributeSet attrs)
    : base(context, attrs)


【讨论】:

是的,我想我之前尝试过使用命名空间。无论如何,我再次尝试,但应用程序仍然崩溃。还有其他想法吗? 请提供堆栈跟踪docs.xamarin.com/android/advanced_topics/android_debug_log 感谢您的帮助。我发布了日志,经过一番研究,我想我已经找到了答案。我的项目名称是“HelloM4A”(这不是我的名字,而是来自 Xamarin 的教程)。大写字母是罪魁祸首。 Java 不喜欢它们。我在寻找此错误后发现了这一点:“UNHANDLED EXCEPTION: Android.Views.InflateException: 'Android.Views.InflateException' 类型的异常被抛出” 也许您可以更新您的答案以包含此信息,然后我可以勾选它。顺便说一句,只是要补充一点,是的,还需要您的构造函数。 是的,这就是为什么我的第一个答案是查找命名空间的 Java 版本,因为这是布局 XML 中需要使用的内容。您可以自己设置或在清单中查找。

以上是关于为 Mono Android 正确扩展 EditText 类的主要内容,如果未能解决你的问题,请参考以下文章

无法从 Flux<PortCall> 转换为 Mono<?扩展对象>

gles2 动态壁纸转换为 c# (xamarin) 符合错误“...mono.android.TypeManager 无法应用于

IL2CPP 与 Mono2x 和 Android 部署

如何在 Mono for android 中使用 Runnable

从 Mono 的列表中创建 Flux 的正确方法

Android 上的 .NET 框架