如何在xamarin android

时间:2017-05-18 22:13:00

标签: c# xamarin xamarin.android android-manifest android-application-class

我想扩展Android应用程序,我想覆盖oncreate方法。 但由于某些原因,它非常糟糕......只有我删除[应用程序]我才能运行它但是根据这个link我们必须添加[应用程序]

[Application]
public class MainApp : Application
{
public MainApp(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}

public override void OnCreate()
{
    base.OnCreate();
    //app init ...
}
}

任何人都可以澄清一下在xamarin android

中扩展应用程序类的正确方法是什么

更新

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

  

/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Common.targets:   错误:执行任务GenerateJavaStubs时出错:应用程序无法执行   具有[Application]属性的类型和   [assembly:Application]属性。 (myapp.Droid)

如果我是应用程序,那么它会编译,但它会抛出运行时错误

  

[AndroidRuntime]关闭VM [AndroidRuntime]致命异常:   main [AndroidRuntime]进程:com.test.myapp,PID:6524   [AndroidRuntime] java.lang.RuntimeException:无法实例化   application 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] 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]     在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]引起: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] at   dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)   [AndroidRuntime] at   java.lang.ClassLoader.loadClass(ClassLoader.java:380)[AndroidRuntime]     在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更多

这是我的清单file

2 个答案:

答案 0 :(得分:7)

AssemblyInfo.cs

评论

     #if DEBUG
    [Application(Debuggable=true)]
    #else
    [Application(Debuggable = false)]
    #endif

并将其移到您的应用程序之上

#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中注释并在扩展类中添加将会计算出来

供参考:

清单是好的

答案 1 :(得分:5)

  1. 将ApplicationAttribute [Application]应用于您的Application子类,以在清单中注册此类

  2. 添加Java / JNI构造函数

    • .ctor(System.IntPtr, Android.Runtime.JniHandleOwnership)
    • 如果你不添加这个,那么当运行时试图实例化Java包装器时,你会收到一个错误....
  3. 覆盖OnCreate方法

    • 这避免了:[art] JNI RegisterNativeMethods: attempt to register 0 native methods因此您的应用程序子类未实例化。
  4. 示例:

    [Application]
    public class MainApplication : Application
    {
        public MainApplication(IntPtr javaReference, Android.Runtime.JniHandleOwnership transfer) : base(javaReference, transfer)
        {
        }
    
        public override void OnCreate()
        {
            base.OnCreate();
        }
    }