我正在尝试实现一个“调查”类型的应用程序,该应用程序需要在活动之间传递数据,以便最终可以将其写入Excel文档,因此我需要将具有不同数据类型的实例化类传递给新活动,因此我可以收集更多数据。但是,我使用@Parcelize并认为这会导致隐藏的样板代码在宗地之间来回跳动的问题。我希望有一个解决方案,而不必删除@Parcelize,因为我是Kotlin和应用程序开发的新手,但是如果没有,那也可以。
这是长的无限循环错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.raymondllc.testapp, PID: 5396
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:389)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.StackOverflowError: stack size 8MB
at android.os.Parcel$ReadWriteHelper.writeString(Parcel.java:353)
at android.os.Parcel.writeString(Parcel.java:675)
at android.os.Parcel.writeParcelableCreator(Parcel.java:1797)
at android.os.Parcel.writeParcelable(Parcel.java:1790)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
E/AndroidRuntime: at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
I/zygote: Do partial code cache collection, code=59KB, data=48KB
I/zygote: After code cache collection, code=59KB, data=48KB
Increasing code cache capacity to 256KB
I/zygote: Background concurrent copying GC freed 3584(1418KB) AllocSpace objects, 2(1572KB) LOS objects, 17% free, 28MB/34MB, paused 720us total 341.766ms
I/zygote: Background concurrent copying GC freed 184569(14MB) AllocSpace objects, 0(0B) LOS objects, 15% free, 31MB/37MB, paused 671us total 213.138ms
I/chatty: uid=10080(com.raymondllc.testapp) identical 3054 lines
E/AndroidRuntime: at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
E/AndroidRuntime: at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Facility.writeToParcel(Unknown Source:47)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at com.raymondllc.testapp.Building.writeToParcel(Unknown Source:55)
at android.os.Parcel.writeParcelable(Parcel.java:1791)
at android.os.Parcel.writeValue(Parcel.java:1697)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:838)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1542)
at android.os.Bundle.writeToParcel(Bundle.java:1232)
at android.os.Parcel.writeBundle(Parcel.java:878)
at android.content.Intent.writeToParcel(Intent.java:9599)
at android.app.IActivityManager$Stub$Proxy.startActivity(IActivityManager.java:4327)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1611)
at android.app.Activity.startActivityForResult(Activity.java:4487)
at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:68)
at android.app.Activity.startActivityForResult(Activity.java:4445)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:751)
at android.app.Activity.startActivity(Activity.java:4806)
at android.app.Activity.startActivity(Activity.java:4774)
at com.raymondllc.testapp.MainActivity.nextButton(MainActivity.kt:25)
... 11 more
我认为它在“未知源”中的读/写之间来回跳动,当我在Android Studio中单击时,它会在我编写的两个不同类之间反弹。这是类的定义:
@Parcelize
class Facility(private var facID : String,
private var buildings: Array<Building>) : Parcelable {
...
@Parcelize
class Building(private var bldgID: String = "",
private var sections : Array<Section> = emptyArray(),
private var facility: Facility? = null) : Parcelable {
...
非常感谢您的帮助/建议!
修改1:
我在类前面删除了@Parcelize,并允许Android Studio自动生成样板代码,现在我可以看到问题了,但不知道如何解决。 Building类中的writeToParcel
函数调用writeParcelable(facility, flags)
,Facility类中的writeToParcel
函数调用Facility类中的writeTypedArray(building, flags)
,后者在每个建筑物上调用writeToParcel
在给定的设施中,依次重复列出的过程,直到停止为止。有想法吗?
答案 0 :(得分:0)
您将需要打破循环依赖。一种方法是仅存储设施或建筑物的ID,而不是存储整个对象。您可以编写一个单独的类,仅用于存储对象的精简版本,然后使用ID重建原始对象。