活动意图SerializableExtra变为null,有什么想法吗?

时间:2019-01-02 12:30:08

标签: android android-intent android-activity kotlin

首先,此崩溃是在用户设备上重现一次,因此不会拼写错误,也不会轻易发现错误。 所以我的代码看起来像这样:

class TradingPositionDetailsActivity : BaseToolbarActivity() {
...
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    ...
    tradingPositions =
        intent.getSerializableExtra(TRADING_POSITIONS) as List<TradingPosition>
    ...
}

...

companion object {

    private const val SELECTED_POSITION = "selected_position"
    private const val TRADING_POSITIONS = "trading_positions"

    fun startActivity(
        activity: Activity,
        data: List<TradingPosition>,
        tradingPosition: TradingPosition
    ) {
        val intent = Intent(activity, TradingPositionDetailsActivity::class.java)
        intent.putExtra(SELECTED_POSITION, tradingPosition)
        intent.putExtra(TRADING_POSITIONS, data as Serializable)

        val activityOptionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(activity)
        activity.startActivity(intent, activityOptionsCompat.toBundle())
    }
}

}

所以 Kotlin确保在startActivity方法中传递非null值,但是当我尝试从意图中获得更多收益时,会抛出

Caused by kotlin.TypeCastException: null cannot be cast to non-null type kotlin.collections.List<com.myapp.business.models.TradingPosition>
   at com.myapp.activities.TradingPositionDetailsActivity.onCreate(SourceFile:98)
   at android.app.Activity.performCreate(Activity.java:7144)
   at android.app.Activity.performCreate(Activity.java:7135)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2894)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049)
   at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
   at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
   at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809)
   at android.os.Handler.dispatchMessage(Handler.java:106)
   at android.os.Looper.loop(Looper.java:193)
   at android.app.ActivityThread.main(ActivityThread.java:6680)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

0 个答案:

没有答案