我有这个应用程序,它有一个listView,当我点击listView上的一个项目时,它会打开一个新的活动。这很好用!
但是,如果我打开新活动而不是按“后退按钮”应用程序“不幸的是,已停止”。
我有什么问题吗?这是我的代码:
第一项活动:
公共类AndroidSQLite扩展了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(); } }
第二项活动:
公共类DetailsPassword扩展了活动{
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView text = new TextView(getApplicationContext()); text.setText("Text to show"); setContentView(text); } }
// ===== EDITED =====
这是Stack Track
10-30 08:55:05.744:E / AndroidRuntime(28046):致命异常:主要 10-30 08:55:05.744:E / AndroidRuntime(28046): java.lang.RuntimeException:无法恢复活动 {com.example.sqliteexemple2 / com.example.sqliteexemple2.AndroidSQLite}: java.lang.IllegalStateException:尝试重新查询已经关闭的 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):引起:java.lang.IllegalStateException: 试图重新查询已经关闭的游标 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
答案 0 :(得分:-1)
您必须覆盖onKeyUp方法
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
Intent backintntent= new Intent();
intententregas.setClass(ThisActiviti.this, MainActivity.class);
// you can add extra variables
// intent.putExtra("variable", Value);
startActivity(backintntent);
finish();
return true;
}