如何在 xamarin android 中扩展应用程序类 [英] How to extend application class in xamarin android
问题描述
我想扩展 Android 应用程序并且我想覆盖 oncreate 方法.但由于某种原因它非常糟糕......只有当我删除 [Application] 我才能运行它但根据这个 link 我们必须添加 [Application]
I want to extend the Android application and I want to override oncreate method. But for some reason its been very terrible...Only if I remove [Application] I am able to run it but according to this link we have to add [Application]
[Application]
public class MainApp : Application
{
public MainApp(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public override void OnCreate()
{
base.OnCreate();
//app init ...
}
}
任何人都可以向我澄清在 xamarin android 中扩展应用程序类的正确方法是什么
Can anybody clarify me what is the rightway to extend application class in xamarin android
更新
如果我不删除 [application],我就会收到以下错误
If I don't remove [application] I am exactly getting the below error
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Common.targets:错误:执行任务 GenerateJavaStubs 时出错:应用程序不能有具有 [Application] 属性的类型和[程序集:应用程序] 属性.(myapp.Droid)
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Common.targets: Error: Error executing task GenerateJavaStubs: Application cannot have both a type with an [Application] attribute and an [assembly:Application] attribute. (myapp.Droid)
如果我应用程序然后它编译但它抛出以下运行时错误
If I application then it compiles but it throws following runtime error
[AndroidRuntime] 关闭虚拟机 [AndroidRuntime] 致命异常:主 [AndroidRuntime] 进程:com.test.myapp,PID:6524[AndroidRuntime] java.lang.RuntimeException:无法实例化应用程序 com.test.myapp.MainApp: java.lang.ClassNotFoundException:在路径上没有找到类com.test.myapp.MainApp":DexPathList[[zip文件"/data/app/com.test.myapp-1/base.apk"],nativeLibraryDirectories=[/data/app/com.test.myapp-1/lib/arm,/data/app/com.test.myapp-1/base.apk!/lib/armeabi-v7a,/system/lib,/vendor/lib]] [AndroidRuntime] 在android.app.LoadedApk.makeApplication(LoadedApk.java:802)[AndroidRuntime] 在android.app.ActivityThread.handleBindApplication(ActivityThread.java:5377)[AndroidRuntime] 在android.app.ActivityThread.-wrap2(ActivityThread.java)[AndroidRuntime] 在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1545)[AndroidRuntime] 在android.os.Handler.dispatchMessage(Handler.java:102) [AndroidRuntime]在 android.os.Looper.loop(Looper.java:154) [AndroidRuntime] 在android.app.ActivityThread.main(ActivityThread.java:6119)[AndroidRuntime] 在 java.lang.reflect.Method.invoke(Native Method)[AndroidRuntime] 在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)[AndroidRuntime] 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)[AndroidRuntime] 引起:java.lang.ClassNotFoundException:没有在路径上找到类com.test.myapp.MainApp":DexPathList[[zip file"/data/app/com.test.myapp-1/base.apk"],nativeLibraryDirectories=[/data/app/com.test.myapp-1/lib/arm,/data/app/com.test.myapp-1/base.apk!/lib/armeabi-v7a,/system/lib,/vendor/lib]] [AndroidRuntime] 在dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)[AndroidRuntime] 在java.lang.ClassLoader.loadClass(ClassLoader.java:380) [AndroidRuntime]在 java.lang.ClassLoader.loadClass(ClassLoader.java:312)[AndroidRuntime] 在android.app.Instrumentation.newApplication(Instrumentation.java:992)[AndroidRuntime] 在android.app.LoadedApk.makeApplication(LoadedApk.java:796)[AndroidRuntime] ... 9 更多
[AndroidRuntime] Shutting down VM [AndroidRuntime] FATAL EXCEPTION: main [AndroidRuntime] Process: com.test.myapp, PID: 6524 [AndroidRuntime] java.lang.RuntimeException: Unable to instantiate application com.test.myapp.MainApp: java.lang.ClassNotFoundException: Didn't find class "com.test.myapp.MainApp" on path: DexPathList[[zip file "/data/app/com.test.myapp-1/base.apk"],nativeLibraryDirectories=[/data/app/com.test.myapp-1/lib/arm, /data/app/com.test.myapp-1/base.apk!/lib/armeabi-v7a, /system/lib, /vendor/lib]] [AndroidRuntime] at android.app.LoadedApk.makeApplication(LoadedApk.java:802) [AndroidRuntime] at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5377) [AndroidRuntime] at android.app.ActivityThread.-wrap2(ActivityThread.java) [AndroidRuntime] at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1545) [AndroidRuntime] at android.os.Handler.dispatchMessage(Handler.java:102) [AndroidRuntime] at android.os.Looper.loop(Looper.java:154) [AndroidRuntime] at android.app.ActivityThread.main(ActivityThread.java:6119) [AndroidRuntime] at java.lang.reflect.Method.invoke(Native Method) [AndroidRuntime] at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) [AndroidRuntime] at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) [AndroidRuntime] Caused by: java.lang.ClassNotFoundException: Didn't find class "com.test.myapp.MainApp" on path: DexPathList[[zip file "/data/app/com.test.myapp-1/base.apk"],nativeLibraryDirectories=[/data/app/com.test.myapp-1/lib/arm, /data/app/com.test.myapp-1/base.apk!/lib/armeabi-v7a, /system/lib, /vendor/lib]] [AndroidRuntime] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) [AndroidRuntime] at java.lang.ClassLoader.loadClass(ClassLoader.java:380) [AndroidRuntime] at java.lang.ClassLoader.loadClass(ClassLoader.java:312) [AndroidRuntime] at android.app.Instrumentation.newApplication(Instrumentation.java:992) [AndroidRuntime] at android.app.LoadedApk.makeApplication(LoadedApk.java:796) [AndroidRuntime] ... 9 more
这是我的清单文件
推荐答案
在 AssemblyInfo.cs
In AssemblyInfo.cs
注释掉
#if DEBUG
[Application(Debuggable=true)]
#else
[Application(Debuggable = false)]
#endif
并将其移至您的应用程序之上
and move that on top of your application
#if DEBUG
[Application(Debuggable=true)]
#else
[Application(Debuggable = false)]
#endif
public class MainApp : Application
{
public MainApp(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public override void OnCreate()
{
base.OnCreate();
//app init ...
}
}
基本上你已经定义了两个地方,这就是为什么会出现这个问题.在AssemblyInfo.cs 中注释掉并添加扩展类就可以了
Basically you have defined it two places that's why this problem occurs. Commenting out in AssemblyInfo.cs and adding in the extended class will work out
仅供参考:
清单没问题
这篇关于如何在 xamarin android 中扩展应用程序类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!