我是应用程序创建的新手,由于某种原因,我的应用程序无法打开名为“ Full Book”的表,该表将在活动中使用“材料搜索栏”进行搜索。因此,每次我在物理设备上安装该应用程序并尝试打开搜索活动时,它都会崩溃,并显示以下错误消息:
Process: uk.co.crystalclearlanguage.httpwww.dyagetmedoc, PID: 31912
java.lang.RuntimeException: Unable to start activity ComponentInfo{uk.co.crystalclearlanguage.httpwww.dyagetmedoc/uk.co.crystalclearlanguage.httpwww.dyagetmedoc.Main2Activity}: android.database.sqlite.SQLiteException: no such table: FullBook (code 1): , while compiling: SELECT Phrase FROM FullBook
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(no such table: FullBook (code 1): , while compiling: SELECT Phrase FROM FullBook)
#################################################################
但是,真正奇怪的是,当我使用在同一数据库中查找另一个表的另一个活动,然后尝试运行搜索功能时,它运行得很好。
这是我用于搜索功能的数据库助手:
public class Database extends SQLiteAssetHelper {
private static final String DB_NAME="DoYouGetMeDoc.db";
private static final int DB_VER=1;
public Database(Context context) {
super(context, DB_NAME, null, DB_VER);
}
public List<Items> getFullBook(){
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String[] sqliteSelect={"Phrase","Description","Example", "Chapter"};
String tableName="FullBook";
qb.setTables(tableName);
Cursor cursor = qb.query(db,sqliteSelect,null,null,null,null,null);
List<Items> results = new ArrayList<>();
if (cursor.moveToFirst()){
do {
Items items = new Items();
items.setPhrase(cursor.getString(cursor.getColumnIndex("Phrase")));
items.setDescription(cursor.getString(cursor.getColumnIndex("Description")));
items.setExample(cursor.getString(cursor.getColumnIndex("Example")));
items.setChapter(cursor.getString(cursor.getColumnIndex("Chapter")));
results.add(items);
}while (cursor.moveToNext());
}
return results;
}
public List<String> getPhrases(){
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String[] sqliteSelect={"Phrase"};
String tableName="FullBook";
qb.setTables(tableName);
Cursor cursor = qb.query(db,sqliteSelect,null,null,null,null,null);
List<String> results = new ArrayList<>();
if (cursor.moveToFirst()){
do {
results.add(cursor.getString(cursor.getColumnIndex("Phrase")));
}while (cursor.moveToNext());
}
return results;
}
public List<Items> getPhrasesbyPhrase(String phrase)
{
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String[] sqliteSelect={"Phrase","Description","Example","Chapter"};
String tableName="FullBook";
qb.setTables(tableName);
Cursor cursor = qb.query(db,sqliteSelect,"Phrase like ?",new String[]{"%"+phrase+"%"},null,null,null);
List<Items> results = new ArrayList<>();
if (cursor.moveToFirst()){
do {
Items items = new Items();
items.setPhrase(cursor.getString(cursor.getColumnIndex("Phrase")));
items.setDescription(cursor.getString(cursor.getColumnIndex("Description")));
items.setExample(cursor.getString(cursor.getColumnIndex("Example")));
items.setChapter(cursor.getString(cursor.getColumnIndex("Chapter")));
results.add(items);
}while (cursor.moveToNext());
}
return results;
}
}
Main2Activity(搜索活动):
public class Main2Activity extends AppCompatActivity{
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
SearchAdapter adapter;
MaterialSearchBar materialSearchBar;
List<String> suggestList = new ArrayList<>();
Database database;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
recyclerView= findViewById(R.id.recyler_search);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
materialSearchBar = findViewById(R.id.search_bar);
database = new Database(this);
materialSearchBar.setHint("Search");
materialSearchBar.setCardViewElevation(10);
loadSuggestionList();
materialSearchBar.addTextChangeListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
List<String> suggest = new ArrayList<>();
for (String search:suggestList)
{
if (search.toLowerCase().contains(materialSearchBar.getText().toLowerCase()))
suggest.add(search);
}
materialSearchBar.setLastSuggestions(suggest);
}
@Override
public void afterTextChanged(Editable s) {
}
});
materialSearchBar.setOnSearchActionListener(new MaterialSearchBar.OnSearchActionListener() {
@Override
public void onSearchStateChanged(boolean enabled) {
if (!enabled)
adapter = new SearchAdapter(getBaseContext(),database.getFullBook());
recyclerView.setAdapter(adapter);
}
@Override
public void onSearchConfirmed(CharSequence text) {
startSearch(text.toString());
}
@Override
public void onButtonClicked(int buttonCode) {
}
});
adapter = new SearchAdapter(this,database.getFullBook());
recyclerView.setAdapter(adapter);
}
private void startSearch(String text) {
adapter = new SearchAdapter(this,database.getPhrasesbyPhrase(text));
recyclerView.setAdapter(adapter);
}
private void loadSuggestionList() {
suggestList = database.getPhrases();
materialSearchBar.setLastSuggestions(suggestList);
}
@Override
public void onBackPressed() {
Intent i= new Intent(Main2Activity.this,MainActivity.class);
startActivity(i);
finish();
}
}
任何帮助/建议将不胜感激
更新: 堆栈肯定表示“ DoYouGetMeDoc.db”数据库已打开。
如果有帮助,请使用搜索适配器/查看器:
class SearchViewHolder extends RecyclerView.ViewHolder {
public TextView phrase;
public TextView decription;
public TextView example;
public TextView chapter;
public SearchViewHolder(@NonNull View itemView) {
super(itemView);
phrase = itemView.findViewById(R.id.txt_Phrase);
decription = itemView.findViewById(R.id.txt_Description);
example = itemView.findViewById(R.id.txt_Example);
chapter = itemView.findViewById(R.id.txt_Chapter);
}
}
public class SearchAdapter extends RecyclerView.Adapter<SearchViewHolder> {
private Context context;
private List<Items> items;
public SearchAdapter(Context context, List<Items> items) {
this.context = context;
this.items = items;
}
@NonNull
@Override
public SearchViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
LayoutInflater layoutInflater = LayoutInflater.from(viewGroup.getContext());
View itemview = layoutInflater.inflate(R.layout.layout_item,viewGroup,false);
return new SearchViewHolder(itemview);
}
@Override
public void onBindViewHolder(@NonNull SearchViewHolder searchViewHolder, int i) {
searchViewHolder.phrase.setText(items.get(i).getPhrase());
searchViewHolder.decription.setText(items.get(i).getDescription());
searchViewHolder.example.setText(items.get(i).getExample());
searchViewHolder.chapter.setText(items.get(i).getChapter());
}
@Override
public int getItemCount() {
return items.size();
}
}
再次。特别感谢所有帮助和指导,因为这是应用程序的最后一部分,需要正确运行。
答案 0 :(得分:0)
我做了一些研究,但在SQLiteAssetHelper类中丢失了一个“实例”。所以我添加了以下内容:
private static Database instance;
public static Database getInstance(Context context){
if (null == instance){
instance = new Database(context);
}
return instance;
}
,然后在“ OnCreate”方法中将以下内容添加到我的MainActivity.Java中:
Database.getInstance(this);