以下将产生 IllegalArgumentException 因为您“无法序列化抽象类”
sealed class Animal {
data class Dog(val isGoodBoy: Boolean) : Animal()
data class Cat(val remainingLives: Int) : Animal()
}
private val moshi = Moshi.Builder()
.build()
@Test
fun test() {
val animal: Animal = Animal.Dog(true)
println(moshi.adapter(Animal::class.java).toJson(animal))
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用自定义适配器解决此问题,但我能想到的唯一解决方案是显式编写每个子类的所有属性名称。例如:
class AnimalAdapter {
@ToJson
fun toJson(jsonWriter: JsonWriter, animal: Animal) {
jsonWriter.beginObject()
jsonWriter.name("type")
when (animal) {
is Animal.Dog -> jsonWriter.value("dog")
is Animal.Cat -> jsonWriter.value("cat")
}
jsonWriter.name("properties").beginObject()
when (animal) {
is Animal.Dog -> jsonWriter.name("isGoodBoy").value(animal.isGoodBoy)
is Animal.Cat -> jsonWriter.name("remainingLives").value(animal.remainingLives)
}
jsonWriter.endObject().endObject()
}
.... …
Run Code Online (Sandbox Code Playgroud) 我正在使用Windows 7,最近从Eclipse切换到Android Studio.我现在无法调试.
运行Android Studio 0.8.6,我设置了一个完全默认的安装,我使用新项目向导创建了一个默认的空项目,目标是ICS SDK.然后我在onCreate中放置一个断点,单击调试按钮并运行.
附加调试器,因为我可以在调试器窗口中看到消息"连接到目标VM".
我知道代码正在执行,因为我正在更新UI中的一些文本来显示这一点.
我曾尝试在许多地方设置断点,但没有一个被击中.
我把头发拉到这里,因为我看不出我做错了什么.我是新手,因此我认为gradle中可能存在一些我应该更改的设置,但是使用向导构建的绝对标准项目肯定会让我遇到断点?
我注意到的一件事是在我的build.gradle文件中没有提到调试版本,只提供了一个版本.我想知道这可能是问题吗?
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
Run Code Online (Sandbox Code Playgroud)
注意.我在我自己的设备和模拟器上都尝试过这个
更新:
我更改了View Breakpoints选项中的设置以打开"Java Exception Breakpoints",但仅限于未捕获的异常.然后,在onCreate结束时,我故意造成NullPointerException.当我现在在调试中运行时,我仍然没有点击我的实际代码并且没有在源代码上看到代码中断,但程序确实暂停.我知道这是我的NullPointerException导致这种情况,因为当我删除它时,我可以看到代码继续并且不会中断.
在我的代码中断调试窗口时,我在"主"线程中,在一个名为"performLaunchActivity"的函数中.我看不到比这更多的信息了.因此,我可能正在通过调用performLaunchActivity的任何级别的代码进行调试,但我的源代码被视为无法单步执行它?
android intellij-idea gradle android-studio android-gradle-plugin
在编译我的android ndk项目时,我添加了
APP_STL := stlport_static
Run Code Online (Sandbox Code Playgroud)
到我的jni/Application.mk文件,允许使用一些STL的东西.但是,当我清理和构建时,我得到一个错误libstlport_static.a:没有这样的文件:权限被拒绝我发现这个解决方案是添加
STLPORT_FORCE_REBUILD := true
Run Code Online (Sandbox Code Playgroud)
这是一个修复,但每次重建stlport库显然非常耗时.有没有更好的解决这个问题?