android - ContentDescriptor 在实例化时抛出 NullPointerException

标签 android android-contentprovider android-contentresolver

我的很多用户在实例化内容描述符时都面临 NullPointerException 问题。

Fatal Exception: java.lang.NullPointerException: Attempt to read from field 'java.lang.String android.content.UriMatcher.mText' on a null object reference
       at android.content.UriMatcher.addURI(UriMatcher.java:186)
       at com.getsuperapp.chat.db.ContentDescriptor.getUriMatcher(SourceFile:25)
       at com.getsuperapp.chat.db.DatabaseProvider.query(SourceFile:33)
       at android.content.ContentProvider.query(ContentProvider.java:1017)
       at android.content.ContentProvider$Transport.query(ContentProvider.java:238)
       at android.content.ContentResolver.query(ContentResolver.java:497)
       at android.content.ContentResolver.query(ContentResolver.java:439)

可以找到设备和环境的集合here .

ContentDescriptor 文件是

/**
* A  few constants from other classes used in the file
*
* from UserTable.java
* public static final String PATH = "user_table";
* public static final int PATH_TOKEN = 10;
*/
public class ContentDescriptor {

    private static UriMatcher URI_MATCHER = null;
    private static Uri BASE_URI = null;

    public static UriMatcher getUriMatcher(Context appContext) {

        String AUTHORITY = appContext.getPackageName() + ".quickblox";

        if (URI_MATCHER == null) {
            URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
//The line below is throwing the exception.
            URI_MATCHER.addURI(AUTHORITY, UserTable.PATH, UserTable.PATH_TOKEN); 
            URI_MATCHER.addURI(AUTHORITY, DialogTable.PATH, DialogTable.PATH_TOKEN);
            URI_MATCHER.addURI(AUTHORITY, MessageTable.PATH, MessageTable.PATH_TOKEN);
            URI_MATCHER.addURI(AUTHORITY, ChatInfoTable.PATH, ChatInfoTable.PATH_TOKEN);
        }
        return URI_MATCHER;
    }

    public static Uri getBaseUri(Context appContext) {
        String AUTHORITY = appContext.getPackageName() + ".quickblox";
        if (BASE_URI == null) {
            BASE_URI = Uri.parse("content://" + AUTHORITY);
        }
        return BASE_URI;
    }

}

如果您需要任何进一步的信息,请告诉我。任何人都可以让我对此有所了解。最后我无法重现崩溃。

编辑

伙计们不要在不阅读的情况下将其刷入任何其他 NPE 类别。它可以毫无问题地在许多设备上运行。初始化参数是常量。其崩溃所在的行也在代码中进行了注释。

最佳答案

您的 AUTHORITY、PATH 和 PATH_TOKEN(所有这些)的值是多少?根据值和 Android 版本,应用程序在您的应用程序上运行可能属于 bug fixed in UriMatcher .我想这就是您想开始挖掘的地方。另外,您是否收到来自开发者控制台或某个错误报告之王的错误报告?如果是,什么 Android 版本有共同的报告?它可能有助于重现故障。或者获取大量运行在不同版本上的设备/模拟器并尝试手动获取它。

关于android - ContentDescriptor 在实例化时抛出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38691613/

相关文章:

android - Glide - 添加过渡交叉淡入淡出会导致占位符调整大小

android - 我如何在android中绘制一个矩形并在其中放置一些文本然后放置它?

android - ContentValues 将值作为 sql 条件

java - android中如何通过图片id查看图片

android - java.lang.IllegalArgumentException 在 Android 10 设备中找不到卷 external_primary

OnRatingBarChangeListener 中的 Android Nougat (7.0/API 24) 评级计数错误

java - 对象未锁定

java - 需要共享 ContentProvider 代码但唯一的内容权限

带有 JSON 文件的 Android ContentProvider

Android 的 ContentResolver.isSyncActive 在实际同步结束之前返回 false