正确扩展Mono Android的EditText类

时间:2012-04-10 02:06:21

标签: android inheritance android-widget android-edittext xamarin.android

我正在尝试扩展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_height="wrap_content"
    android:layout_width="fill_parent"
    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 个答案:

答案 0 :(得分:3)

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

<testing.SuperEditText
    android:id="@+id/eta"      
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    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)
{
}