gpt4 book ai didi

android - Sugar ORM 无法在 Kotlin 中保存

转载 作者:行者123 更新时间:2023-11-30 00:05:26 25 4
gpt4 key购买 nike

我有一个扩展 SugarRecord 的类,就在这里:

class Advertiser : SugarRecord<Advertiser> {
var number: String? = null
var name: String? = null

constructor() {}

constructor(number: String, name: String) {
this.number = number
this.name = name
}
}

list 中有糖元数据:

    <meta-data android:name="DATABASE" android:value="sugar_example.db" />
<meta-data android:name="VERSION" android:value="2" />
<meta-data android:name="QUERY_LOG" android:value="true" />
<meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="com.example" />

我正在尝试创建 Advertiser 的新实例并将其保存在 MainActivity.kt 中:

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val advertiser = Advertiser("123123", "test advertiser")
advertiser.save()





}
}

但我遇到了异常,这是堆栈跟踪:

FATAL EXCEPTION: main Process: ge.ketili.team.blocksms, PID: 22305 java.lang.RuntimeException: Unable to start activity ComponentInfo{ge.ketili.team.blocksms/ge.ketili.team.blocksms.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.orm.Database com.orm.SugarApp.getDatabase()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2706) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1514) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:163) at android.app.ActivityThread.main(ActivityThread.java:6205) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.orm.Database com.orm.SugarApp.getDatabase()' on a null object reference at com.orm.SugarRecord.save(SugarRecord.java:45) at ge.ketili.team.blocksms.MainActivity.onCreate(MainActivity.kt:15) at android.app.Activity.performCreate(Activity.java:6864) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)

最佳答案

您的应用程序应该是 AndroidManifest.xml 中的 SugarApp

<application android:name="com.orm.SugarApp"
...
</application>

关于android - Sugar ORM 无法在 Kotlin 中保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49113922/

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