问题描述
我刚刚开始使用 Xamarin 和 VisualStudio 2017 在 C# 中开发 Android.与 Windows 生态系统完全不同的范式.Target 是一个商业应用程序,它将拥有特定的受众,但在我深入研究之前,我我只是想学习绳索,理解概念和程序..
I just started development for Android in C# using Xamarin and VisualStudio 2017. Quite a different paradigm from Windows eco-system.. Target is a business app, that will have a specific audience, but before i get deep into that i'm just trying to learn the ropes, understand concepts & procedures..
因此,我从最简单的应用程序开始,使用空白模板.这会导致创建一个带有活动 (MainActivity.cs) 和 2 个资源(Resources\layout\Main.axml、Res..\values\Strings.xml)和一些其他项目(例如 Properties\AndroidManifest.xml)的项目、GettingStarted.Xamarin 等).
Therefore, i start with simplest app, using Blank template. That results in creating a project with an activity (MainActivity.cs), and 2 resources (Resources\layout\Main.axml, Res..\values\Strings.xml), and a few other items (e.g. Properties\AndroidManifest.xml, GettingStarted.Xamarin, etc.).
我在附加的 BLU Vivo 5 Mini (Android 6.0 - API 23)
上编译并运行它.到目前为止一切顺利!
I compile and run it on an attached BLU Vivo 5 Mini (Android 6.0 - API 23)
. So far so good!
接下来是我第一次尝试玩任何东西......出人意料地结局很糟糕.
Next come my first attempts at playing with anything .. that surprisingly end badly.
1. 重命名 MainActivity 导致:
1. Renaming MainActivity results in:
未处理的异常:Java.Lang.RuntimeException: 无法实例化活动 ComponentInfo{App4.App4/md54bce54f405542800672a5cecd6c84592.MainActivity}: java.lang.ClassNotFoundException: 没有找到类 "md54bce54f4055428000067./App4.App4-1/base.apk"],nativeLibraryDirectories=[/data/app/App4.App4-1/lib/arm,/data/app/App4.App4-1/base.apk!/lib/armeabi-v7a,/vendor/lib,/system/lib]]
名称 MainActivity
是否是某种预定义的入口点,类似于 C/C++ 的 main()
?
我没有看到任何参考.但是[Activity(MainLauncher=true)]
属性有什么意义,如果不标记主/启动活动?
好的,现在我将其重命名.重新编译,运行.. 好的.
Is the name MainActivity
some kind of a predefined entry-point, similar to C/C++'s main()
?
I haven't seen any reference to that. But then what's the point of [Activity(MainLauncher=true)]
attribute, if not to mark the main/startup activity?
Fine, for now i rename it back. Recompile, run .. ok.
2. 删除 GettingStarted.Xamarin
文件(因为它只是一个链接列表,存在于每个项目中)似乎会导致同样的问题!?那只是一个 HTML 页面,所以是什么??
2. Deleting GettingStarted.Xamarin
file ('cause it's just a list of links, present in every project) seems to result in the same problem!? That's just an HTML page, so what gives??
好吧,我不会再碰这个文件了.关闭解决方案,重新创建项目,运行.. ok.
Fine, i won't be touching this file anymore. Close solution, re-create project, run .. ok.
3. 将 Properties\AssemblyInfo.cs 修改为:[assembly: AssemblyVersion( "1.0.*" )]
(以匹配其他版本的构建版本编号架构项目)导致同样的问题!...
(令人惊讶的是,修改 AssemblyFileVersion
并没有什么不好的影响 - 它可以运行;也许 Android 端被使用 *
-wildcard 的结果冒犯了?但为什么呢?..)
3. Modifying Properties\AssemblyInfo.cs to: [assembly: AssemblyVersion( "1.0.*" )]
(to match the build-version numbering schema for other projects) results in the same problem!..
(Surprisingly, modifying AssemblyFileVersion
has no bad effect - it runs; maybe Android side is offended by results of using *
-wildcard? But why?..)
好的,我将放弃所有最好的努力学习来保持源代码的组织和可维护性.
关闭解决方案,重新创建项目,运行.. ok.
Fine, i'll forgo all the best hard-learned efforts to keep source code organized and maintainable.
Close solution, re-create project, run .. ok.
4. 重命名 Main
布局(到 ActMain
甚至 MainActivity
- 匹配活动名称)导致另一个类似的启动异常.
4. Renaming Main
layout (to ActMain
or even MainActivity
- to match activity name) results in yet another similar startup exception.
傻瓜!",我说,然后又回来了.重新编译,运行.. 好的.我现在应该学会了吧?
"Fool!", says i, and revert back. Recompile, run .. ok. I should learn by now, right?
让我印象深刻的是代码总是可以编译,所有这些问题都在运行时出现!?!?
好吧,我放弃了,我不会修改 Xamarin 或 VS 预先生成的任何内容.为了使实验更干净,我重新启动 VS,取消所有以前的项目并创建一个新项目 - 让我们从一个空白模板重新开始.
Fine, i give up, i'm not gonna modify anything that Xamarin or VS generates upfront. To make the experiment clean even further i restart VS, kill off all the previous projects and create a new one - let's start fresh with a Blank template.
5. 进入应用程序的入口点将是一个登录页面",所以我添加了一些 TextView
、EditText
和 Button
控件到 Main.axml
:按钮还没有做任何事情 - 代码是空的.编译,运行 .. 到目前为止一切顺利.可爱!
5. Going forward the entry-point into the app would be a Log-in "page", so i add some TextView
, EditText
and Button
controls to the Main.axml
:Buttons don't do anything yet - code is empty. Compile, run .. so far so good. Lovely!
现在我添加一个新活动并为其命名ActSettings
.是的,我喜欢匈牙利符号,如果你愿意的话,我会讨厌我,但它帮助我保持我的对象有条理.但这不是重点:此时我应该可以随意命名一个新类,我不应该吗?
Now i add a new Activity and give it a name ActSettings
. Yes, i like Hungarian notation, hate me for that if you want, but it helps me keep my objects organized. But that is besides the point: at this moment i should be free to name a new class anything i want, shouldn't i?
接下来我创建一个相应的布局,并[显然]将其命名为ActSettings
.也将一些控件放入其中.
Next i create a corresponding layout, and [obviously] name it ActSettings
. Slap a few controls into it as well.
编译,运行.. 好的.
Compile, run .. ok.
还没有将活动链接在一起的代码 - 让我们添加它:
There was no code linking activities together yet - let's add it:
6. 这是解决方案和添加了操作的 MainActivity.cs 代码:我听到 Doug Marcaida 说:它会编译.但它会运行吗?"
6. Here's the solution and MainActivity.cs code with added action:I hear Doug Marcaida going: "It will compile. But will it run?"
7. 不,这就是我得到的(.NET 非常了解 ActSettings
类,但不了解 Java!?):
7. Nope, here's what i get (.NET knows ActSettings
class pretty well, but not Java!?):
02-02 13:47:11.686 W/monodroid( 5867): JNIEnv.FindClass(Type) caught unexpected exception: Java.Lang.ClassNotFoundException: md54e704423a01db64492900da8de435cc0.ActSettings ---> Java.Lang.ClassNotFoundException: Didn't find class "md54e704423a01db64492900da8de435cc0.ActSettings" on path: DexPathList[[zip file "/data/app/J7980ca.J7980ca-1/base.apk"],nativeLibraryDirectories=[/data/app/J7980ca.J7980ca-1/lib/arm, /data/app/J7980ca.J7980ca-1/base.apk!/lib/armeabi-v7a, /vendor/lib, /system/lib]]
02-02 13:47:11.686 W/monodroid( 5867): --- End of inner exception stack trace ---
02-02 13:47:11.686 W/monodroid( 5867): at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <657aa8fea4454dc898a9e5f379c58734>:0
02-02 13:47:11.686 W/monodroid( 5867): at Java.Interop.JniEnvironment+StaticMethods.CallStaticObjectMethod (Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00069] in <e736913786c2475188869561ae512b72>:0
02-02 13:47:11.686 W/monodroid( 5867): at Android.Runtime.JNIEnv.CallStaticObjectMethod (System.IntPtr jclass, System.IntPtr jmethod, Android.Runtime.JValue* parms) [0x0000e] in <0fb41b3e3d7346ce92c4a742b20078ff>:0
02-02 13:47:11.686 W/monodroid( 5867): at Android.Runtime.JNIEnv.CallStaticObjectMethod (System.IntPtr jclass, System.IntPtr jmethod, Android.Runtime.JValue[] parms) [0x00017] in <0fb41b3e3d7346ce92c4a742b20078ff>:0
02-02 13:47:11.686 W/monodroid( 5867): at Android.Runtime.JNIEnv.FindClass (System.String classname) [0x0003d] in <0fb41b3e3d7346ce92c4a742b20078ff>:0
02-02 13:47:11.686 W/monodroid( 5867): at Android.Runtime.JNIEnv.FindClass (System.Type type) [0x00015] in <0fb41b3e3d7346ce92c4a742b20078ff>:0
02-02 13:47:11.686 W/monodroid( 5867): --- End of managed Java.Lang.ClassNotFoundException stack trace ---
02-02 13:47:11.686 W/monodroid( 5867): java.lang.ClassNotFoundException: md54e704423a01db64492900da8de435cc0.ActSettings
02-02 13:47:11.686 W/monodroid( 5867): at java.lang.Class.classForName(Native Method)
02-02 13:47:11.687 W/monodroid( 5867): at java.lang.Class.forName(Class.java:324)
02-02 13:47:11.687 W/monodroid( 5867): at mono.android.view.View_OnClickListenerImplementor.n_onClick(Native Method)
02-02 13:47:11.687 W/monodroid( 5867): at mono.android.view.View_OnClickListenerImplementor.onClick(View_OnClickListenerImplementor.java:30)
02-02 13:47:11.687 W/monodroid( 5867): at android.view.View.performClick(View.java:5265)
02-02 13:47:11.687 W/monodroid( 5867): at android.view.View$PerformClick.run(View.java:21534)
02-02 13:47:11.687 W/monodroid( 5867): at android.os.Handler.handleCallback(Handler.java:815)
02-02 13:47:11.687 W/monodroid( 5867): at android.os.Handler.dispatchMessage(Handler.java:104)
02-02 13:47:11.687 W/monodroid( 5867): at android.os.Looper.loop(Looper.java:207)
02-02 13:47:11.687 W/monodroid( 5867): at android.app.ActivityThread.main(ActivityThread.java:5765)
02-02 13:47:11.687 W/monodroid( 5867): at java.lang.reflect.Method.invoke(Native Method)
02-02 13:47:11.687 W/monodroid( 5867): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
02-02 13:47:11.687 W/monodroid( 5867): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
02-02 13:47:11.687 W/monodroid( 5867): Caused by: java.lang.ClassNotFoundException: Didn't find class "md54e704423a01db64492900da8de435cc0.ActSettings" on path: DexPathList[[zip file "/data/app/J7980ca.J7980ca-1/base.apk"],nativeLibraryDirectories=[/data/app/J7980ca.J7980ca-1/lib/arm, /data/app/J7980ca.J7980ca-1/base.apk!/lib/armeabi-v7a, /vendor/lib, /system/lib]]
02-02 13:47:11.687 W/monodroid( 5867): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
02-02 13:47:11.687 W/monodroid( 5867): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
02-02 13:47:11.687 W/monodroid( 5867): at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
02-02 13:47:11.688 W/monodroid( 5867): ... 13 more
02-02 13:47:11.688 W/monodroid( 5867): Suppressed: java.lang.ClassNotFoundException: md54e704423a01db64492900da8de435cc0.ActSettings
02-02 13:47:11.688 W/monodroid( 5867): at java.lang.Class.classForName(Native Method)
02-02 13:47:11.688 W/monodroid( 5867): at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
02-02 13:47:11.688 W/monodroid( 5867): at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
02-02 13:47:11.688 W/monodroid( 5867): at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
02-02 13:47:11.688 W/monodroid( 5867): ... 14 more
02-02 13:47:11.688 W/monodroid( 5867): Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
Unhandled Exception:
Java.Lang.ClassNotFoundException: md54e704423a01db64492900da8de435cc0.ActSettings occurred
没关系,我怎么称呼它:SecondActivity
也不工作!
And it doesn't matter, what i call it: SecondActivity
does not work either!
8. 已经问过的最接近的类似问题是请注意,项目中的文件甚至没有提到一半的内容!然而,生成的输出完美地说明了所有活动,这只会加深我的困惑.
8. The closest similar question already asked is Second activity in Android, but a) it is from 2011, b) there's no accepted answer, c) answers point to AndroidManifest.xml
file.. Well, here it is:Notice, that the file in the project doesn't even mention half the stuff! Yet the generated output has all the activities spelled out perfectly, which only deepens my confusion.
9.另一个问题我如何解决 ClassNotFoundException? 可能是最接近的,但同样,它来自 2013 年,带有 Java
标签(不是 Android
、Xamarin
或任何与手机相关的东西).
我完全不知道,classpath
在哪里,如何设置以及设置什么,最重要的是为什么 Xamarin 或 VisualStudio-2017 一开始就不能正确地为我做这些?
9. Another question How do I resolve ClassNotFoundException? is probably closest, but again, it's from 2013 and it's tagged Java
(not Android
, Xamarin
or anything related to phones).
I have absolutely no clue, where is that classpath
, how and what to set it to, and most importantly why would Xamarin or VisualStudio-2017 not do that for me properly in the first place?
我希望现代 IDE 兜售平台支持将正确覆盖所有基础.最终,应用程序将需要通过 WiFi、后台/前台服务、通知等进行 TCP/UDP 通信.但我只是在做你好,世界!":o
I would expect that a modern IDE touting platform support would be properly covering all the bases. Eventually the app would need TCP/UDP communication over WiFi, background / foreground servi-ces, notifications, and what not. But i'm having a fit simply doing "Hello, world!" :o
我尝试运行(并重命名文件和类!)ActivityLifecycle 示例,它启动了第二个活动,它运行良好.. 前提是我没有更改任何名称.如果我这样做 - 它会失败,但恢复名称使其再次工作.我找不到该示例代码与我自己的代码之间的任何合理差异,这可以解释为什么 Android 找不到我的第二个活动.
I tried running (and renaming files and classes!) ActivityLifecycle example, which launches a second activity, and it works perfectly .. provided i have not changed any names. If i do - it fails, but reverting names makes it work again. I cannot find any reasonable differences between that sample's code and my own, which would explain why Android can't find my second activity.
我做错了什么?还是不做?请帮忙!
What am i doing wrong? Or not doing? Please help!
推荐答案
未处理的异常:Java.Lang.RuntimeException:无法实例化活动 ComponentInfo{App4.App4/md54bce54f405542800672a5cecd6c84592.MainActivity}:java.lang.ClassNotFoundException:没有找到类......
Unhandled Exception:Java.Lang.RuntimeException: Unable to instantiate activity ComponentInfo{App4.App4/md54bce54f405542800672a5cecd6c84592.MainActivity}: java.lang.ClassNotFoundException: Didn't find class ......
这是一个关于 Xamarin 的问题,它已在最新的 Visual Studio 版本中修复,它与您的代码无关.
您可以参考 Jon Douglas 的回答:Xamarin.Android 应用程序仅在干净编译时查找启动器活动.
此问题已修复并包含在 Visual Studio 15.5.5
或 Visual Studio 15.6 Preview 3
中.
此修复程序包含的相应 Xamarin.Android 版本为 Xamarin.Android 8.1.5.0(稳定版)
和 Xamarin.Android 8.2.0.6(预览版)
>
The respective Xamarin.Android version this fix is included in are Xamarin.Android 8.1.5.0 (Stable)
and Xamarin.Android 8.2.0.6 (Preview)
解决方案 1:
将您的 Visual Studio 更新到最新版本,Visual Studio 15.5.6 现已推出.
Solution 1:
Update your Visual Studio to the latest version, Visual Studio 15.5.6 is available now.
- 删除您的解决方案 bin&obj 文件夹
- 再次重建
这篇关于Xamarin Android:Java.Lang.ClassNotFoundException 启动第二个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!