gpt4 book ai didi

android - 无法打开 Activity : cannot create instance of Android View Model

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

我正在创建一个应用程序来扫描条形码并使用 Android View 模型和 LiveData 将该信息导入房间数据库。这是错误信息

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.davin.scoutingapp2019/com.example.davin.scoutingapp2019.DataDisplay}: java.lang.RuntimeException: Cannot create an instance of class com.example.davin.scoutingapp2019.TeamViewModel
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
Caused by: java.lang.RuntimeException: Cannot create an instance of class com.example.davin.scoutingapp2019.TeamViewModel
at android.arch.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:207)
at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:134)
at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:102)
at com.example.davin.scoutingapp2019.DataDisplay.onCreate(DataDisplay.java:30)
at android.app.Activity.performCreate(Activity.java:6672)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6123) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757) 
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at android.arch.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:199)
at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:134) 
at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:102) 
at com.example.davin.scoutingapp2019.DataDisplay.onCreate(DataDisplay.java:30) 
at android.app.Activity.performCreate(Activity.java:6672) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6123) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.example.davin.scoutingapp2019.TeamDAO com.example.davin.scoutingapp2019.TeamsDatabase.teamDAO()' on a null object reference
at com.example.davin.scoutingapp2019.TeamRepository.<init>(TeamRepository.java:15)
at com.example.davin.scoutingapp2019.TeamViewModel.<init>(TeamViewModel.java:15)
at java.lang.reflect.Constructor.newInstance0(Native Method) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:430) 
at android.arch.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:199) 
at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:134) 
at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:102) 
at com.example.davin.scoutingapp2019.DataDisplay.onCreate(DataDisplay.java:30) 
at android.app.Activity.performCreate(Activity.java:6672) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6123) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757) 

这是 AndroidViewModel 类

package com.example.davin.scoutingapp2019;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.LiveData;

import java.util.List;

public class TeamViewModel extends AndroidViewModel {
private TeamRepository mRepository;
private LiveData<List<Team>> mAllData;

public TeamViewModel(Application application) {
super(application);
mRepository = new TeamRepository(application);
mAllData = mRepository.getAllWords();
}


public LiveData<List<Team>> getAllTeams() {
return mAllData;
}

public void insert(Team team) {
mRepository.insert(team);
}


}

这是具有 TeamViewModel 实例的类/Activity DataDisplay

package com.example.davin.scoutingapp2019;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import java.util.List;

public class DataDisplay extends AppCompatActivity {


private TeamViewModel teamViewModel;
public static final int NEW_WORD_ACTIVITY_REQUEST_CODE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data_display);
RecyclerView recyclerView = findViewById(R.id.recyclerview);
final TeamListAdapter adapter = new TeamListAdapter(this);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));

teamViewModel = ViewModelProviders.of(this).get(TeamViewModel.class);
teamViewModel.getAllTeams().observe(this, new Observer<List<Team>>() {
@Override
public void onChanged(@Nullable final List<Team> teams) {
// Update the cached copy of the words in the adapter.
adapter.setTeams(teams);
}
});
}




public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == NEW_WORD_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
Destring destring=new Destring(data.getStringExtra("str"));
Team newTeam= new Team(destring.getTeamNumber(),destring.getPosition(),destring.getSandstorm("hab line"),destring.getSandstorm("cargo balls"),destring.getSandstorm("cargo hatches"),destring.getSandstorm("rocket balls"),destring.getSandstorm("rocket hatches"),destring.getTeleop("cargo balls"),destring.getTeleop("cargo hatches"),destring.getTeleop("rocket balls"),destring.getTeleop("rocket hatches"),destring.getRocketRole(),destring.getClimberRole(),destring.getOverallRole(),destring.getOtherComments());

teamViewModel.insert(newTeam);
} else {
Toast.makeText(
getApplicationContext(),
R.string.empty_not_saved,
Toast.LENGTH_LONG).show();
}
}






}

这个问题和另一个相似question但我的 TeamViewModel 构造函数中只有一个参数。

其余代码在github

谢谢

最佳答案

您的 ViewModel 需要一个工厂类。

public class MyViewModelFactory implements ViewModelProvider.Factory {
private Application mApplication;
private String mParam;


public MyViewModelFactory(Application application, String param) {
mApplication = application;
mParam = param;
}


@Override
public <T extends ViewModel> T create(Class<T> modelClass) {
return (T) new MyViewModel(mApplication, mParam);
}

在实例化 View 模型时,您可以这样做:

MyViewModel myViewModel = new ViewModelProvider(this, new MyViewModelFactory(this.getApplication(), "my awesome param")).get(MyViewModel.class);

原创answer

关于android - 无法打开 Activity : cannot create instance of Android View Model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54677426/

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