在手機上重新安裝應用程序之前,一切都很順利。如有必要,我可以發佈我的代碼。我不知道是什麼導致了這個問題。 這裏是logcat的:嘗試列出數據庫條目時應用程序崩潰
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
這裏是我的MyDBhelper文件:
package com.cookbook.data;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class MyDBhelper extends SQLiteOpenHelper{
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 SQLiteDatabase db;
// database initialization
public MyDBhelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.v("MyDBhelper onCreate","Creating all the tables");
try {
this.db = db;
db.execSQL(CREATE_TABLE);
for(int i=1; i <= 48; i++) { insertdiary(db, "free",""); }
}
catch(SQLiteException ex) {
Log.v("Create table exception", ex.getMessage());
}
}
// Saves a diary entry to the database as name-value pairs in ContentValues instance
// then passes the data to the SQLitedatabase instance to do an insert
public long insertdiary(SQLiteDatabase db, String title, String content)
{
try{
ContentValues newTaskValue = new ContentValues();
newTaskValue.put(Constants.TITLE_NAME, title);
newTaskValue.put(Constants.CONTENT_NAME, content);
return db.insert(Constants.TABLE_NAME, null, newTaskValue);
} catch(SQLiteException ex) {
Log.v("Insert into database exception caught",
ex.getMessage());
return -1;
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion) {
Log.w("TaskDBAdapter", "Upgrading from version "+oldVersion
+" to "+newVersion
+", which will destroy all old data");
db.execSQL("drop table if exists "+Constants.TABLE_NAME);
onCreate(db);
}
}
這裏是MYDB文件:
package com.cookbook.data;
import com.example.classorganizer.Diary;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;
public class MyDB {
private static final String TABLE_NAME = null;
private static final String KEY_ID = null;
private SQLiteDatabase db;
private final Context context;
private final MyDBhelper dbhelper;
// Initializes MyDBHelper instance
public MyDB(Context c){
context = c;
dbhelper = new MyDBhelper(context, Constants.DATABASE_NAME, null,
Constants.DATABASE_VERSION);
}
// Closes the database connection
public void close()
{
db.close();
}
// Initializes a SQLiteDatabase instance using MyDBhelper
public void open() throws SQLiteException
{
try {
db = dbhelper.getWritableDatabase();
} catch(SQLiteException ex) {
Log.v("Open database exception caught", ex.getMessage());
db = dbhelper.getReadableDatabase();
}
}
// updates a diary entry (existing row)
public boolean updateDiaryEntry(String title, long rowId)
{
ContentValues newValue = new ContentValues();
newValue.put(Constants.TITLE_NAME, title);
return db.update(Constants.TABLE_NAME, newValue, Constants.KEY_ID + "=" + rowId, null)>0;
}
// Reads the diary entries from database, saves them in a Cursor class and returns it from the method
public Cursor getdiaries()
{
Cursor c = db.query(Constants.TABLE_NAME, null, null,
null, null, null, null);
return c;
}
}
這裏是輸出在新建行的文件清單:
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);
}
}
你應該張貼在那裏你創建表的代碼。問題在那裏。 –