后退按钮不起作用

时间:2011-12-29 14:11:44

标签: android

enter image description here

当我点击这里mihai我从下面得到活动

enter image description here

问题是,当我在最后一个屏幕上点击后退按钮时,我没有回到搜索活动,但我得到了以下错误:

                   12-29 15:55:47.153: E/AndroidRuntime(408): FATAL EXCEPTION: main
                  12-29 15:55:47.153: E/AndroidRuntime(408): java.lang.RuntimeException: Unable to resume activity {org.example.dbcontactconsole/org.example.dbcontactconsole.Search}: java.lang.IllegalStateException: database /data/data/org.example.dbcontactconsole/databases/contactTest1.db already closed
                  12-29 15:55:47.153: E/AndroidRuntime(408):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3128)
                 12-29 15:55:47.153: E/AndroidRuntime(408):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3143)
                 12-29 15:55:47.153: E/AndroidRuntime(408):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2059)
                 12-29 15:55:47.153: E/AndroidRuntime(408):     at android.os.Handler.dispatchMessage(Handler.java:99)
                  12-29 15:55:47.153: E/AndroidRuntime(408):    at android.os.Looper.loop(Looper.java:123)
                 12-29 15:55:47.153: E/AndroidRuntime(408):     at android.app.ActivityThread.main(ActivityThread.java:4627)
                 12-29 15:55:47.153: E/AndroidRuntime(408):     at java.lang.reflect.Method.invokeNative(Native Method)
                 12-29 15:55:47.153: E/AndroidRuntime(408):     at java.lang.reflect.Method.invoke(Method.java:521)
                 12-29 15:55:47.153: E/AndroidRuntime(408):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
                 12-29 15:55:47.153: E/AndroidRuntime(408):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
                 12-29 15:55:47.153: E/AndroidRuntime(408):     at dalvik.system.NativeStart.main(Native Method)
                 12-29 15:55:47.153: E/AndroidRuntime(408): Caused by: java.lang.IllegalStateException: database /data/data/org.example.dbcontactconsole/databases/contactTest1.db already closed
                 12-29 15:55:47.153: E/AndroidRuntime(408):     at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:237)
                 12-29 15:55:47.153: E/AndroidRuntime(408):     at android.database.sqlite.SQLiteQuery.requery(SQLiteQuery.java:145)
                 12-29 15:55:47.153: E/AndroidRuntime(408):     at android.database.sqlite.SQLiteCursor.requery(SQLiteCursor.java:531)
                 12-29 15:55:47.153: E/AndroidRuntime(408):     at android.app.Activity.performRestart(Activity.java:3795)
                12-29 15:55:47.153: E/AndroidRuntime(408):  at android.app.Activity.performResume(Activity.java:3816)
                12-29 15:55:47.153: E/AndroidRuntime(408):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118)

我的search.java是:

`

                   public class Search extends  ListActivity {

 private static int[] TO = {R.id.name };
private static String[] FROM = {DbConstants.NAME, DbConstants.PHONE, DbConstants.EMAIL,_ID};
private Button sButton;

 private ListView lv1;
 private static SQLiteDatabase db;
  private DbCreate contacts;
  private Cursor cursor;
  private EditText searchText;
  protected SimpleCursorAdapter adapter;


        protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search);
    searchText=(EditText)findViewById(R.id.searchtext);
    sButton=(Button)findViewById(R.id.searchButton);
    sButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            showDatabaseContent();
              lv1 = getListView();

              lv1.setTextFilterEnabled(true);
        }
    });





   }


          private Cursor getContacts() {
      db = contacts.getReadableDatabase();
      cursor = db.rawQuery("SELECT  _id,name, phone, email FROM contactTest1 WHERE name LIKE ?", 
                new String[]{searchText.getText().toString()+"%"});
      startManagingCursor(cursor);
      return cursor;
         }

            public void showDatabaseContent(){
   contacts = new DbCreate(this); 
   try {
       cursor = getContacts(); 
       showContacts(cursor); 
   } finally {
       contacts.close(); 
       db.close();
   }
         }

               private void showContacts(Cursor cursor) {
   //set up data binding
   adapter = new SimpleCursorAdapter(this, R.layout.item, cursor, FROM, TO);
   setListAdapter(adapter);
           }

             public void onListItemClick(ListView parent, View v, int position, long id) {
   Intent abaintent = new Intent(this,Detalii.class);
   Cursor cursor = (Cursor) adapter.getItem(position);
   abaintent.putExtra("Contact_ID", cursor.getInt(cursor.getColumnIndex("_id")));
   startActivity(abaintent);

          }

}

`

