2012-12-17 2 views
1

내 앱은 여러 가지 활동으로 구성되어 있습니다. 지금까지 활동간에 이동하는 동안 데이터베이스에 액세스하는 데 아무런 문제가 없었습니다. 그러나 마지막 listActivity (LocationActivity)에는 각 listView 항목에 포함 된 단추가 있습니다.활동간에 전환하는 동안 SQLite 데이터베이스에 액세스 할 때 예외가 발생했습니다.

이 버튼 중 하나를 클릭하면 사용자가 specView를 해당 listView 항목 (손상된 구성 요소)의 일부 EditText 필드에 입력하는 경우 SpecificationEdit.java로 이동하지만 저장을 클릭하면 다음 오류로 인해 충돌합니다 메시지 (데이터를 주 데이터베이스 확인에 저장됩니다) : 여기

java.lang.RuntimeException: Unable to resume activity blah blah 
Exception: trying to requery an already closed cursor blah blah 

는 listActivity 클래스입니다 :

public class LocationActivity extends ListActivity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_location); 
    setLongClick(); 

    rmDbHelper = new RMDbAdapter(this); 
    rmDbHelper.open(); 
    getIntents(); 
    setUpViews(); 
    setAdapter();    
    setTextChangedListeners(); 
} 

protected void onResume(){ 
    super.onResume(); 
    final Cursor locationCursor = (Cursor) rmDbHelper.fetchLocationsForRun(runId); 
    startManagingCursor(locationCursor); 
    locationCursorSize = locationCursor.getCount(); 
    setAdapter(); 
    setTextChangedListeners(); 
} 

그리고 여기이 활동의 ​​비트가 SpecificationEdit.java

당신을 보낸다 당신은 저장 버튼을 클릭하면 발사 코드의 비트와

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    rmDbHelper = new RMDbAdapter(this); 
    rmDbHelper.open(); 
    Intent i = getIntent(); 
    inspectionId = i.getLongExtra("Intent_InspectionID", -1); 
    areaId = i.getLongExtra("Intent_AreaID", -1); 
    runId = i.getLongExtra("Intent_RunID", -1); 
    locationId = i.getLongExtra("Intent_LocationID", -1); 
    damagedComponentId = i.getLongExtra("Intent_DamagedComponentID", -1); 
    setContentView(R.layout.edit_specification); 
    setUpViews(); 
    populateFields(); 
    fillSpinner(); 
    setListeners(); 
} 

: 마지막으로

protected void saveDamagedComponentSpec() { 

    String manufacturer = ((Cursor)manufacturerSpinner.getSelectedItem()).getString(1).toString(); 
    String text1 = specEditText1.getText().toString(); 
    String text2 = specEditText2.getText().toString(); 
    String text3 = specEditText3.getText().toString(); 
    String text4 = specEditText4.getText().toString(); 
    String notes_spec = specEditTextNotes.getText().toString(); 

    rmDbHelper.saveDamagedComponentSpec(damagedComponentId, manufacturer, text1, text2, text3, text4, notes_spec);  

    if ("Yes".equals(specSaved)){ 
     Toast.makeText(getApplicationContext(), "Component specification updated", 
      Toast.LENGTH_SHORT).show(); 
    } 

    else { 
     Toast.makeText(getApplicationContext(), "Component specification added", 
       Toast.LENGTH_SHORT).show(); 

    } 

    finish(); 
} 

을, 여기에 코드입니다 16,

private void startComponentEdit() { 
    Intent i = new Intent(LocationActivity.this, SpecificationEdit.class); 
    i.putExtra("Intent_InspectionID", inspectionId); 
    i.putExtra("Intent_AreaID", areaId); 
    i.putExtra("Intent_RunID", runId); 
    i.putExtra("Intent_LocationID", locationId); 
    i.putExtra("Intent_Ref", locationRef); 
    i.putExtra("Intent_DamagedComponentID", damagedComponentId); 
    startActivityForResult(i, ACTIVITY_CREATE); 

} 

그리고 여기가 SpecificationEdit.java의 한 OnCreate입니다 데이터베이스 도우미 클래스에서 :

//Constructor - takes the context to allow the database to be opened/created 
public RMDbAdapter(Context ctx) { 
    this.mCtx = ctx; 
} 

