gpt4 book ai didi

java - AndroidViewModel 没有零参数构造函数。如何解决这个问题?

转载 作者:行者123 更新时间:2023-12-03 23:02:28 24 4
gpt4 key购买 nike

我几乎在整个互联网上搜索了我的问题,但找不到我需要的答案。
是的。我的问题不是唯一的,但没有答案,这可以帮助我。
所以我决定自己问。
现在关于问题:

public class AuthActivityViewModel extends AndroidViewModel {

Repository repository;
private LiveData<List<User>> users;
private LiveData<User> user;

public AuthActivityViewModel(@NonNull Application application) {
super(application);

repository = new Repository(application);
}

public LiveData<List<User>> getUsers() {
users = repository.getUsers();
return users;
}

public LiveData<User> getUser(int userId) {
user = repository.getUser(userId);
return user;
}

public void addNewUser(User user) {
repository.insertUser(user);
}

public void updateUser(User user) {
repository.updateUser(user);
}

public void deleteUser(User user) {
repository.deleteUser(user);
}
}
..................................................
public class AuthActivity extends AppCompatActivity implements AuthInteractionListener {

private FragmentManager fragmentManager;
private final ConnectionBroadcastReceiver connectionBroadcastReceiver = new ConnectionBroadcastReceiver();

private int limitOfOnWindowFocusChangedOperationForAuthActivity = 0;
private boolean isActivityRecreated;

AuthActivityViewModel authActivityViewModel;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auth);

isActivityRecreated = true;
IntentFilter connectionIntentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(connectionBroadcastReceiver, connectionIntentFilter);

authActivityViewModel = new ViewModelProvider(this).get(AuthActivityViewModel.class);
}
...
}
......
错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.findthepikachuupgradeversion2/com.example.android.findthepikachuupgradeversion3.view.AuthActivity}: java.lang.RuntimeException: Cannot create an instance of class com.example.android.findthepikachuupgradeversion3.viewmodel.AuthActivityViewModel
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3307)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3446)
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:2043)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7548)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
Caused by: java.lang.RuntimeException: Cannot create an instance of class com.example.android.findthepikachuupgradeversion3.viewmodel.AuthActivityViewModel
at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:221)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
at com.example.android.findthepikachuupgradeversion3.view.AuthActivity.onCreate(AuthActivity.java:44)
at android.app.Activity.performCreate(Activity.java:7893)
at android.app.Activity.performCreate(Activity.java:7880)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3282)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3446) 
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:2043) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:224) 
at android.app.ActivityThread.main(ActivityThread.java:7548) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950) 
Caused by: java.lang.InstantiationException: java.lang.Class<com.example.android.findthepikachuupgradeversion3.viewmodel.AuthActivityViewModel> has no zero argument constructor
at java.lang.Class.newInstance(Native Method)
at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:219)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187) 
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150) 
at com.example.android.findthepikachuupgradeversion3.view.AuthActivity.onCreate(AuthActivity.java:44) 
at android.app.Activity.performCreate(Activity.java:7893) 
at android.app.Activity.performCreate(Activity.java:7880) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3282) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3446) 
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:2043) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:224) 
at android.app.ActivityThread.main(ActivityThread.java:7548) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950) 
  • 我尝试在类 AuthActivityViewModel 中添加一个不带参数的构造函数,但收到此错误

  • There is no default constructor available inandroidx.lifecycle.AndroidViewModel


  • 建议如
  • https://stackoverflow.com/a/54462438/14048134
  • 确保您的 View 模型类构造函数是公开的。
  • 确保您在 gradle 文件中添加了生命周期的依赖项

  • 不是实际的。为什么?因为他们已经是他们应该的样子了,但我有一个不应该的错误。
    既然问题存在,肯定是有什么我没有算进去的。

    这是 gradle 文件的一部分:

    def lifecycle_version = "2.2.0"implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"


    implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"

    annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"

    def room_version = "2.2.5"

    implementation "androidx.room:room-runtime:$room_version"

    annotationProcessor "androidx.room:room-compiler:$room_version"

    我怎么解决这个问题?

    最佳答案

    将以下依赖项添加到 gradle app 模块级别。 implementation 'androidx.lifecycle:lifecycle-extensions:$lifecycle_version扩展包括许多库,包括 LiveData , 所以,通过添加它,你可以摆脱:implementation 'androidx.lifecycle:lifecycle-livedata:$lifecycle_version

    But it appears lifecycle-extensions has been deprecated. Is thereanother dependency that solves this issue?


    没错,它是 deprecated as of version:2.2.0 .

    lifecycle-extensions Artifact Deprecation: With the above deprecationof ViewModelProviders.of(), this release marks the deprecation of thelast API in lifecycle-extensions and this artifact should now beconsidered deprecated in its entirety. We strongly recommend dependingon the specific Lifecycle artifacts you need (such aslifecycle-service if you’re using LifecycleService andlifecycle-process if you’re using ProcessLifecycleOwner) rather thanlifecycle-extensions as there will not be a future 2.3.0 release oflifecycle-extensions.


    正如文档所引用的那样,您可以改用特定的生命周期工件:正如我们在评论中讨论的那样,有效的特定生命周期工件是: implementation "android.arch.lifecycle:runtime:$lifecycle_version并保留您的其他生命周期依赖项工件。

    关于java - AndroidViewModel 没有零参数构造函数。如何解决这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64834877/

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