1

目前,我正在开发一个项目,我尝试同时使用 dagger (hilt) 和 parseServer SDK。问题是,在我看来,不可能在一个项目中使用这种组合......

以下是我对为什么我认为这是不可能的看法,至少在方式上并且知道我希望它是怎样的:

使用 Parse SDK 并创建类时,您必须使用@ParseClasssName("YourDBClassName"). 这个自定义类必须onCreate()ParseObject.registerSubclass(YourClassName::class.java)调用之前注册Parse.initialize...

@Inject lateinit var YourClassName现在我的问题来了:当使用 dagger (hilt) 并创建这个 customClass 时,它必须在调用之前注入onCreate()。但是当你inject在调用解析之前这个customClassonCreate()会抛出一个错误,因为1.它不知道这个类是一个customClass 2. ParseObject 必须在onCreate()方法内创建。

为了更清楚一点,这是我的代码和我想要实现的目标:

自定义用户类

@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.access$1000(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) 

请注意!

  1. 我以前从未使用过依赖注入
  2. 我是匕首新手(刀柄)
  3. 我是解析sdk的新手

所以有可能我只是不具备编写我想要的代码的知识。我想要做的是,匕首(刀柄)创建用户+获取他的名字等......

我感谢每一个帮助,谢谢!

4

1 回答 1

1

您应该将registerSubclass()代码移至您的application班级。

你的application班级:

@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"
        ....
    
于 2020-07-04T09:57:53.410 回答