这是我的Detalii .java:

          public class Detalii extends ListActivity
           { 
           protected TextView contactName;
           protected TextView contactPhone;
              protected TextView email;
          protected int contactId;
          protected  List<Actiune> actiune;
            protected ActiuneAdapter adapter;
           @Override
            public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
            setContentView(R.layout.detalii);

            contactId = getIntent().getIntExtra("Contact_ID",0);
             SQLiteDatabase db = (new DbCreate(this)).getWritableDatabase();
             Cursor cursor = db.rawQuery("SELECT name,phone,email FROM contactTest1 WHERE _id=?",new String[]{""+contactId});

          if (cursor.getCount() == 1)
    {
    cursor.moveToFirst();

   actiune= new ArrayList<Actiune>();
   String phoneString=cursor.getString(cursor.getColumnIndex("phone"));
   if (phoneString!=null)
   {
    actiune.add(new Actiune("Suna la numar",phoneString,Actiune.ACTION_CALL));
    actiune.add(new Actiune("SMS",phoneString,Actiune.ACTION_SMS));
   }
   String stringemail = cursor.getString(cursor.getColumnIndex("email"));
   if (stringemail != null) {
           actiune.add(new Actiune("Email", stringemail,Actiune.ACTION_EMAIL));
           }
   adapter = new ActiuneAdapter();
   setListAdapter(adapter);
      }
      }
              public void onListItemClick(ListView parent, View view, int position, long id) {

       Actiune action = actiune.get(position);

       Intent intent;
       switch (action.getType()) {

    case Actiune.ACTION_CALL:  
            Uri callUri = Uri.parse("tel:" + action.getData());  
            intent = new Intent(Intent.ACTION_CALL, callUri);
        startActivity(intent);
            break;

    case Actiune.ACTION_EMAIL:  
    intent = new Intent(Intent.ACTION_SEND);
    intent.setType("plain/text");
    intent.putExtra(Intent.EXTRA_EMAIL, new String[]{action.getData()});
    startActivity(intent);
    break;
    case Actiune.ACTION_SMS:  
        Uri smsUri = Uri.parse("sms:" + action.getData());  
        intent = new Intent(Intent.ACTION_VIEW, smsUri); 
        startActivity(intent);
        break;

       }
        }    

          class ActiuneAdapter extends ArrayAdapter<Actiune> {

         ActiuneAdapter() {
            super(Detalii.this, R.layout.actiune_detalii, actiune);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    Actiune action = actiune.get(position);
    LayoutInflater inflater = getLayoutInflater();
    View view = inflater.inflate(R.layout.actiune_detalii, parent, false);
    TextView label = (TextView) view.findViewById(R.id.label);
    label.setText(action.getLabel());
    TextView data = (TextView) view.findViewById(R.id.data);
    data.setText(action.getData());
    return view;
  }

   }

  }

我该怎么办才能让后退按钮工作?我在模拟器上模拟,因为我没有手机

3 个答案:

答案 0 :(得分:0)

尝试将此代码SQLiteDatabase db = (new DbCreate(this)).getWritableDatabase();添加到OnResume活动的Detalii功能。

答案 1 :(得分:0)

错误表明您已关闭与数据库的连接,并且可能在触发恢复功能时尝试向数据库运行问题。

当您重新打开搜索视图时,通过打开与数据库的新连接来解决此问题。

答案 2 :(得分:0)

这很简单。您假设您的数据库已打开,但事实上,当您恢复以前的活动时,它实际上已关闭。可能是因为您的应用程序无法正确处理恢复/暂停事件,或者可能是因为您的数据库变量被声明为static。请尝试在onDestroy()中关闭它。