首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TabActivity未恢复

TabActivity未恢复
EN

Stack Overflow用户
提问于 2014-11-01 12:30:38
回答 2查看 60关注 0票数 0

我正在开发一个应用程序,在这个应用程序中,我在扩展选项卡活动的类下创建了一个数据库。但是,当我的选项卡活动恢复时,我的应用程序就会崩溃。任何帮助都将不胜感激。

这里是我的代码

代码语言:javascript
复制
public class MainActivity extends TabActivity {
    /** Called when the activity is first created. */
    public static   boolean paid=false;
    public String num;
    public String num1;
    public String  name;
    static int delay;
    public static Boolean fromapp=false;
    SQLiteDatabase db;
    TabHost tabHost;

    @SuppressWarnings("deprecation")
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);



    Toast.makeText(getApplicationContext(), String.valueOf(fromapp), Toast.LENGTH_SHORT).show();    
        Resources res = getResources(); // Resource object to get Drawables
        tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the tab (to be reused)

      /*  
        intent = new Intent().setClass(this, DialPad.class);
        spec = tabHost.newTabSpec("DIAL PAD").setIndicator("DIAL PAD",res.getDrawable(R.drawable.ic_launcher)).setContent(intent);
        tabHost.addTab(spec);*/


        intent = new Intent().setClass(this, Calllog.class);
        spec = tabHost.newTabSpec("CALL LOG").setIndicator("CALL LOG",res.getDrawable(R.drawable.ic_launcher)).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, Contacts.class);
        spec = tabHost.newTabSpec("CONTACTS").setIndicator("CONTACTS",res.getDrawable(R.drawable.ic_launcher)).setContent(intent);
        tabHost.addTab(spec);


        db=openOrCreateDatabase("Mydb", MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS Fromapp(delay INTEGER);");

        Cursor c=db.rawQuery("select * from Fromapp", null);


        if(c.moveToFirst())
        {
        }
        else
        {
            db.execSQL("insert into Fromapp values("+5000+")");
        }



 }





    @Override
     public boolean onOptionsItemSelected(MenuItem item)
     {

            switch (item.getItemId())
            {
            case R.id.ten:
                db.execSQL("update Fromapp set delay="+10000);


                return true;
            case R.id.twenty:
                db.execSQL("update Fromapp set delay="+20000);


                return true;
            case R.id.fifty:
                db.execSQL("update Fromapp set delay="+30000);


                return true;    
            case R.id.hundred:
                db.execSQL("update Fromapp set delay="+40000);
                return true;
            case R.id.twohundred:
                db.execSQL("update Fromapp set delay="+50000);
                return true;
            case R.id.fivehundred:
                db.execSQL("update Fromapp set delay="+60000);
                return true;    

            default:

                return super.onOptionsItemSelected(item);
            }
        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();       
    }

My LogCat显示了下面的

代码语言:javascript
复制
11-01 12:28:49.513: E/AndroidRuntime(32547): FATAL EXCEPTION: main
11-01 12:28:49.513: E/AndroidRuntime(32547): java.lang.RuntimeException: Unable to resume activity {com.thoughtrix.tryamisscall/com.thoughtrix.tryamisscall.MainActivity}: android.database.StaleDataException: Attempted to access a cursor after it has been closed.
11-01 12:28:49.513: E/AndroidRuntime(32547):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2742)
11-01 12:28:49.513: E/AndroidRuntime(32547):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2771)
11-01 12:28:49.513: E/AndroidRuntime(32547):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1276)
11-01 12:28:49.513: E/AndroidRuntime(32547):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-01 12:28:49.513: E/AndroidRuntime(32547):    at android.os.Looper.loop(Looper.java:137)
11-01 12:28:49.513: E/AndroidRuntime(32547):    at android.app.ActivityThread.main(ActivityThread.java:5041)
11-01 12:28:49.513: E/AndroidRuntime(32547):    at java.lang.reflect.Method.invokeNative(Native Method)
11-01 12:28:49.513: E/AndroidRuntime(32547):    at java.lang.reflect.Method.invoke(Method.java:511)
11-01 12:28:49.513: E/AndroidRuntime(32547):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-01 12:28:49.513: E/AndroidRuntime(32547):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-01 12:28:49.513: E/AndroidRuntime(32547):    at dalvik.system.NativeStart.main(Native Method)
11-01 12:28:49.513: E/AndroidRuntime(32547): Caused by: android.database.StaleDataException: Attempted to access a cursor after it has been closed.
11-01 12:28:49.513: E/AndroidRuntime(32547):    at android.database.BulkCursorToCursorAdaptor.throwIfCursorIsClosed(BulkCursorToCursorAdaptor.java:64)
11-01 12:28:49.513: E/AndroidRuntime(32547):    at android.database.BulkCursorToCursorAdaptor.requery(BulkCursorToCursorAdaptor.java:133)
11-01 12:28:49.513: E/AndroidRuntime(32547):    at android.database.CursorWrapper.requery(CursorWrapper.java:186)
11-01 12:28:49.513: E/AndroidRuntime(32547):    at android.app.Activity.performRestart(Activity.java:5148)
11-01 12:28:49.513: E/AndroidRuntime(32547):    at android.app.ActivityThread.performRestartActivity(ActivityThread.java:3214)
11-01 12:28:49.513: E/AndroidRuntime(32547):    at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:168)
11-01 12:28:49.513: E/AndroidRuntime(32547):    at android.app.LocalActivityManager.dispatchResume(LocalActivityManager.java:523)
11-01 12:28:49.513: E/AndroidRuntime(32547):    at android.app.ActivityGroup.onResume(ActivityGroup.java:61)
11-01 12:28:49.513: E/AndroidRuntime(32547):    at com.thoughtrix.tryamisscall.MainActivity.onResume(MainActivity.java:134)
11-01 12:28:49.513: E/AndroidRuntime(32547):    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1185)
11-01 12:28:49.513: E/AndroidRuntime(32547):    at android.app.Activity.performResume(Activity.java:5182)
11-01 12:28:49.513: E/AndroidRuntime(32547):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2732)
11-01 12:28:49.513: E/AndroidRuntime(32547):    ... 10 more
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-11-04 08:08:55

我最终找到了答案。这个异常经常会弹出,因为我的calllog类中有"managedQuery“。我过去常常在使用managedQuery检索数据之后显式地关闭游标。绝不能这样做。当光标完成时,您的活动将自动关闭它。

票数 0
EN

Stack Overflow用户

发布于 2014-11-01 12:35:47

试着打开数据库

代码语言:javascript
复制
db.getWritableDatabase();
db.open();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26689327

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档