IllegalStateException android

时间:2016-02-17 06:19:01

标签: android exception illegalstateexception

我正在尝试构建一个引用http://www.valokafor.com/create-android-notepad-app-part-2/的记事本应用。我的启动器活动是笔记列表,但是当我尝试运行应用程序时它会出现此错误 -

Caused by: java.lang.IllegalStateException: Invalid tables


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

方法中NoteContentProvider.java中的第70行 - public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)

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

列表中最初没有注释,它尝试从内存中获取一个可能导致此错误的内容。 以下是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) {

    }
}

2 个答案:

答案 0 :(得分:0)

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

请将上述条款更改为以下条款。也就是说,改为单个/后续内容:

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

答案 1 :(得分:0)

问题可能是,因为当您尝试在设备上多次运行应用程序时,您已多次更改数据库模式。它可能在那里搞砸了。

一个简单的选项就是问题,尝试卸载应用程序并从头开始重新运行应用程序。

这就是你有

的原因

DATABASE_VERSION

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

    }

处理这种情况。

OH WAIT,

或者表格创建字符串可能存在错误 - notenotes以及Constants.NOTES_TABLE

的冲突