Android Contente提供程序错误:" java.lang.IllegalArgumentException:column' theme'不存在"

时间:2017-01-01 18:57:02

标签: android android-contentprovider

当我尝试使用ListAdapter在Fragment Class中读取我的数据库时出错。我觉得这不是一个难以解决的错误,并且没有太多要检查的代码,但我不知道为什么会出现此错误:" java.lang.IllegalArgumentException:column& #39;主题'不存在" 。 我的Fragment类中的函数 databaseView()应该读取我的数据库,因此我发出了获取所有数据的请求,因此我应该获取所有列。

我甚至可以在我的数据库中使用我的内容提供商添加一个新条目(来自另一个Fragment Class),我使用" sqliteviewer"在铬和它运作良好。

我使用此请求创建数据库:

"create table "+ STOCK_TABLE + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, THEME TEXT, QUESTION TEXT, REPONSE TEXT, DIFFICULTE FLOAT)"

这是我的Fragment类:

public class ViewCardEditor extends Fragment {
StockCard stock;
ViewGroup container;
ListView listView;
SimpleCursorAdapter cursorAdapter;




@Override
public View onCreateView(LayoutInflater inflater, ViewGroup c,
                         Bundle savedInstanceState) {
    container = c;
    View view = inflater.inflate(R.layout.fragment_view_card_editor, container, false);


    // Inflate the layout for this fragment
    if (container != null) {
        container.removeAllViews();
    }
    listView = (ListView) view.findViewById(R.id.listView);
    stock = new StockCard(c.getContext());


    databaseView();
    return view;
}

public void databaseView()
{
    ArrayList<String> list = new ArrayList<>();
    Cursor cursor = stock.getData();

    if(cursor.getCount() == 0)
    {
        Toast.makeText(container.getContext(), "Aucunes cartes en stock !!", Toast.LENGTH_LONG).show();
    }
    else
    {
        while(cursor.moveToNext())
        {
            list.add(cursor.getString(0));
            list.add(cursor.getString(1));
            list.add(cursor.getString(2));
            list.add(cursor.getString(3));
            list.add(cursor.getString(4));

//HERE is where I get my exception :            
ListAdapter adapter = new SimpleCursorAdapter(container.getContext(),
                    R.layout.card_stock,
                    cursor,
                    new String[]{stock._ID,
                            stock.THEME,
                            stock.QUESTION,
                            stock.REPONSE,
                            stock.DIFFICULTE},
                    new int[]{R.id.idList, R.id.themeList, R.id.questionList, R.id.reponseList, R.id.difficultList}, 0);

            listView.setAdapter(adapter);
        }
    }

}

}

我的SQLiteOpenHelper类

public class StockCard extends SQLiteOpenHelper {
public static final String STOCK_NAME ="StockCard.db";
public static final String STOCK_TABLE = "Carte_table";
private static int VERSION = 1;

public static final String _ID = "_id";
public static final String THEME = "theme";
public static final String QUESTION = "question";
public static final String REPONSE = "reponse";
public static final String DIFFICULTE = "difficulte";

private static StockCard instance;

//Constructeur pour AddCardEditor (Fragment de CardEditor) et ses fonctions
public StockCard(Context context) {
    super(context, STOCK_NAME, null, VERSION);
    //SQLiteDatabase db = this.getWritableDatabase();
}

public static StockCard getInstance(Context context) {
    if( instance == null ){
        instance = new StockCard(context);
    }
    return instance;
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table "+ STOCK_TABLE + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, THEME TEXT, QUESTION TEXT, REPONSE TEXT, DIFFICULTE FLOAT)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if(newVersion > oldVersion)
    {
        db.execSQL("DROP TABLE IF EXISTS "+STOCK_TABLE);
        onCreate(db);
    }

}

public Cursor getData()
{
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor data = db.rawQuery("select * from "+STOCK_TABLE, null);

    return data;
}

}

然后是我的ContentProvider:

public class CardContentProvider extends ContentProvider {
private StockCard stock;

public static String authority = "com.example.jean.cartememoire.CardContentProvider";
private static String path ="Carte_table";

public static final String _ID = "_id";
public static final String THEME = "theme";
public static final String QUESTION = "question";
public static final String REPONSE = "reponse";
public static final String DIFFICULTE = "difficulte"; //# = un chiffre
public static final String STOCK_TABLE = "Carte_table";

private static final int ID_STOCK_TABLE = 1;
private static final int ID_THEME = 2;
private static final int ID_QUESTION = 3;
private static final int ID_REPONSE = 4;
private static final int ID_DIFFICULTE = 5;
private static final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);

static {
    matcher.addURI(authority, STOCK_TABLE, ID_STOCK_TABLE);
    matcher.addURI(authority, THEME, ID_THEME);
    matcher.addURI(authority, QUESTION, ID_QUESTION);
    matcher.addURI(authority, REPONSE, ID_REPONSE);
    matcher.addURI(authority, DIFFICULTE, ID_DIFFICULTE);

}


@Override
public boolean onCreate() {
    stock = StockCard.getInstance(getContext());
    return true;
}

@Nullable
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    SQLiteDatabase db = stock.getReadableDatabase();
    int code = matcher.match(uri);
    long id;
    Cursor cursor;

    switch (code)
    {
        case ID_STOCK_TABLE:
            cursor = db.query(STOCK_TABLE, projection, selection,
                    selectionArgs, null, null, sortOrder);
            break;
        default:
            Log.d("Uri provider =", uri.toString());
            throw new UnsupportedOperationException("Pas encore implémenté");
    }

    return cursor;
}

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

@Nullable
@Override
public Uri insert(Uri uri, ContentValues values) {
    SQLiteDatabase db = stock.getWritableDatabase();
    int code = matcher.match(uri);
    long id;
    Uri.Builder builder = new Uri.Builder();

    switch(code)
    {
        case ID_STOCK_TABLE:
            System.out.println(values.toString());
            id = db.insert(STOCK_TABLE, null, values);
            builder.appendPath(STOCK_TABLE);
            break;
        default:
            throw new UnsupportedOperationException("Pas encore implémenté");
    }

    builder.authority(authority);
    builder = ContentUris.appendId(builder, id);

    return builder.build();
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
    return 0;
}

@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
    return 0;
}
}

感谢您的帮助,对不起,如果英语不好。

1 个答案:

答案 0 :(得分:1)

我认为您的问题是区分大小写

https://code.google.com/p/android/issues/detail?id=42636

你声明你的表是这样的:

 db.execSQL("create table "+ STOCK_TABLE + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, THEME TEXT, QUESTION TEXT, REPONSE TEXT, DIFFICULTE FLOAT)");

即。列名= THEME

然后您尝试使用以下方式访问它:

public static final String THEME = "theme";

即。列名= theme

CursorAdapter内。更改一个以匹配另一个,看看它是否有效。例如,更改CardContentProvider将是最简单的:

public static final String THEME = "THEME";