简体   繁体   English

无法获取提供程序:ClassNotFoundException

[英]Unable to get provider : ClassNotFoundException

I have MyContentProvider in my app which works fine when I develop and run in debug mode. 我在我的应用程序中有MyContentProvider ,当我在调试模式下开发和运行时它工作正常。

<provider android:name=".MyContentProvider"
        android:authorities="com.contactcities"
        android:exported="false">
</provider>

But when I export the app, download it and run it, it crashes instantly : 但是当我导出应用程序,下载并运行它时,它会立即崩溃:

10-10 18:24:37.682 E/AndroidRuntime(10428): FATAL EXCEPTION: main
10-10 18:24:37.682 E/AndroidRuntime(10428): java.lang.RuntimeException: Unable to get provider com.contactcities.MyContentProvider: java.lang.ClassNotFoundException: com.contactcities.MyContentProvider in loader dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar:/data/app/com.contactcities-1.apk]
10-10 18:24:37.682 E/AndroidRuntime(10428): at android.app.ActivityThread.installProvider(ActivityThread.java:4509)
10-10 18:24:37.682 E/AndroidRuntime(10428): at android.app.ActivityThread.installContentProviders(ActivityThread.java:4281)
10-10 18:24:37.682 E/AndroidRuntime(10428): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4237)
10-10 18:24:37.682 E/AndroidRuntime(10428): at android.app.ActivityThread.access$3000(ActivityThread.java:125)
10-10 18:24:37.682 E/AndroidRuntime(10428): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2071)
10-10 18:24:37.682 E/AndroidRuntime(10428): at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 18:24:37.682 E/AndroidRuntime(10428): at android.os.Looper.loop(Looper.java:123)

I can reproduce it on all my devices, 2.2, 4.0, 4.1 我可以在我的所有设备上重现它,2.2,4.0,4.1

I have read through numerous threads today. 我今天已阅读了很多主题。 Some of them blaming ProGuard for this. 其中一些人指责ProGuard。 I have tried adding 我试过添加

-keep public class com.contactcities.MyContentProvider

but with no luck. 但没有运气。

When I disable proguard, by not putting proguard.config=proguard.cfg in my project.properties . 当我禁用proguard时,不要在我的project.properties放入proguard.config=proguard.cfg It still gives the same error in release version. 它仍然在发布版本中提供相同的错误。 Debug is again fine. 调试再次没问题。 Maybe its not enough to disable proguard like that? 也许这还不足以像那样禁用proguard?

Maybe the hint is that it refers to maps.jar in this crash. 也许提示是它在这次崩溃中引用了maps.jar。 Im not sure why it does that 我不知道为什么会那样做

in loader dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar

Any clues will be much appreciated 任何线索将非常感激

Argh, thought I tried that, but seems like I didnt. 唉,以为我试过了,但好像我没有。

Project -> clean made it work Project - > clean使它工作

face palm 面掌

I was getting the same error, turns out I had the wrong android:name attribute value for my provider tag in AndroidManifest.xml : 我得到了同样的错误,结果我在AndroidManifest.xml中为我的提供者标签设置了错误的android:name属性值:

<provider
    android:authorities="com.example.ProviderClass"
    android:name=".ProviderClass"
    android:exported="false" >
</provider>

So double check this value is as above .ProviderClass or whatever your class name is (prefixed with a dot). 所以仔细检查这个值是否如上所述.ProviderClass或你的类名是什么(以点为前缀)。 Then do Project > Clean and it will work. 然后做Project> Clean,它会起作用。

The authorities attribute needs to match the content authority while the name attribute needs to match the class name of your Custom Provider class. authority属性需要匹配内容权限,而name属性需要与Custom Provider类的类名匹配。

For eg: 例如:

      <provider
         android:authorities="com.richa.myapp"
         android:name=".data.CustomProvider"/>

Maybe my inexperience will help someone else with this bug. 也许我的经验不足会帮助其他人解决这个问题。 It was utterly cryptic to me, but like everything in computers obvious once you know. 这对我来说是完全神秘的,但是就像你知道的那样,计算机中的一切都很明显。

I was following this tutorial, which is overall very good: http://www.vogella.com/tutorials/AndroidSQLite/article.html 我正在学习本教程,总体来说非常好: http//www.vogella.com/tutorials/AndroidSQLite/article.html

Early on, he says to create a package called "contentprovider" and then a class within it "MyTodoContentProvider". 早期,他说要创建一个名为“contentprovider”的包,然后创建一个名为“MyTodoContentProvider”的类。 Now anyone familiar with Java-style naming conventions would have seen this was supposed to be relative to the main package. 现在任何熟悉Java风格命名约定的人都会看到这应该是相对于主包的。 But I was new to this all, so I created it over on the side somewhere. 但我对这一切都很陌生,所以我在某处创造了它。

