我有四个标签(报告,评论报告,地图和设置)。我有一个填充了报告信息的sqlite数据库。我希望整个应用程序都可以访问数据库。 “报告”选项卡成功地将数据添加到数据库,但是使用相同的方法会使应用程序崩溃。 Android调试器指向再次调用数据库的行。
在“报告”标签中,以下代码用于启动数据库...
this.reportDatabase = new ReportDatabase(this);
this.reportDatabase.insert("(" + latitude + ", " + longitude + ", " + time + ", '" + spinnerState + "', " + lower + ", " + upper + ", " + agreed + ", " + getAlgorithmCount() + ", " + xAxis + ", " + yAxis + ", " + zAxis + ", " + altitude + ", "+ accuracy + ", 'photo');");
在Review选项卡的onCreate()方法中 - 我希望查看报告 - 我尝试通过调用返回报告方法的访问来访问数据库
this.reportDatabase = new ReportDatabase(this);
然而,这无效。在android调试器中突出显示问题属于提供给它的上下文。我意识到报告选项卡已经访问过报告数据库,并想知道这是否是导致问题的原因。我是编程android的新手,该应用程序旨在报告非洲的火烈鸟,任何帮助将不胜感激!
根据Seva Alekseyev的建议,我改编如下......
我已将ReportDatabase改编为......
public ReportDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
ReportDatabase.context = context;
OpenHelper openHelper = new OpenHelper(ReportDatabase.context);
this.database = openHelper.getWritableDatabase();
}
static ReportDatabase open(Context c){
if(reportDatabase == null){
reportDatabase = new ReportDatabase(ReportDatabase.context);
return reportDatabase;
}
return reportDatabase;
}
...使用
reportDatabase = ReportDatabase.open(this);
作为报告和审核标签中的通话。不幸的是,这似乎不起作用,调试器停止在相同的方法上。完整的ReportDatabase.java文件在这里......
package com.android.flamingo;
import java.util.Vector;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class ReportDatabase extends SQLiteOpenHelper {
private static Context context;
private SQLiteDatabase database;
static ReportDatabase reportDatabase;
private static final String DATABASE_NAME = "flamingo_reports";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "reports";
/**
* Default (and only) constructor....
*
* @param context
*/
public ReportDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
ReportDatabase.context = context;
OpenHelper openHelper = new OpenHelper(ReportDatabase.context);
this.database = openHelper.getWritableDatabase();
}
static ReportDatabase open(Context c){
if(reportDatabase == null){
reportDatabase = new ReportDatabase(ReportDatabase.context);
return reportDatabase;
}
return reportDatabase;
}
/**
*
* @param name
*/
public void insert(String name){
this.database.execSQL("INSERT INTO " + TABLE_NAME + "(latitude, longitude, time, lake, lower_estimate, higher_estimate, agreed_estimate, algorithm_count, xaxis, yaxis, zaxis, altitude, accuracy, photo_identifier) VALUES " + name);
}
/**
* This method returns a double array and probably shouldn't be this hacky...
*
*
* @return
*/
public Vector<ReportInstanceQuery> reportSelect(){
Vector<ReportInstanceQuery> tempReports = new Vector<ReportInstanceQuery>();
Cursor c = database.rawQuery("SELECT id,time,lake,lower_estimate,higher_estimate,agreed_estimate,algorithm_count FROM" + TABLE_NAME + ";",null);
int indexTime = c.getColumnIndex("time");
int indexLake = c.getColumnIndex("lake");
int indexLowerEstimate = c.getColumnIndex("lower_estimate");
int indexHigherEstimate = c.getColumnIndex("higher_estimate");
int indexAgreedEstimate = c.getColumnIndex("agreed_estimate");
int indexAlgorithmCount = c.getColumnIndex("algorithm_count");
if (c != null){
int i = 0;
do {
i++;
int columnTime = c.getInt(indexTime);
String columnLake = c.getString(indexLake);
int columnLowerEstimate = c.getInt(indexLowerEstimate);
int columnHigherEstimate = c.getInt(indexHigherEstimate);
int columnAgreedEstimate = c.getInt(indexAgreedEstimate);
int columnAlgorithmCount = c.getInt(indexAlgorithmCount);
tempReports.add(new ReportInstanceQuery(columnTime, columnLake, columnLowerEstimate, columnHigherEstimate, columnAgreedEstimate, columnAlgorithmCount));
} while (c.moveToNext());
}
reportDatabase.close();
return tempReports;
}
/**
* This method connects to the database
*
*/
public void CSVReportSelect(){
}
public void delete(){
this.database.delete(TABLE_NAME, null, null);
}
@Override
public void onCreate(SQLiteDatabase database) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
private static class OpenHelper extends SQLiteOpenHelper {
OpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY AUTOINCREMENT, latitude REAL, longitude REAL, time INTEGER, lake TEXT, lower_estimate INTEGER, higher_estimate INTEGER, agreed_estimate INTEGER, algorithm_count INTEGER, xaxis REAL, yaxis REAL, zaxis REAL, altitude REAL, accuracy REAL, photo_identifier TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w("Example", "Upgrading database, this will drop tables and recreate.");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
}
错误堆栈是......
ReportDatabase.<init>(Context) line: 29
ReportDatabase.open(Context) line: 37
ReviewTab.onCreate(Bundle) line: 26
Instrumentation.callActivityOnCreate(Activity, Bundle) line: 1123
ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord) line: 2231
ActivityThread.startActivityNow(Activity, String, Intent, ActivityInfo, IBinder, Bundle, Object) line: 2112
LocalActivityManager.moveToState(LocalActivityManager$LocalActivityRecord, int) line: 130
LocalActivityManager.startActivity(String, Intent) line: 342
TabHost$IntentContentStrategy.getContentView() line: 600
TabHost.setCurrentTab(int) line: 310
TabHost$2.onTabSelectionChanged(int, boolean) line: 126
TabWidget$TabClickListener.onClick(View) line: 268
RelativeLayout(View).performClick() line: 2183
RelativeLayout(View).onTouchEvent(MotionEvent) line: 3849
RelativeLayout(View).dispatchTouchEvent(MotionEvent) line: 3389
RelativeLayout(ViewGroup).dispatchTouchEvent(MotionEvent) line: 831
TabWidget(ViewGroup).dispatchTouchEvent(MotionEvent) line: 863
LinearLayout(ViewGroup).dispatchTouchEvent(MotionEvent) line: 863
TabHost(ViewGroup).dispatchTouchEvent(MotionEvent) line: 863
FrameLayout(ViewGroup).dispatchTouchEvent(MotionEvent) line: 863
LinearLayout(ViewGroup).dispatchTouchEvent(MotionEvent) line: 863
PhoneWindow$DecorView(ViewGroup).dispatchTouchEvent(MotionEvent) line: 863
PhoneWindow$DecorView.superDispatchTouchEvent(MotionEvent) line: 1707
PhoneWindow.superDispatchTouchEvent(MotionEvent) line: 1197
HelloFlamingos(Activity).dispatchTouchEvent(MotionEvent) line: 1993
PhoneWindow$DecorView.dispatchTouchEvent(MotionEvent) line: 1691
ViewRoot.handleMessage(Message) line: 1525
ViewRoot(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 123
ActivityThread.main(String[]) line: 3948
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 782
ZygoteInit.main(String[]) line: 540
NativeStart.main(String[]) line: not available [native method]
想法?
答案 0 :(得分:1)
您正尝试多次打开数据库。使用单个ReportDatabase对象,可通过静态方法ReportDatabase获得。像这样:
class ReportDatabase
{
static ReportDatabase TheDatabase = null;
static ReportDatabase Open(Context c)
{
if(TheDatabase == null)
TheDatabase = new ReportDatabase(c);
return TheDatabase;
}
}
这通常被称为单身人士。或全球:)