{"id":424026,"date":"2021-04-04T02:42:27","date_gmt":"2021-04-03T18:42:27","guid":{"rendered":"http:\/\/4563.org\/?p=424026"},"modified":"2021-04-04T02:42:27","modified_gmt":"2021-04-03T18:42:27","slug":"%e4%bc%9f%e5%a4%a7%e7%9a%84%e7%bd%91%e5%8f%8b%e4%bb%ac%ef%bc%8c%e6%9d%a5%e5%b8%ae%e5%b8%ae%e5%bf%99%e3%80%82%e5%85%b3%e4%ba%8e%e4%bd%bf%e7%94%a8-datastore-%e7%9a%84%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=424026","title":{"rendered":"\u4f1f\u5927\u7684\u7f51\u53cb\u4eec\uff0c\u6765\u5e2e\u5e2e\u5fd9\u3002\u5173\u4e8e\u4f7f\u7528 DataStore \u7684\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u4f1f\u5927\u7684\u7f51\u53cb\u4eec\uff0c\u6765\u5e2e\u5e2e\u5fd9\u3002\u5173\u4e8e\u4f7f\u7528 DataStore \u7684\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : CongrooPsyEl <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u6700\u8fd1\u521a\u597d\u60f3\u8bd5\u4e00\u4e0b DataStore\uff0c\u8bd5\u7740\u5199\u4e86\u4e2a\u66f4\u6362\u8bed\u8a00\u7684\u529f\u80fd\u3002<\/p>\n<pre><code> open class BaseActivity : AppCompatActivity() {       override fun attachBaseContext(newBase: Context) {         lifecycleScope.launch(Dispatchers.Main) {             var context = newBase             val language = withContext(Dispatchers.IO) {                 DataStore.read(context, DataStore.KEY_LANG)             }             context = Language.changeLanguage(context, language ?:              Language.ZH)             super.attachBaseContext(context)         }     } } <\/code><\/pre>\n<p>\u4e4b\u524d\u90fd\u662f\u5728\u8fd9\u7528 sp \u62ff\u51fa\u5b58\u50a8\u7684\u952e\u503c\u5bf9\u7684\uff0c\u73b0\u5728\u6362\u4e86 DataStore \u4e4b\u540e\u4e0d\u5f97\u4e0d\u7528\u534f\u7a0b\u6240\u4ee5\u5c31\u534f\u7a0b\u4e0a\u9762\u7684\u6837\u5b50\u4e86\u3002<\/p>\n<p>\u7136\u540e\u5982\u679c\u8fd9\u6837\u5199\u7684\u8bdd\u662f\u4f1a\u62a5\u8fd9\u6837\u7684\u9519\uff1a<\/p>\n<pre><code>java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference <\/code><\/pre>\n<p>\u6253\u4e86\u65ad\u70b9\u770b\u4e86\u4e0b\uff0c\u4ee3\u7801\u8d70\u4e0d\u5230 super.attachBaseContext(context)\u3002 \u4e07\u80fd\u7684\u7f51\u53cb\u840c\uff0c\u6559\u6559\u840c\u65b0\u5427~<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>8<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5629632\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Still4 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5144\u5f1f\u4f60\u8fd9\u4ee3\u7801\u8d34\u4e00\u534a\u554a&#8230;DataStore \u7528\u7684\u54ea\u4e2a\u7248\u672c\uff0cLanguage \u662f\u4ec0\u4e48\u5305<\/p>\n<p>\u4f60\u8bd5\u8bd5\u81ea\u5df1\u65b0\u8d77\u4e00\u4e2a\u5de5\u7a0b\uff0c\u8d34\u4e0a\u8fd9\u6bb5\u4ee3\u7801\u80fd\u901a\u8fc7\u7f16\u8bd1\u4e48&#8230;                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5629633\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ikas <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             lifecycleScope.launch \u4e4b\u540e\u76f4\u63a5\u8fd4\u56de\u4e86&#8230;                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5629634\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ikas <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7136\u540e\u5f53\u7136\u8d70\u4e0d\u5230 super.attachBaseContext(context),\u6240\u4ee5 context \u662f null                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5629635\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : fromzero <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             lifecycleScope \u751f\u547d\u5468\u671f\u95ee\u9898                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5629636\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ikas <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             attachBaseContext \u8fd9\u4e2a\u65b9\u6cd5\u4f60\u8981\u4fdd\u8bc1 super \u8fd9\u4e2a\u8c03\u7528\u4e4b\u540e,\u624d\u80fd\u8fd4\u56de                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5629637\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : fromzero <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @fromzero \u770b\u9519\u4e86\u3002\u6211\u4e5f\u4e0d\u4e0d\u77e5\u9053                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5629638\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hstan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             mmkv \u4e0d\u9999\u5417                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5629639\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Still4 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             class MyFragment: Fragment() {<br \/> override fun onViewCreated(view: View, savedInstanceState: Bundle?) {<br \/> super.onViewCreated(view, savedInstanceState)<br \/> viewLifecycleOwner.lifecycleScope.launch {<br \/>  val params = TextViewCompat.getTextMetricsParams(textView)<br \/> val precomputedText = withContext(Dispatchers.Default) {<br \/> PrecomputedTextCompat.create(longTextContent, params)<br \/> }<br \/> TextViewCompat.setPrecomputedText(textView, precomputedText)<br \/> }<br \/> }<br \/>}<\/p>\n<p>lifecycleScope \u5b98\u65b9\u7528\u6cd5\u662f\u8fd9\u6837\u7684\uff0c\u6211\u7406\u89e3\u662f lifecycleScope.launch(Dispatchers.Main)\u8fd9\u91cc\u5c31\u6709\u95ee\u9898                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u4f1f\u5927\u7684\u7f51\u53cb\u4eec\uff0c\u6765\u5e2e\u5e2e\u5fd9\u3002\u5173\u4e8e\u4f7f\u7528 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/424026"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=424026"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/424026\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=424026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=424026"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=424026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}