Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 ...
}
}

Can anybody clarify me what is the rightway to extend application class in xamarin android

Update

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: 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] 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

Here is my manifest file

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
136 views
Welcome To Ask or Share your Answers For Others

1 Answer

In AssemblyInfo.cs

comment out

     #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 ...
}
}

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

FYI:

Manifest is ok


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...