2

携帯電話の「戻るボタン」をタッチすると(前のページに戻るため)、アプリで次のエラーがスローされます。

11-06 19:16:56.016: E/AndroidRuntime(24018): java.lang.RuntimeException: Unable to resume activity {app.piotrek.learning/app.piotrek.learning.AddNewQuestion}: java.lang.IllegalStateException: trying to requery an already closed cursor  android.database.sqlite.SQLiteCursor@40f84ab8
11-06 19:16:56.016: E/AndroidRuntime(24018):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2567)
11-06 19:16:56.016: E/AndroidRuntime(24018):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2595)
11-06 19:16:56.016: E/AndroidRuntime(24018):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1183)
11-06 19:16:56.016: E/AndroidRuntime(24018):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-06 19:16:56.016: E/AndroidRuntime(24018):    at android.os.Looper.loop(Looper.java:137)
11-06 19:16:56.016: E/AndroidRuntime(24018):    at android.app.ActivityThread.main(ActivityThread.java:4575)
11-06 19:16:56.016: E/AndroidRuntime(24018):    at java.lang.reflect.Method.invokeNative(Native Method)
11-06 19:16:56.016: E/AndroidRuntime(24018):    at java.lang.reflect.Method.invoke(Method.java:511)
11-06 19:16:56.016: E/AndroidRuntime(24018):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
11-06 19:16:56.016: E/AndroidRuntime(24018):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
11-06 19:16:56.016: E/AndroidRuntime(24018):    at dalvik.system.NativeStart.main(Native Method)
11-06 19:16:56.016: E/AndroidRuntime(24018): Caused by: java.lang.IllegalStateException: trying to requery an already closed cursor  android.database.sqlite.SQLiteCursor@40f84ab8
11-06 19:16:56.016: E/AndroidRuntime(24018):    at android.app.Activity.performRestart(Activity.java:4508)
11-06 19:16:56.016: E/AndroidRuntime(24018):    at android.app.Activity.performResume(Activity.java:4531)
11-06 19:16:56.016: E/AndroidRuntime(24018):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2557)
11-06 19:16:56.016: E/AndroidRuntime(24018):    ... 10 more

修正方法がわかりません。ほぼすべてのカーソルは、onCreate 関数の「setOnClickListener」などの関数にあります。バグがどこにあるのかわかりません。

編集:これは私のコードです:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.Point;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;

public class AddNewQuestion extends Activity {
    Context context;
    Database db;

    Button wczytajobrazek;
    Button addquestion;
    ImageView obrazekdoedycji;
    Bitmap wczytanyobrazek;
    Spinner spinnerSubject;
    Spinner spinnerTitles;
    EditText quest;
    EditText answ;
    Button addprzedmiot;
    Button addtitles;

