gpt4 book ai didi

java - 如何在 Android 中使用 kotlin 枚举修复 'java.lang.InstantiationError'

转载 作者:行者123 更新时间:2023-11-29 18:30:07 25 4
gpt4 key购买 nike

我正在尝试从 Android 中的遗留 java Activity 启动新的 kotlin Activity ,并且需要传递一个枚举(在 kotlin 中定义)作为 intent extra。从第一个(java) Activity 中,我可以看到额外的 Intent ,但是从第二个(kotlin) Activity 中,应用程序崩溃了,因为额外的 Intent 消失了。有没有办法直接传递枚举,还是我必须恢复传递枚举的名称或序号?

请注意,如果枚举类不包含其自身的任何抽象函数(在本例中为 getTitle()),则不会出现错误。但是,我需要保留这些功能。

主 Activity .java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
IntentExtension.putParcelableExtra(intent, "test", TestEnum.TWO);
startActivityForResult(intent, 42);
}
});
}
}

SecondActivity.kt

@Parcelize
enum class TestEnum : Parcelable {
ONE {
override fun getTitle(): String = "Title One!"
},

TWO {
override fun getTitle(): String = "Title Two!"
};

abstract fun getTitle(): String
}

class SecondActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
val extra = intent.getParcelableExtra<TestEnum>("test") // crash here
welcomeText.text = getString(R.string.message, extra.name)
}
}

Intent 扩展.kt

@file:JvmName("IntentExtension")

fun Intent.putParcelableExtra(key: String, value: Parcelable) {
this.putExtra(key, value)
}

错误日志

    java.lang.InstantiationError: com.example.myapplication.TestEnum
at com.example.myapplication.TestEnum$Creator.createFromParcel(Unknown Source:5)
at android.os.Parcel.readParcelable(Parcel.java:2782)
at android.os.Parcel.readValue(Parcel.java:2676)
at android.os.Parcel.readArrayMapInternal(Parcel.java:3043)
at android.os.BaseBundle.unparcel(BaseBundle.java:257)
at android.os.Bundle.getParcelable(Bundle.java:888)
at android.content.Intent.getParcelableExtra(Intent.java:7075)
at com.example.myapplication.SecondActivity.onCreate(SecondActivity.kt:27)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

最佳答案

似乎 kotlin 注释处理器无法为枚举 parcelization 生成正确的代码。像这样编写您自己的代码:

enum class TestEnum : Parcelable {
ONE {
override fun getTitle(): String = "Title One!"
},

TWO {
override fun getTitle(): String = "Title Two!"
};

abstract fun getTitle(): String

override fun describeContents(): Int {
return 0
}

override fun writeToParcel(dest: Parcel?, flags: Int) {
dest!!.writeInt(ordinal)
}

companion object CREATOR : Parcelable.Creator<TestEnum> {
override fun createFromParcel(parcel: Parcel): TestEnum {
return values()[parcel.readInt()]
}

override fun newArray(size: Int): Array<TestEnum?> {
return newArray(size)
}
}
}

关于java - 如何在 Android 中使用 kotlin 枚举修复 'java.lang.InstantiationError',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56741623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com