Sugar ORM 无法在 Kotlin 中保存
Sugar ORM can't save in Kotlin
我有一个扩展 SugarRecord
的 class,这是它:
class Advertiser : SugarRecord<Advertiser> {
var number: String? = null
var name: String? = null
constructor() {}
constructor(number: String, name: String) {
this.number = number
this.name = name
}
}
我在清单中有 sugar 元数据:
<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)
您的申请应该是 SugarApp
AndroidManifest.xml
。
<application android:name="com.orm.SugarApp"
...
</application>
我有一个扩展 SugarRecord
的 class,这是它:
class Advertiser : SugarRecord<Advertiser> {
var number: String? = null
var name: String? = null
constructor() {}
constructor(number: String, name: String) {
this.number = number
this.name = name
}
}
我在清单中有 sugar 元数据:
<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)
您的申请应该是 SugarApp
AndroidManifest.xml
。
<application android:name="com.orm.SugarApp"
...
</application>