/** 
* Open the rm database. If it cannot be opened, try to create a new 
* instance of the database. If it cannot be created, throw an exception to 
* signal the failure 
* 
* @return this (self reference, allowing this to be chained in an 
*   Initialisation call) 
* @throws SQLException if the database could be neither opened or created 
*/ 
public RMDbAdapter open() throws SQLException { 
    rmDbHelper = new DatabaseHelper(mCtx); 
    rmDb = rmDbHelper.getWritableDatabase(); 
    return this; 
} 


public void close() { 
    rmDbHelper.close(); 
} 

새 항목은 listView 항목 (포함 된 항목이 아닌 실제 항목) 또는 '새 구성 요소 추가'버튼 중 하나를 클릭하면 매우 유사한 인터페이스로 다른 활동 인 ComponentEdit.java로 이동합니다 (구성 요소를 추가하기 위해 목록으로)을 SpecificationEdit으로 설정하지만 완료되면 앱을 다운시키지 않습니다.

두 활동간에 큰 차이는 보이지 않지만 LocationActivity로 돌아 왔을 때이 오류로 인해 충돌이 발생합니다.

나는 방금 onResume을 제거해 보았습니다. 그리고 이것은 아무런 차이가 없었습니다. 이것으로 벽돌 벽을 치면 나에게 너트가 몰리게됩니다.

내 에뮬레이터에서 정상적으로 작동한다고 추가해야하지만 휴대 전화 (HTC One S)에서 테스트 할 때 충돌이 발생합니다. 아주 이상해 ..

+0

수동으로 locationCursor를 닫으시겠습니까? – nandeesh

+0

빠른 응답 nandeesh을 보내 주셔서 감사합니다. 코드를 업데이트했지만 아무런 차이가 없습니다. – Scamparelli

+0

startManagingCursor를 사용하는 경우 절대 커서를 닫지 말고 작업을 수행해야합니다.너를 위해 그것의 배려 – nandeesh

답변

0

마우스 오른쪽을 시작, 문제 (명백한 실수를 발견)를 발견),하지만 SpecifcationEdit.class를 완료하면이 활동으로 돌아가서 componentsCursor를 닫으려고 시도하지만 분명히 실패합니다.

어리석은 일은 실제로이 커서가 아직 아무것도하지 못했다는 것입니다! 도!

단지 몇 가지 추가 조언/whittering; 내 응용 프로그램은 기본적으로 startActivityForResult를 사용하지 않기 때문에 Google 노트 패드 예제와 근본적으로 다르다. (실제로 지금은 startActivity만으로이 모든 것을 바꿀 것이다.) 편집 활동에 대한 정보를 의도적으로 전달하고 이전 활동으로 돌아 왔을 때 추가 정보를 입력하지 말고

이 코드는 내 논리적 영역에서 더 논리적이지만이 접근법에 대한 피드백은 무엇입니까?

0

rmDbHelper.close();를 호출하는 것을 잊지 마세요.) (

Cursor componentsCursor = (Cursor) rmDbHelper.fetchDamagedComponentSpecForInspection(inspectionId, componentType); 
    startManagingCursor(componentsCursor); 

    Intent i = new Intent(this, SpecificationEdit.class); 
    i.putExtra("Intent_InspectionID", inspectionId); 
    i.putExtra("Intent_AreaID", areaId); 
    i.putExtra("Intent_RunID", runId); 
    i.putExtra("Intent_LocationID", locationId); 
    i.putExtra("Intent_Ref", locationRef); 
    i.putExtra("Intent_DamagedComponentID", damagedComponentId); 
    startActivityForResult(i, ACTIVITY_CREATE); 

    componentsCursor.close(); 

그래서는 componentsCursor.close 전에 왼쪽이 명백한 (나는 약간 밖으로 차단했다 코드 아니었다 : 전에 다른 활동

+0

답장을 보내 주셔서 감사합니다.하지만, 비록 내가 SpecificationEdit.java를 끝내고 LocationActivity로 돌아 오기 전에 데이터베이스를 닫더라도 아무런 차이가 없습니다. 그러나 나는 이전 활동을 떠날 때 그것을 닫지 않았고 하나의 활동에서 돌아 왔지만 다른 활동에서는 돌아 오지 않을 때 괜찮습니다. – Scamparelli

+0

startComponentEdit()에 rmDbHelper.close를 추가하려고 시도 했습니까? 그리고 SpecificationEdit의 onDestroy에서? – letroll

관련 문제