如何在Xamarin Android中扩展应用程序类 [英] How to extend application class in xamarin android

查看:139
本文介绍了如何在Xamarin Android中扩展应用程序类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想扩展Android应用程序,并且想重写oncreate方法. 但是由于某种原因,它非常可怕...仅当我删除[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

更新

如果我不删除[应用程序],我将得到以下错误

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]属性的类型和 [assembly: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]正在关闭VM [AndroidRuntime]致命异常: 主要[AndroidRuntime]进程:com.test.myapp,PID:6524 [AndroidRuntime] java.lang.RuntimeException:无法实例化 应用程序com.test.myapp.MainApp:java.lang.ClassNotFoundException: 在路径:DexPathList [[zip]中找不到类"com.test.myapp.MainApp" 文件 "/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(本机方法) [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:没有 在路径:DexPathList [[zip文件]中找到类"com.test.myapp.MainApp" "/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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