加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Fragment和Sqlite问题

发布时间:2020-12-12 20:09:50 所属栏目:百科 来源:网络整理
导读:Fragment之 IllegalStateException: Can not perform this action after onSaveInstanceState: 今天使用Fragment的时候,出现了这个错误 IllegalStateException: Can not perform this action after onSaveInstanceState: E/AndroidRuntime(12747): Caused

Fragment之 IllegalStateException: Can not perform this action after onSaveInstanceState:

今天使用Fragment的时候,出现了这个错误 IllegalStateException: Can not perform this action after onSaveInstanceState:

E/AndroidRuntime(12747): Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1314)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1325)


使用FragmentTransition的 commit方法添加一个Fragment的时候出现的,后来发现官方的API已经对commit()方法做了说明。

其中有这么一句:

Schedules a commit of this transaction. The commit does not happen immediately; it will be scheduled as work on the main thread to be done the next time that thread is ready.

A transaction can only be committed with this method prior to its containing activity saving its state. If the commit is attempted after that point,an exception will be thrown. This is because the state after the commit can be lost if the activity needs to be restored from its state. See commitAllowingStateLoss() for situations where it may be okay to lose the commit.



参见:http://developer.android.com/reference/android/app/FragmentTransaction.html#commit()

意思是说 commit方法是在Activity的onSaveInstanceState()之后调用的,这样会出错,因为onSaveInstanceState方法是在该Activity即将被销毁前调用,来保存Activity数据的,如果在保存状态后再给它添加Fragment就会出错。如果允许丢失提交的话,可以把commit()方法替换成 commitAllowingStateLoss()就行了。

SQLITE时间戳与系统时间不符

发现sqlite的自动生成的时间戳总是与系统时间不符,分析了下,肯定是sqlite使用的时间区域不对,找了找建表的sql语句,发现果然是sql语句有问题。 如果想在sqlite3下创建的表里插入数据的时候也把插入数据的时间点存储在sqlite数据库里怎么处理呢? 默认的使用时间戳的方式是:[CreatedTime] TimeStamp NOT NULL DEFAULT CURRENT_TIMESTAMP) 或者[CreatedTime] TimeStamp NOT NULL DEFAULT (datetime('now')) 这时会发现这个时间并不是系统的时间,而世界时间,怎么处理呢? 改为: [CreatedTime] TimeStamp NOT NULL DEFAULT (datetime('now','localtime'))); 再次插入数据就和系统的本地时间对应了。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读