Application stops on back button in new activity

Posted by Bruno Almeida on Stack Overflow See other posts from Stack Overflow or by Bruno Almeida
Published on 2012-10-30T10:38:41Z Indexed on 2012/10/30 11:02 UTC
Read the original article Hit count: 216

I have this application, that have a listView, and when I click in a item on listView, it opens a new activity. That works fine!

But, if I open the new activity and than press the "back button" the application "Unfortunately, has stopped".

Is there something I'm doing wrong? Here is my code:

First activity:

public class AndroidSQLite extends Activity { private SQLiteAdapter mySQLiteAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ListView listContent = (ListView)findViewById(R.id.contentlist);

    mySQLiteAdapter = new SQLiteAdapter(this);
    mySQLiteAdapter.openToRead();

    Cursor cursor = mySQLiteAdapter.queueAll();
    startManagingCursor(cursor);

    String[] from = new String[]{SQLiteAdapter.KEY_NOME,SQLiteAdapter.KEY_ID};
    int[] to = new int[]{R.id.text,R.id.id};

    SimpleCursorAdapter cursorAdapter =
     new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);

    listContent.setAdapter(cursorAdapter);

    listContent.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {

          Toast.makeText(getBaseContext(), id + "", Toast.LENGTH_LONG).show();

          Intent details = new Intent(getApplicationContext(),DetailsPassword.class);
          startActivity(details);

        }
    });

    mySQLiteAdapter.close();
} }

Second Activity:

public class DetailsPassword extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  TextView text = new TextView(getApplicationContext());
  text.setText("Text to show");
  setContentView(text);
} }

// ===== EDITED =====

here is the Stack Track

10-30 08:55:05.744: E/AndroidRuntime(28046): FATAL EXCEPTION: main 10-30 08:55:05.744: E/AndroidRuntime(28046): java.lang.RuntimeException: Unable to resume activity {com.example.sqliteexemple2/com.example.sqliteexemple2.AndroidSQLite}: java.lang.IllegalStateException: trying to requery an already closed cursor android.database.sqlite.SQLiteCursor@4180a370 10-30 08:55:05.744: E/AndroidRuntime(28046): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2701) 10-30 08:55:05.744: E/AndroidRuntime(28046): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2729) 10-30 08:55:05.744: E/AndroidRuntime(28046): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1250) 10-30 08:55:05.744: E/AndroidRuntime(28046): at android.os.Handler.dispatchMessage(Handler.java:99) 10-30 08:55:05.744: E/AndroidRuntime(28046): at android.os.Looper.loop(Looper.java:137) 10-30 08:55:05.744: E/AndroidRuntime(28046): at android.app.ActivityThread.main(ActivityThread.java:4931) 10-30 08:55:05.744: E/AndroidRuntime(28046): at java.lang.reflect.Method.invokeNative(Native Method) 10-30 08:55:05.744: E/AndroidRuntime(28046): at java.lang.reflect.Method.invoke(Method.java:511) 10-30 08:55:05.744: E/AndroidRuntime(28046): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 10-30 08:55:05.744: E/AndroidRuntime(28046): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558) 10-30 08:55:05.744: E/AndroidRuntime(28046): at dalvik.system.NativeStart.main(Native Method) 10-30 08:55:05.744: E/AndroidRuntime(28046): Caused by: java.lang.IllegalStateException: trying to requery an already closed cursor android.database.sqlite.SQLiteCursor@4180a370 10-30 08:55:05.744: E/AndroidRuntime(28046): at android.app.Activity.performRestart(Activity.java:5051) 10-30 08:55:05.744: E/AndroidRuntime(28046): at android.app.Activity.performResume(Activity.java:5074) 10-30 08:55:05.744: E/AndroidRuntime(28046): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2691) 10-30 08:55:05.744: E/AndroidRuntime(28046): ... 10 more

© Stack Overflow or respective owner

Related posts about android

Related posts about android-activity