第一次在这里问,所以请对我严格。 :)
问了很多次问题,但我仍然不知道我的问题是什么。
E / Android运行时:致命异常:主要 流程:com.hfad.mydic,PID:4481 java.lang.IllegalArgumentException:无法绑定索引1的参数,因为索引超出范围。该语句具有0个参数。 在android.database.sqlite.SQLiteProgram.bind(SQLiteProgram.java:212) 在android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:166) 在android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:200) 在android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:49) 在android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1408) 在android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1347) 在com.hfad.mydic.DBHelper.getSentence(DBHelper.java:100) 在com.hfad.mydic.DetailFragment.onViewCreated(DetailFragment.java:61)
...
这是我的DBHelper
public ArrayList<String> getSentence(int dicType) {
String tableName = getTableName(dicType);
String q = " SELECT * FROM " + tableName;
Cursor result = mDB.rawQuery(q, null);
ArrayList<String> source = new ArrayList<>();
while (result.moveToNext()) {
source.add(result.getString(result.getColumnIndex(COL_text)));
}
return source;
}
public Sentence getSentence(String id, int dicType) {
String tableName = getTableName(dicType);
String q = " SELECT * FROM " + tableName + " WHERE upper([id])=upper('?')";
Cursor result = mDB.rawQuery(q, new String[]{id}); //What is wrong here?
Sentence sentence = new Sentence();
while (result.moveToNext()) {
sentence.id = result.getString(result.getColumnIndex(COl_id));
sentence.textlanguage = result.getString(result.getColumnIndex(COL_text));
}
return sentence;
}
DetailFragment
public class DetailFragment extends Fragment {
private String DFvalue = "";
private TextView tvWord;
private ImageButton btnBookmark, btnVolume;
private WebView tvWordTranslate;
private DBHelper mDBHelper;
private int mDicType;
public DetailFragment() {
// Required empty public constructor
}
public static DetailFragment getNewInstance(String value, DBHelper dbHelper, int dicType) {
DetailFragment fragment = new DetailFragment();
fragment.DFvalue = value;
fragment.mDBHelper = dbHelper;
fragment.mDicType = dicType;
return fragment;
}
...
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
tvWord = view.findViewById(R.id.tvWord);
tvWordTranslate = view.findViewById(R.id.tvWordTranslate);
btnBookmark = view.findViewById(R.id.btnBookmark);
btnVolume = view.findViewById(R.id.btnVolume);
Sentence sentence = mDBHelper.getSentence(DFvalue, mDicType); //What is wrong here?
tvWord.setText(sentence.id);
tvWordTranslate.loadDataWithBaseURL(null, sentence.textlanguage, "text/html", "utf-8", null);
Sentence bookmarkSentence = mDBHelper.getSentenceFromBookmark(DFvalue);
int isMark = bookmarkSentence == null ? 0 : 1;
btnBookmark.setTag(isMark);
int icon = bookmarkSentence == null ? R.drawable.ic_bookmark_border : R.drawable.ic_bookmark_fill;
btnBookmark.setImageResource(icon);
btnBookmark.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int i = (int) btnBookmark.getTag();
if (i == 0) {
btnBookmark.setImageResource(R.drawable.ic_bookmark_fill);
btnBookmark.setTag(1);
} else if (i == 1) {
btnBookmark.setImageResource(R.drawable.ic_bookmark_border);
btnBookmark.setTag(0);
}
}
});
}
答案 0 :(得分:0)
诸如?
之类的参数不应用引号引起来。 '?'
是文字问号字符串,?
表示参数。