    Display display;
    Point size;
    int screenWidth;
    int screenHeight;
    int IdOfSubjectfromSpinner;
    int IdOfTitlesfromSpinner;
    Bitmap bmp;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dodajpytania);
        context = getApplicationContext();

        display = getWindowManager().getDefaultDisplay();
        size = new Point();
        screenWidth = display.getWidth();
        screenHeight = display.getHeight();

        quest = (EditText) findViewById(R.id.editText1);
        answ = (EditText) findViewById(R.id.editText2);
        wczytajobrazek = (Button) findViewById(R.id.button1);
        spinnerSubject = (Spinner) findViewById(R.id.spinner1);
        spinnerTitles = (Spinner) findViewById(R.id.spinner2);
        obrazekdoedycji = (ImageView) findViewById(R.id.imageButton1);
        addquestion = (Button) findViewById(R.id.add_question);
        addprzedmiot = (Button) findViewById(R.id.addprzedmiot);
        addtitles = (Button) findViewById(R.id.addTitles);


        db = new Database(getApplicationContext());
        db.open();

        fillSpinner(db.getAllEntriesSubjects(), spinnerSubject, DatabaseData.KEY_NAME);
        long data = spinnerSubject.getSelectedItemId();
        Log.v("ADDNEWQUESTION", "Wybrano: " + data);
        //fillSpinner(db.getAllEntriesTitles(), spinnerTitles);
        db.close();


        wczytajobrazek.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, 0);
            }
        });

        addprzedmiot.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, AddNewSubjectOrTitle.class);
                startActivity(intent);
            }
        });
        addtitles.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, AddNewSubjectOrTitle.class);
                startActivity(intent);
            }
        });

        addquestion.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                String question = quest.getText().toString();
                String answer = answ.getText().toString();
                String img = "abc";
                String desc = "desc";
                Log.v("AddNewQuestion", "Question: " + question + " Answer: " + answer + " Save in (ID): Subject: " + IdOfSubjectfromSpinner + " Titles: " + IdOfTitlesfromSpinner);
                db.open();
                db.insertD(new Questions(question, answer, img, desc, IdOfTitlesfromSpinner).returnData(), DatabaseData.DB_THIRD_TABLE);
                db.close();
                quest.setText("");
                answ.setText("");
            }
        });


        spinnerSubject.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                db.open();
                //String item = parent.getItemAtPosition(pos).toString();
                Cursor c = (Cursor)parent.getItemAtPosition(pos);
                startManagingCursor(c);
                String item = c.getString( c.getColumnIndex( DatabaseData.KEY_NAME ) );
                IdOfSubjectfromSpinner = c.getInt( c.getColumnIndex( DatabaseData.KEY_ID ) );
                Log.v("AddQuestion", "Wybrano: " + item + "("+ c.getInt(c.getColumnIndex(DatabaseData.KEY_ID)) +")");

                fillSpinner(db.getSomeEntriesTitles(c.getInt(c.getColumnIndex(DatabaseData.KEY_ID))), spinnerTitles, DatabaseData.KEY_TITLES_NAME);
                db.close();

            }
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        spinnerTitles.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                Cursor c = (Cursor)parent.getItemAtPosition(pos);
                startManagingCursor(c);

                //String item = c.getString( c.getColumnIndex( DatabaseData.KEY_NAME ) );   
                IdOfTitlesfromSpinner = c.getInt( c.getColumnIndex( DatabaseData.KEY_TITLES_ID ) );
            }
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {
            Uri targetUri = data.getData();
            File newfile = new File(targetUri.toString());
            try {
                wczytanyobrazek = MediaStore.Images.Media.getBitmap(this.getContentResolver(), targetUri);
                wczytanyobrazek = scaleImages(wczytanyobrazek, screenWidth/4, screenWidth/4);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            obrazekdoedycji.setImageBitmap(wczytanyobrazek);
        }
    }

    @Override
    protected void onStart(){
        super.onStart();
    }

    public void fillSpinner(Cursor cs, Spinner spinner, String dane){
        startManagingCursor(cs);

        SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, cs, new String[] {dane}, new int[] {android.R.id.text1});
        mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
        spinner.setAdapter(mAdapter);
    }


    public Bitmap scaleImages(Bitmap qBitmap, float nbwdth, float nbhght){
        float skalujSzer = ((float) nbwdth) / qBitmap.getWidth();
        float skalujWys = ((float) nbhght) / qBitmap.getHeight();
        Matrix matrixMAP1 = new Matrix();
        matrixMAP1.postScale(skalujSzer, skalujWys);

        qBitmap = Bitmap.createBitmap(qBitmap, 0, 0, qBitmap.getWidth(), qBitmap.getHeight(), matrixMAP1, true);

        return qBitmap;
    }

}
4

2 に答える 2

1

これを入れてください:

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();

        cursorObject.requery();

    }
于 2013-06-24T05:51:06.097 に答える
0

ほとんどの場合、メソッドでクエリを実現することが原因であるonCreateため、最初はクエリが正常に実行されてデータが表示されますが、戻ったときにonCreateメソッドが呼び出されず、カーソルが閉じています。クエリをonStartメソッドに移動します。

アクティビティライフサイクルのドキュメントが役立つかもしれません

PS:あなたがあなたのコードを投稿するならあなたを助けるのは簡単でしょう。

于 2012-11-06T18:43:14.097 に答える