使用匕首柄 + 解析服务器 SDK:java.lang.IllegalArgumentException
Using dagger hilt + parse Server SDK: java.lang.IllegalArgumentException
目前,我正在做一个项目,我尝试同时使用匕首(刀柄)和 parseServer SDK。问题是在我看来不可能在一个项目中使用这种组合...
以下是我对为什么我认为这是不可能的看法,至少在方式上是这样,并且知道我希望它成为什么样子:
当使用 Parse SDK 并创建一个 class 时,您必须使用 @ParseClasssName("YourDBClassName")
对其进行注释。在调用 Parse.initialize...
.
之前,必须使用 ParseObject.registerSubclass(YourClassName::class.java)
在 onCreate()
中注册此自定义 class
现在我的问题来了:当使用匕首(刀柄)并创建这个自定义Class时,必须在调用onCreate()
之前注入@Inject lateinit var YourClassName
。但是当你inject
这个customClass在调用onCreate()
解析之前抛出一个Error,因为1.它不知道这个class是一个customClass 2. 必须在 onCreate()
方法中创建 ParseObject。
为了更清楚一点,这是我的代码和我想要实现的目标:
自定义用户Class
@ParseClassName("User")
data class User(var name: String, var passWord: String, var eMail: String) : ParseUser() {
@Inject constructor(): this("","","")
// Creates a new User
fun signUpWithOutVerification() = apply {
username = name
setPassword(passWord)
email = eMail
signUpInBackground()
}
// logIn
fun logIn() = logInInBackground(name, passWord) ?: Timber.e("logIn Error")
// logOut
fun logOut() = logOutInBackground() ?: Timber.e("logOut Error")
// Update
fun updateCredential(key: String, value: Any) = getCurrentUser().apply {
put(key, value)
saveInBackground()
} ?: Timber.e("Update failed")
// Delet and Logout
fun deleteUser() = getCurrentUser().apply {
deleteInBackground()
logOut()
} ?: Timber.e("Delete failed")
应用模块
@Module
@InstallIn(ApplicationComponent::class)
object ApplicationModule {
@Provides
@Singleton
fun provideUser() = User()
}
主要活动
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
@Inject lateinit var user: User
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
ParseObject.registerSubclass(User::class.java)
Parse.initialize(
Parse.Configuration.Builder(this)
.applicationId("YourAppKey")
.clientKey("YourClientKey")
.server("https://parseapi.back4app.com")
.build()
)
Timber.plant(Timber.DebugTree())
lateinit var userName: String
lateinit var userPw: String
btn_signup.setOnClickListener {
userName = etName.text.toString()
userPw = etPassword.text.toString()
user.apply{
name = userName
passWord = userPw
eMail = "$userName@email.com"
signUpWithOutVerification()
}
}
主应用程序
@HiltAndroidApp
class App : Application()
当前错误堆栈
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.app, PID: 31532
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.ui.view.MainActivity}: java.lang.IllegalArgumentException: You must create this type of ParseObject using ParseObject.create() or the proper subclass.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.IllegalArgumentException: You must create this type of ParseObject using ParseObject.create() or the proper subclass.
at com.parse.ParseObject.<init>(ParseObject.java:162)
at com.parse.ParseObject.<init>(ParseObject.java:132)
at com.parse.ParseUser.<init>(ParseUser.java:56)
at com.example.app.data.model.User.<init>(User.kt:10)
at com.example.app.data.model.User.<init>(User.kt:12)
at com.example.app.di.modules.ApplicationModule.provideUser(ApplicationModule.kt:16)
at com.example.app.di.modules.ApplicationModule_ProvideUserFactory.provideUser(ApplicationModule_ProvideUserFactory.java:27)
at com.example.app.DaggerApp_HiltComponents_ApplicationC.getUser(DaggerApp_HiltComponents_ApplicationC.java:61)
at com.example.app.DaggerApp_HiltComponents_ApplicationC.access00(DaggerApp_HiltComponents_ApplicationC.java:41)
at com.example.app.DaggerApp_HiltComponents_ApplicationC$ActivityRetainedCImpl$ActivityCImpl.injectMainActivity2(DaggerApp_HiltComponents_ApplicationC.java:174)
at com.example.app.DaggerApp_HiltComponents_ApplicationC$ActivityRetainedCImpl$ActivityCImpl.injectMainActivity(DaggerApp_HiltComponents_ApplicationC.java:155)
at com.example.app.ui.view.Hilt_MainActivity.inject(Hilt_MainActivity.java:62)
at com.example.app.ui.view.Hilt_MainActivity.onCreate(Hilt_MainActivity.java:37)
at com.example.app.ui.view.MainActivity.onCreate(MainActivity.kt:22)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
请注意!
- 我以前从未使用过依赖注入
- 我是匕首新手(刀柄)
- 我是解析sdk的新手
所以我可能只是不具备编写所需代码的知识。我想做的是,那把匕首(刀柄)创建用户 + 获取他的名字等...
非常感谢您的帮助,谢谢!
您应该将 registerSubclass()
代码移动到您的 application
class。
你的application
class:
@HiltAndroidApp
class App : Application() {
override fun onCreate() {
super.onCreate()
ParseObject.registerSubclass(User::class.java)
Parse.initialize(
Parse.Configuration.Builder(this)
.applicationId("YourAppKey")
.clientKey("YourClientKey")
.server("https://parseapi.back4app.com")
.build()
)
}
}
在清单文件中:-
<application
android:name=".App"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
....
目前,我正在做一个项目,我尝试同时使用匕首(刀柄)和 parseServer SDK。问题是在我看来不可能在一个项目中使用这种组合...
以下是我对为什么我认为这是不可能的看法,至少在方式上是这样,并且知道我希望它成为什么样子:
当使用 Parse SDK 并创建一个 class 时,您必须使用 @ParseClasssName("YourDBClassName")
对其进行注释。在调用 Parse.initialize...
.
ParseObject.registerSubclass(YourClassName::class.java)
在 onCreate()
中注册此自定义 class
现在我的问题来了:当使用匕首(刀柄)并创建这个自定义Class时,必须在调用onCreate()
之前注入@Inject lateinit var YourClassName
。但是当你inject
这个customClass在调用onCreate()
解析之前抛出一个Error,因为1.它不知道这个class是一个customClass 2. 必须在 onCreate()
方法中创建 ParseObject。
为了更清楚一点,这是我的代码和我想要实现的目标:
自定义用户Class
@ParseClassName("User")
data class User(var name: String, var passWord: String, var eMail: String) : ParseUser() {
@Inject constructor(): this("","","")
// Creates a new User
fun signUpWithOutVerification() = apply {
username = name
setPassword(passWord)
email = eMail
signUpInBackground()
}
// logIn
fun logIn() = logInInBackground(name, passWord) ?: Timber.e("logIn Error")
// logOut
fun logOut() = logOutInBackground() ?: Timber.e("logOut Error")
// Update
fun updateCredential(key: String, value: Any) = getCurrentUser().apply {
put(key, value)
saveInBackground()
} ?: Timber.e("Update failed")
// Delet and Logout
fun deleteUser() = getCurrentUser().apply {
deleteInBackground()
logOut()
} ?: Timber.e("Delete failed")
应用模块
@Module
@InstallIn(ApplicationComponent::class)
object ApplicationModule {
@Provides
@Singleton
fun provideUser() = User()
}
主要活动
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
@Inject lateinit var user: User
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
ParseObject.registerSubclass(User::class.java)
Parse.initialize(
Parse.Configuration.Builder(this)
.applicationId("YourAppKey")
.clientKey("YourClientKey")
.server("https://parseapi.back4app.com")
.build()
)
Timber.plant(Timber.DebugTree())
lateinit var userName: String
lateinit var userPw: String
btn_signup.setOnClickListener {
userName = etName.text.toString()
userPw = etPassword.text.toString()
user.apply{
name = userName
passWord = userPw
eMail = "$userName@email.com"
signUpWithOutVerification()
}
}
主应用程序
@HiltAndroidApp
class App : Application()
当前错误堆栈
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.app, PID: 31532
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.ui.view.MainActivity}: java.lang.IllegalArgumentException: You must create this type of ParseObject using ParseObject.create() or the proper subclass.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.IllegalArgumentException: You must create this type of ParseObject using ParseObject.create() or the proper subclass.
at com.parse.ParseObject.<init>(ParseObject.java:162)
at com.parse.ParseObject.<init>(ParseObject.java:132)
at com.parse.ParseUser.<init>(ParseUser.java:56)
at com.example.app.data.model.User.<init>(User.kt:10)
at com.example.app.data.model.User.<init>(User.kt:12)
at com.example.app.di.modules.ApplicationModule.provideUser(ApplicationModule.kt:16)
at com.example.app.di.modules.ApplicationModule_ProvideUserFactory.provideUser(ApplicationModule_ProvideUserFactory.java:27)
at com.example.app.DaggerApp_HiltComponents_ApplicationC.getUser(DaggerApp_HiltComponents_ApplicationC.java:61)
at com.example.app.DaggerApp_HiltComponents_ApplicationC.access00(DaggerApp_HiltComponents_ApplicationC.java:41)
at com.example.app.DaggerApp_HiltComponents_ApplicationC$ActivityRetainedCImpl$ActivityCImpl.injectMainActivity2(DaggerApp_HiltComponents_ApplicationC.java:174)
at com.example.app.DaggerApp_HiltComponents_ApplicationC$ActivityRetainedCImpl$ActivityCImpl.injectMainActivity(DaggerApp_HiltComponents_ApplicationC.java:155)
at com.example.app.ui.view.Hilt_MainActivity.inject(Hilt_MainActivity.java:62)
at com.example.app.ui.view.Hilt_MainActivity.onCreate(Hilt_MainActivity.java:37)
at com.example.app.ui.view.MainActivity.onCreate(MainActivity.kt:22)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
请注意!
- 我以前从未使用过依赖注入
- 我是匕首新手(刀柄)
- 我是解析sdk的新手
所以我可能只是不具备编写所需代码的知识。我想做的是,那把匕首(刀柄)创建用户 + 获取他的名字等...
非常感谢您的帮助,谢谢!
您应该将 registerSubclass()
代码移动到您的 application
class。
你的application
class:
@HiltAndroidApp
class App : Application() {
override fun onCreate() {
super.onCreate()
ParseObject.registerSubclass(User::class.java)
Parse.initialize(
Parse.Configuration.Builder(this)
.applicationId("YourAppKey")
.clientKey("YourClientKey")
.server("https://parseapi.back4app.com")
.build()
)
}
}
在清单文件中:-
<application
android:name=".App"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
....