All through building the code, things worked fine. 通过构建代码,一切正常。 Eclipse found and included this package, and showed no errors. Eclipse找到并包含此包,并且没有显示任何错误。 Then the crash and the cryptic "Unable to get provider" message. 然后崩溃和神秘的“无法获取提供者”消息。 The path in the error message, which I now understand was the non-existent path where the code was looking for the class, was so long and contained so many iterations of "contentprovider" it was hard to see what it was referring to or what was wrong with it. 错误消息中的路径,我现在理解的是代码正在寻找类的不存在的路径,是如此之长并且包含了很多“contentprovider”的迭代,很难看出它所指的是什么或者是什么这是错的。

I suspected the problem was in the manifest file, but I was sure it was in "android:authority". 我怀疑问题出在清单文件中,但我确信它是在“android:authority”中。 That was a bit non-intuitive. 这有点不直观。 The string in this element in the manifest corresponds to a public member you expose in your provider class. 清单中此元素中的字符串对应于您在提供程序类中公开的公共成员。 That member is named "CONTENT_URI". 该成员名为“CONTENT_URI”。 But the two do not exactly match. 但两者并不完全匹配。 For example: 例如:

private static final String AUTHORITY = "com.vegnab.provider";
private static final String BASE_PATH = "todos";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH);

This ends up with: 最终得到:

CONTENT_URI = "content://com.vegnab.provider/todos"

(Note that AUTHORITY itself is a private member, not externally available.) (请注意,AUTHORITY本身是私有成员,不是外部可用的。)

The corresponding manifest line would be: 相应的清单行将是:

android:authorities="com.vegnab.provider"

This is the middle part between the scheme "content://" and the base path. 这是方案“content://”和基本路径之间的中间部分。

The problem was actually in the manifest line: 问题实际上在清单行中:

android:name=".contentprovider.MyTodoContentProvider"

I had an error-free class named "MyTodoContentProvider" in a package named "contentprovider", so what was the problem? 我在一个名为“contentprovider”的包中有一个名为“MyTodoContentProvider”的无错误类,那么问题是什么? The problem was that the package was in the wrong place. 问题是包裹在错误的地方。 It was supposed to be relative to the main package. 它应该是相对于主要包裹。 That's what the dot, first in the name, means. 这就是名字中首先出现的点。 It should follow the same syntax as the "android:name" lines of the activities, in their sections of the manifest. 它应该遵循与活动的“android:name”行相同的语法,在清单的各个部分中。

If you find this useful, please one-up. 如果您觉得这很有用,请一次性。 Once you solve this problem, it's easy to forget how cryptic it was. 一旦你解决了这个问题,就很容易忘记它是多么神秘。

You might have passed your class limit on older OS versions—before Android 5.0 (API level 21) replaced the DEX runtime with ART. 在Android 5.0(API级别21)用ART取代DEX运行时之前,您可能已经通过了旧版操作系统版本的类限制。 If you're right on the edge of the limit, you can pass compilation but get this error during startup. 如果您处于限制的边缘,则可以通过编译,但在启动期间会出现此错误。

Try enabling multi-dex as described in the Android Enable Multidex for Apps with Over 64K Methods documentation. 尝试启用multi-dex,如Android 启用Multidex for Apps with Over 64K Methods文档中所述。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Facebook“无法获取提供者” java.lang.ClassNotFoundException - Android Facebook “Unable to get provider” java.lang.ClassNotFoundException 无法获取提供者com.google.firebase.provider.FirebaseInitProvider:java.lang.ClassNotFoundException - Unable to get provider com.google.firebase.provider.FirebaseInitProvider: java.lang.ClassNotFoundException 无法获取提供程序abc.xxx.Datapro:java.lang.ClassNotFoundException - Unable to get provider abc.xxx.Datapro: java.lang.ClassNotFoundException 无法获取提供程序android.arch.lifecycle.LifecycleRuntimeTrojanProvider:java.lang.ClassNotFoundException - Unable to get provider android.arch.lifecycle.LifecycleRuntimeTrojanProvider: java.lang.ClassNotFoundException 无法获取提供程序 com.facebook.internal.FacebookInitProvider: java.lang.ClassNotFoundException - Unable to get provider com.facebook.internal.FacebookInitProvider: java.lang.ClassNotFoundException 尝试为 Unity 构建 Android 插件时无法获取提供程序 androidx.core.content.FileProvider ClassNotFoundException - Unable to get provider androidx.core.content.FileProvider ClassNotFoundException when trying to build an Android Plugin for Unity 无法获得提供者com.crashlytics.android.CrashlyticsInitProvider java.lang.ClassNotFoundException - Unable to get provider com.crashlytics.android.CrashlyticsInitProvider java.lang.ClassNotFoundException 无法获取提供者com.google.android.gms.ads.MobileAdsInitProvider:java.lang.ClassNotFoundException-与multidex不相关 - Unable to get provider com.google.android.gms.ads.MobileAdsInitProvider: java.lang.ClassNotFoundException - NOT multidex related AdMob 导致:无法获取提供程序 com.google.android.gms.ads.MobileAdsInitProvider:java.lang.ClassNotFoundException - AdMob causing: unable to get provider com.google.android.gms.ads.MobileAdsInitProvider: java.lang.ClassNotFoundException Android:无法获取提供程序 - Android: Unable to get provider
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM