Java 尝试列出数据库条目时,应用程序崩溃

Java 尝试列出数据库条目时,应用程序崩溃,java,android,sqlite,Java,Android,Sqlite,在我将应用程序重新安装到手机上之前,一切都正常。如有必要,我可以发布我的代码。我不知道是什么导致了这个问题。 以下是日志: 04-19 19:31:48.799: E/AndroidRuntime(24963): FATAL EXCEPTION: main 04-19 19:31:48.799: E/AndroidRuntime(24963): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.examp

在我将应用程序重新安装到手机上之前,一切都正常。如有必要,我可以发布我的代码。我不知道是什么导致了这个问题。 以下是日志:

04-19 19:31:48.799: E/AndroidRuntime(24963): FATAL EXCEPTION: main
04-19 19:31:48.799: E/AndroidRuntime(24963): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.classorganizer/com.example.classorganizer.Monday}: android.database.sqlite.SQLiteException: no such table: diaries: , while compiling: SELECT * FROM diaries
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.os.Looper.loop(Looper.java:130)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.app.ActivityThread.main(ActivityThread.java:3687)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at java.lang.reflect.Method.invokeNative(Native Method)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at java.lang.reflect.Method.invoke(Method.java:507)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at dalvik.system.NativeStart.main(Native Method)
04-19 19:31:48.799: E/AndroidRuntime(24963): Caused by: android.database.sqlite.SQLiteException: no such table: diaries: , while compiling: SELECT * FROM diaries
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1501)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1380)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1334)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1416)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at com.cookbook.data.MyDB.getdiaries(MyDB.java:66)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at com.example.classorganizer.Monday$DiaryAdapter.getdata(Monday.java:86)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at com.example.classorganizer.Monday$DiaryAdapter.<init>(Monday.java:80)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at com.example.classorganizer.Monday.onCreate(Monday.java:68)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
04-19 19:31:48.799: E/AndroidRuntime(24963):    ... 11 more
以下是在列表中输出新创建行的文件:

package com.example.classorganizer;


import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.cookbook.data.Constants;
import com.cookbook.data.MyDB;




public class Monday extends ListActivity {



private static final int MyMenu = 0;
MyDB dba;
DiaryAdapter myAdapter;

private class MyDiary{
    public MyDiary(String t, String c){
        title=t;
        content=c;

        ListView listView = new ListView(Monday.this);
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                new EditListItemDialog(view.getContext()).show();

                return true;


            }
        });


}

    public String title;
    public String content;

}
@Override
protected void onCreate(Bundle savedInstanceState) {
    dba = new MyDB(this);
    dba.open();
    setContentView(R.layout.fragment_monday);





    super.onCreate(savedInstanceState);
    myAdapter = new DiaryAdapter(this);
    this.setListAdapter(myAdapter);
}



private class DiaryAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private ArrayList<MyDiary> fragment_monday;
    public DiaryAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
        fragment_monday = new ArrayList<MyDiary>();
        getdata();


    }

    public void getdata(){
        Cursor c = dba.getdiaries();
        startManagingCursor(c);
        if(c.moveToFirst()){
            do{
                String title =
                        c.getString(c.getColumnIndex(Constants.TITLE_NAME));
                String content =
                        c.getString(c.getColumnIndex(Constants.CONTENT_NAME));

                MyDiary temp = new MyDiary(title,content);
                fragment_monday.add(temp);
            } while(c.moveToNext());
        }

    }



    @Override
    public int getCount() {return fragment_monday.size();}
    public MyDiary getItem(int i) {return fragment_monday.get(i);}
    public long getItemId(int i) {return i;}
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        final ViewHolder holder;

        View v = arg1;
        if ((v == null) || (v.getTag() == null)) {
            v = mInflater.inflate(R.layout.diaryrow,  null);
            holder = new ViewHolder();
            holder.mTitle = (TextView)v.findViewById(R.id.name);

            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }

        holder.mdiary = getItem(arg0);
        holder.mTitle.setText(holder.mdiary.title);


        v.setTag(holder);

        return v;


    }

    public class ViewHolder {
        MyDiary mdiary;
        TextView mTitle;


    }

}




/** Called when the user clicks the Edit button */
public void visitDiary(View view) {
    Intent intent = new Intent(this, Diary.class);
    startActivity(intent);
}
/** Called when the user clicks the back button */
public void visitSchedule(View view) {
    Intent intent = new Intent(this, DisplayScheduleScreen.class);
    startActivity(intent);
}


}
package com.example.classorganizer;
导入java.util.ArrayList;
导入android.app.ListActivity;
导入android.content.Context;
导入android.content.Intent;
导入android.database.Cursor;
导入android.os.Bundle;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.AdapterView;
导入android.widget.BaseAdapter;
导入android.widget.ListView;
导入android.widget.TextView;
导入com.cookbook.data.Constants;
导入com.cookbook.data.MyDB;
公开课星期一延长活动时间{
私有静态final int MyMenu=0;
MyDB dba;
日记适配器;
私人课堂日记{
公共MyDiary(字符串t、字符串c){
title=t;
内容=c;
ListView ListView=新建ListView(星期一,本周);
setOnItemLongClickListener(新的AdapterView.OnItemLongClickListener(){
@凌驾
公共布尔值长单击(AdapterView父项、视图、整型位置、长id){
新建EditListItemDialog(view.getContext()).show();
返回true;
}
});
}
公共字符串标题;
公共字符串内容;
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
dba=新的MyDB(本);
dba.open();
setContentView(R.layout.fragment_周一);
super.onCreate(savedInstanceState);
myAdapter=新日记适配器(此);
这个.setListAdapter(myAdapter);
}
私有类DiaryAdapter扩展了BaseAdapter{
私人停车场;
私人ArrayList fragment_周一;
公共日记适配器(上下文){
mInflater=LayoutInflater.from(上下文);
fragment_monday=new ArrayList();
getdata();
}
public void getdata(){
游标c=dba.getdiaries();
开始管理光标(c);
if(c.moveToFirst()){
做{
字符串标题=
c、 getString(c.getColumnIndex(Constants.TITLE_NAME));
字符串内容=
c、 getString(c.getColumnIndex(Constants.CONTENT_NAME));
MyDaily temp=新MyDaily(标题、内容);
添加(临时);
}而(c.moveToNext());
}
}
@凌驾
public int getCount(){return fragment_monday.size();}
公共MyDaily getItem(int i){返回片段_周一。获取(i);}
公共长getItemId(int i){return i;}
公共视图getView(int arg0、视图arg1、视图组arg2){
最终持票人;
视图v=arg1;
if((v==null)| |(v.getTag()==null)){
v=最小充气量(R.layout.diaryrow,空);
holder=新的ViewHolder();
holder.mTitle=(TextView)v.findViewById(R.id.name);
v、 setTag(支架);
}否则{
holder=(ViewHolder)v.getTag();
}
holder.mdiary=getItem(arg0);
holder.mTitle.setText(holder.mdiary.title);
v、 setTag(支架);
返回v;
}
公共类视图持有者{
我的日记;
文本视图mTitle;
}
}
/**当用户单击编辑按钮时调用*/
公共无效访问库(视图){
意向意向=新意向(本,日记类);
星触觉(意向);
}
/**当用户单击后退按钮时调用*/
公共无效访问计划(视图){
Intent Intent=新的Intent(这是DisplayScheduleScreen.class);
星触觉(意向);
}
}

这是您的问题:
SQLiteException:没有这样的表:日记

在尝试从数据库中选择之前,请检查该表是否已在数据库中创建。

这是您的问题:

no such table: diaries
问题很可能是,自从你说你重新安装了应用程序后,你还没有创建要从中选择的数据库或表

编辑:现在您发布了数据库创建脚本,出现语法错误:

private static final String CREATE_TABLE="create table "+
Constants.TABLE_NAME+" ("+
Constants.KEY_ID+" integer primary key autoincrement, "+
Constants.TITLE_NAME+" text not null, "+
Constants.CONTENT_NAME+" text not null, "
;

您将把Constants.TABLE\u NAME后面的括号保留为打开状态。在Constants.CONTENT\u NAME之后,写:
+“text not null)”

问题是这一行:

Constants.CONTENT_NAME+" text not null, "
删除最后一个逗号并添加右括号:

Constants.CONTENT_NAME+" text not null)"
您的
onCreate()
有SQL语法问题,但您捕获了
SQLiteException
,因此框架认为表创建成功。您不应该在sqlite助手生命周期方法中捕获异常。如果有问题,应该将其传播到调用方,而不是默默地忽略

至于语法问题,

private static final String CREATE_TABLE="create table "+
Constants.TABLE_NAME+" ("+
Constants.KEY_ID+" integer primary key autoincrement, "+
Constants.TITLE_NAME+" text not null, "+
Constants.CONTENT_NAME+" text not null, "
;
应该是

private static final String CREATE_TABLE="create table "+
Constants.TABLE_NAME+" ("+
Constants.KEY_ID+" integer primary key autoincrement, "+
Constants.TITLE_NAME+" text not null, "+
Constants.CONTENT_NAME+" text not null)"
;

您应该在创建表的位置发布代码。问题是存在的。但这怎么可能,因为它在以前尝试重新安装应用程序时创建了表?当我更改它时,日记和_id抱怨无法解决为变量…它现在创建的数据库为空
private static final String CREATE_TABLE="create table "+
Constants.TABLE_NAME+" ("+
Constants.KEY_ID+" integer primary key autoincrement, "+
Constants.TITLE_NAME+" text not null, "+
Constants.CONTENT_NAME+" text not null, "
;
private static final String CREATE_TABLE="create table "+
Constants.TABLE_NAME+" ("+
Constants.KEY_ID+" integer primary key autoincrement, "+
Constants.TITLE_NAME+" text not null, "+
Constants.CONTENT_NAME+" text not null)"
;