2010-07-22 6 views
0

4 개의 탭 (보고서, 리뷰 보고서,지도 및 설정)이 있습니다. 나는 보고서 정보로 채워진 sqlite 데이터베이스를 가지고있다. 애플리케이션 전체에서 데이터베이스에 액세스 할 수 있기를 원합니다. 보고서 탭은 데이터베이스에 데이터를 성공적으로 추가하지만 동일한 방법론을 사용하면 응용 프로그램이 충돌합니다. Android 디버거는 데이터베이스가 다시 호출되는 행을 가리 킵니다. 다음 코드는 데이터베이스를 시작하는 데 사용되는 보고서 탭에서 Android 데이터베이스 간 탭 액세스

은 ... 검토 탭의에서 onCreate() 메소드에서
this.reportDatabase = new ReportDatabase(this); 
    this.reportDatabase.insert("(" + latitude + ", " + longitude + ", " + time + ", '" + spinnerState + "', " + lower + ", " + upper + ", " + agreed + ", " + getAlgorithmCount() + ", " + xAxis + ", " + yAxis + ", " + zAxis + ", " + altitude + ", "+ accuracy + ", 'photo');"); 

- 나는 보고서를 검토하고자하는 - 나는에 액세스하려고 호출을 통해 반환 보고서 메서드를 호출하는 데이터베이스

this.reportDatabase = new ReportDatabase (this);

그러나 이것은 작동하지 않습니다. 안드로이드 디버거에서는 문제가 컨텍스트와 함께 제공된다는 것을 강조 표시합니다. 보고서 탭에서 이미 보고서 데이터베이스에 액세스했으며 이것이 문제의 원인인지 궁금합니다. 프로그래밍 안드로이드에 익숙하지 않은이 응용 프로그램은 아프리카의 플라밍고에 대해보고하도록 고안되었습니다. 어떤 도움이라도 대단히 감사하겠습니다! 다음과 같이 내가 적응 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

우리는 스택 추적을 볼 수 있습니까? Android는 오류를 디버그하고 오류 메시지를 볼 때 까다로울 수 있습니다. 또한 전체 ReportDatabase 클래스입니까? – chrisbunney

+0

데이터베이스를 닫지 않는 것이 문제 일 수 있습니까? –

+0

아마도 데이터베이스 사용을 마치면 데이터베이스 닫기를 시도 했습니까? 29 번과 37 번 라인은 뭐니? – chrisbunney

답변

1

데이터베이스를 여러 번 열려고합니다. 정적 ReportDatabase 메서드를 통해 사용할 수있는 단일 ReportDatabase 개체를 사용합니다. 다음과 같은 내용이 있습니다.

class ReportDatabase 
{ 
    static ReportDatabase TheDatabase = null; 

    static ReportDatabase Open(Context c) 
    { 
     if(TheDatabase == null) 
      TheDatabase = new ReportDatabase(c); 
     return TheDatabase; 
    } 
} 

흔히 싱글 톤이라고합니다. 또는 글로벌 :

+0

나는 이것을 시험해 보았지만 효과가 없다. 더 이상의 제안이 있나? –

+0

앱 전체에서 ReportDatabase.open()을 사용하고 있습니까? 확인할 생성자를 비공개로 만드는 것을 고려하십시오. 거기에 생성자 호출이 있으면 컴파일 오류가 발생합니다. –