IllegalStateException异常的Andr​​oid [英] IllegalStateException android

查看:226
本文介绍了IllegalStateException异常的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建一个记事本程序指的http:/ /www.valokafor.com/create-android-notepad-app-part-2/ 。我的发射活动是音符的名单,但是当我尝试运行应用程序它给这个错误 -

 产生的原因:java.lang.IllegalStateException:无效的表
 在com.example.android.sqlitedbase.data.NoteContentProvider.query(NoteContentProvider.java:70)

70号线NoteContentProvider.java的方法 -
公共游标查询(URI URI,字符串[]投影,选择字符串,字符串[] selectionArgs两个,字符串中将sortOrder)

  cursor.setNotificationUri(的getContext()getContentResolver(),URI);

有没有注意到最初在列表中,它试图获取一个来自内存,这可能导致这个错误。
以下是NoteContentProvider.java的内容

 公共类NoteContentProvider扩展ContentProvider的{
    私人DatabaseHelper dbHelper;    私有静态最后弦乐BASE_PATH_NOTE =注意事项;
    私有静态最后弦乐AUTHORITY =com.example.android.sqlitedbase.data.NoteContentProvider;
    公共静态最终乌里CONTENT_URI = Uri.parse(内容://+ AUTHORITY +/+ BASE_PATH_NOTE);
    私有静态最终诠释注= 100;
    私有静态最终诠释NOTES = 101;    私有静态最终UriMatcher URI_MATCHER =新UriMatcher(UriMatcher.NO_MATCH);
    静态{
        URI_MATCHER.addURI(权威,BASE_PATH_NOTE,NOTES);
        URI_MATCHER.addURI(权威,BASE_PATH_NOTE +/#注);    }    私人无效checkColumns(字符串[]投影){
        如果(投影!= NULL){
            HashSet的<串GT;请求=新的HashSet<串GT;(Arrays.asList(投影));
            HashSet的<串GT;可用=新的HashSet<串GT;(Arrays.asList(Constants.COLUMNS));
            如果(!available.containsAll(要求)){
                抛出新抛出:IllegalArgumentException(投影未知列);
            }
        }
    }    @覆盖
    公共布尔的onCreate(){
        dbHelper =新DatabaseHelper(的getContext());
        返回false;
    }    @覆盖
    公共光标查询(URI URI,字符串[]投影,选择字符串,字符串[] selectionArgs两个,字符串中将sortOrder){
        SQLiteQueryBuilder的QueryBuilder =新SQLiteQueryBuilder();
        checkColumns(凸起);        整型= URI_MATCHER.match(URI);
        开关(类型){
            情况说明:
                //没有做,如果查询是表
                打破;
            案例说明:
                queryBuilder.appendWhere(Constants.COLUMN_ID +=+ uri.getLastPathSegment());
                打破;
            默认:
                抛出新抛出:IllegalArgumentException(未知URI:+ URI);
        }
        SQLiteDatabase分贝= dbHelper.getWritableDatabase();
        光标光标= queryBuilder.query(分贝,投影,选择,selectionArgs两个,NULL,NULL,中将sortOrder);
        cursor.setNotificationUri(的getContext()getContentResolver(),URI); // 70号线这会导致错误的logcat
        返回游标;
    }    @覆盖
    公共字符串的getType(URI URI){
        返回null;
    }    @覆盖
    公共乌里插入(URI URI,ContentValues​​值){
        整型= URI_MATCHER.match(URI);
        SQLiteDatabase分贝= dbHelper.getWritableDatabase();
        龙ID;
        开关(类型){
            案例说明:
                ID = db.insert(Constants.NOTES_TABLE,空,价值);
                打破;
            默认:
                抛出新抛出:IllegalArgumentException(未知URI:+ URI);
        }
        的getContext()getContentResolver()有NotifyChange(URI,空)。
        返回Uri.parse(BASE_PATH_NOTE +/+ ID);
    }
    @覆盖
    公众诠释删除(URI URI,选择字符串,字符串[] selectionArgs两个){
        整型= URI_MATCHER.match(URI);
        SQLiteDatabase分贝= dbHelper.getWritableDatabase();
        INT affectedRows;
        开关(类型){
            案例说明:
                affectedRows = db.delete(Constants.NOTES_TABLE,选择selectionArgs两个);
                打破;            情况说明:
                字符串ID = uri.getLastPathSegment();
                如果(TextUtils.isEmpty(选择)){
                    affectedRows = db.delete(Constants.NOTES_TABLE,Constants.COLUMN_ID +=+ ID,NULL);
                }其他{
                    affectedRows = db.delete(Constants.NOTES_TABLE,Constants.COLUMN_ID +=+ ID +和+选择,selectionArgs两个);
                }
                打破;            默认:
                抛出新抛出:IllegalArgumentException(未知URI:+ URI);
        }
        的getContext()getContentResolver()有NotifyChange(URI,空)。
        返回affectedRows;
    }
    @覆盖
    公众诠释更新(开放的URI,ContentValues​​价值观,选择字符串,字符串[] selectionArgs两个){
        整型= URI_MATCHER.match(URI);
        SQLiteDatabase分贝= dbHelper.getWritableDatabase();
        INT affectedRows;
        开关(类型){
            案例说明:
                affectedRows = db.update(Constants.NOTES_TABLE,价值观,选择,selectionArgs两个);
                打破;            情况说明:
                字符串ID = uri.getLastPathSegment();
                如果(TextUtils.isEmpty(选择)){
                    affectedRows = db.update(Constants.NOTES_TABLE,价值观,Constants.COLUMN_ID +=+ ID,NULL);
                }其他{
                    affectedRows = db.update(Constants.NOTES_TABLE,价值观,Constants.COLUMN_ID +=+编号+和+选择,selectionArgs两个);
                }
                打破;            默认:
                抛出新抛出:IllegalArgumentException(未知URI:+ URI);
        }
        的getContext()getContentResolver()有NotifyChange(URI,空)。
        返回affectedRows;
    }
}

DatabaseHelper.java

 公共类DatabaseHelper扩展SQLiteOpenHelper
{
    私有静态最后弦乐DATABASE_NAME =simple_note_app.db;
    私有静态最终诠释DATABASE_VERSION = 1;
    私有静态最后弦乐COLUMN_ID =_ ID;
    私有静态最后弦乐COLUMN_NAME =名;
    私有静态最后弦乐COLUMN_TITLE =称号;
    私有静态最后弦乐COLUMN_CONTENT =内容;
    私有静态最后弦乐COLUMN_MODIFIED_TIME =modified_time;
    私有静态最后弦乐COLUMN_CREATED_TIME =CREATED_TIME;
    私有静态最后弦乐CREATE_TABLE_NOTE =创建表的说明
            +(+ COLUMN_ID +整数主键自动增量
            + COLUMN_TITLE +文本不为空,
            + COLUMN_CONTENT +文本不为空,
            + COLUMN_MODIFIED_TIME +整数NOT NULL,
            + COLUMN_CREATED_TIME +整数非空+);
    公共DatabaseHelper(上下文的背景下)
    {
        超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
    }
    @覆盖
    公共无效的onCreate(SQLiteDatabase DB)
    {
        db.execSQL(CREATE_TABLE_NOTE);
    }    @覆盖
    公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){    }
}


解决方案

 公共静态最终乌里CONTENT_URI = Uri.parse(内容://+ AUTHORITY +/+ BASE_PATH_NOTE );

请更改上面到下面的条款的条款。也就是说,更改为单/ aftercontent:

 公共静态最终乌里CONTENT_URI = Uri.parse(内容:/+ AUTHORITY +/+ BASE_PATH_NOTE);

I'm trying to build a notepad app referring http://www.valokafor.com/create-android-notepad-app-part-2/ . My launcher activity is the list of notes but it's giving this error when I try to run the app -

Caused by: java.lang.IllegalStateException: Invalid tables


 at com.example.android.sqlitedbase.data.NoteContentProvider.query(NoteContentProvider.java:70)

Line 70 in NoteContentProvider.java in the method - public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) is

cursor.setNotificationUri(getContext().getContentResolver(), uri);

There is no note initially in the list and it tries to fetch one from memory which possibly leads to this error. Following are the contents of the NoteContentProvider.java

public class NoteContentProvider extends ContentProvider {
    private DatabaseHelper dbHelper;

    private static final String BASE_PATH_NOTE = "notes";
    private static final String AUTHORITY = "com.example.android.sqlitedbase.data.NoteContentProvider";
    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH_NOTE);
    private static final int NOTE = 100;
    private static final int NOTES = 101;

    private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
    static {
        URI_MATCHER.addURI(AUTHORITY, BASE_PATH_NOTE, NOTES);
        URI_MATCHER.addURI(AUTHORITY, BASE_PATH_NOTE + "/#", NOTE);

    }

    private void checkColumns(String[] projection) {
        if (projection != null) {
            HashSet<String> request = new HashSet<String>(Arrays.asList(projection));
            HashSet<String> available = new HashSet<String>(Arrays.asList(Constants.COLUMNS));
            if (!available.containsAll(request)) {
                throw new IllegalArgumentException("Unknown columns in projection");
            }
        }
    }

    @Override
    public boolean onCreate() {
        dbHelper = new DatabaseHelper(getContext());
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
        checkColumns(projection);

        int type = URI_MATCHER.match(uri);
        switch (type){
            case NOTE:
                //there is not to do if the query is for the table
                break;
            case NOTES:
                queryBuilder.appendWhere(Constants.COLUMN_ID + " = " + uri.getLastPathSegment());
                break;
            default:
                throw new IllegalArgumentException("Unknown URI: " + uri);
        }
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Cursor cursor = queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
        cursor.setNotificationUri(getContext().getContentResolver(), uri); // Line 70 which causes error in Logcat
        return cursor;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        int type = URI_MATCHER.match(uri);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Long id;
        switch (type){
            case NOTES:
                id = db.insert(Constants.NOTES_TABLE, null, values);
                break;
            default:
                throw new IllegalArgumentException("Unknown URI: " + uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return Uri.parse(BASE_PATH_NOTE + "/" + id);
    }


    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int type = URI_MATCHER.match(uri);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        int affectedRows;
        switch (type) {
            case NOTES:
                affectedRows = db.delete(Constants.NOTES_TABLE, selection, selectionArgs);
                break;

            case NOTE:
                String id = uri.getLastPathSegment();
                if (TextUtils.isEmpty(selection)) {
                    affectedRows = db.delete(Constants.NOTES_TABLE, Constants.COLUMN_ID + "=" + id, null);
                } else {
                    affectedRows = db.delete(Constants.NOTES_TABLE, Constants.COLUMN_ID + "=" + id + " and " + selection, selectionArgs);
                }
                break;

            default:
                throw new IllegalArgumentException("Unknown URI: " + uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return affectedRows;
    }


    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        int type = URI_MATCHER.match(uri);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        int affectedRows;
        switch (type) {
            case NOTES:
                affectedRows = db.update(Constants.NOTES_TABLE, values, selection, selectionArgs);
                break;

            case NOTE:
                String id = uri.getLastPathSegment();
                if (TextUtils.isEmpty(selection)) {
                    affectedRows = db.update(Constants.NOTES_TABLE, values, Constants.COLUMN_ID + "=" + id, null);
                } else {
                    affectedRows = db.update(Constants.NOTES_TABLE, values, Constants.COLUMN_ID + "=" + id + " and " + selection, selectionArgs);
                }
                break;

            default:
                throw new IllegalArgumentException("Unknown URI: " + uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return affectedRows;
    }
}

DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper
{
    private static final String DATABASE_NAME="simple_note_app.db";
    private static final int DATABASE_VERSION=1;
    private static final String COLUMN_ID="_id";
    private static final String COLUMN_NAME="name";


    private static final String COLUMN_TITLE="title";
    private static final String COLUMN_CONTENT="content";
    private static final String COLUMN_MODIFIED_TIME="modified_time";
    private static final String COLUMN_CREATED_TIME="created_time";
    private static final String CREATE_TABLE_NOTE="create table note"
            +"("+COLUMN_ID+" integer primary key autoincrement, "
            + COLUMN_TITLE+" text not null, "
            +COLUMN_CONTENT+" text not null, "
            +COLUMN_MODIFIED_TIME+" integer not null, "
            +COLUMN_CREATED_TIME+" integer not null "+")";


    public DatabaseHelper(Context context)
    {
        super(context,DATABASE_NAME,null,DATABASE_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db)
    {
        db.execSQL(CREATE_TABLE_NOTE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

解决方案

public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH_NOTE);

Please change the clause above to the clause below. That is, change to a single / aftercontent:

public static final Uri CONTENT_URI = Uri.parse("content:/" + AUTHORITY + "/" + BASE_PATH_NOTE);

这篇关于IllegalStateException异常的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