2017-08-24 199 views
0

我只是試圖實現onActivityReenter在重新輸入轉換運行之前執行特殊的UI更新。我硬是把塊從onActivityResult請求代碼,並將其分成2個部分,一部分進入onActivityReenter,對方呆在那裏:解組意外拋出異常拋出異常,但只在onActivityReenter期間

BEFORE: (onActivityResult) 
      if (resultCode == RESULT_OK) { 
       // replace the MatchPlaySubmit model 
       data?.let { 
        this.dataBinding.submitModel = data.getMatchPlaySubmitModel() 
        val profIndex = data.getMatchPlayFinalProfileIndex() 
        dataBinding.matchPlay.matchPlayPicker.jumpToProfileNumber(profIndex + 1) 
        this.updatePickerView() 
       } 
      } 


override fun onActivityReenter(resultCode: Int, data: Intent?) { 
    if (resultCode == RESULT_OK && data != null) { 
     // makes sure that the data is from pager, since there no request code 
>  if (data.getStringExtra("source") == "mppager") { // causes CRASH 
      val profIndex = data.getMatchPlayFinalProfileIndex() 
      dataBinding.matchPlay.matchPlayPicker.jumpToProfileNumber(profIndex + 1) 
     } 
    } 
} 

AFTER: (onActivityResult) 
      if (resultCode == RESULT_OK) { 
       // replace the MatchPlaySubmit model 
       data?.let { 
        this.dataBinding.submitModel = data.getMatchPlaySubmitModel() 
        this.updatePickerView() 
       } 
      } 

然而,新的代碼onActivityReenter總是會導致崩潰。崩潰就好像我沒有正確設置類加載器的額外功能。但是,我只是添加到kotlin擴展func以確保extras的classloader被設置爲我的。然而,崩潰總是會發生。

inline internal fun Intent.setMatchPlaySubmitModel(model: MatchPlaySubmitModel?) { 
    this.putExtra("@[email protected]", model) 
    this.setExtrasClassLoader(MatchPlaySubmitModel::class.java.classLoader) 
} 

android.os.BadParcelableException:ClassNotFoundException的時 解組:letstwinkle.com.twinkle.api.MatchPlaySubmitModel 在android.os.Parcel.readParcelableCreator(Parcel.java:2535) 在android.os .Parcel.readParcelable(Parcel.java:2461) at android.os.Parcel.readValue(Parcel.java:2364) at android.os.Parcel.readArrayMapInternal(Parcel.java:2717) at android.os.BaseBundle .unparcel(BaseBundle.java:269) at android.os.BaseBundle.getString(Bas eBundle.java:992) 在android.content.Intent.getStringExtra(Intent.java:6211) 在letstwinkle.com.twinkle.MatchPlayActivity.onActivityReenter(MatchPlayActivity.kt:1123)

測試API 24

回答

0

看起來,框架轉換實現有一個糟糕的情況。目前沒有「好」的解決方案。可能的解決方法是避免將您的應用程序定義的類放入結果意圖的任何方法。我選擇的方式是針對那個類(幸運的是隻有一個),刪除Parcelable實現並從Intent和writeToIntent方法提供構造函數:

constructor(intent: Intent) { 
    playID = intent.getStringExtra("@[email protected]") 
    chosenProfileID = intent.getStringExtra("@[email protected]") 
    extraChosenProfileID = intent.getStringExtra("@[email protected]") 
    passed = intent.getBooleanExtra("@[email protected]", false) 
    shout = intent.getStringExtra("@[email protected]") 
    extraShout = intent.getStringExtra("@[email protected]") 
} 
fun writeToIntent(intent: Intent) { 
    intent.putExtra("@[email protected]", playID) 
    intent.putExtra("@[email protected]", chosenProfileID) 
    intent.putExtra("@[email protected]", extraChosenProfileID) 
    intent.putExtra("@[email protected]", passed) 
    intent.putExtra("@[email protected]", shout) 
    intent.putExtra("@[email protected]", extraShout) 
}