使用 ViewModelProvider(this) 初始化 ViewModel 实例时出现问题
Problem initializing ViewModel instance using ViewModelProvider(this)
我正在实例化 ViewModel class 实例,但它没有采用参数 this
。请看我的代码。
这是一个简单的代码,因为我正在尝试学习 ViewModel 和 LiveData
我搜索过,但到处都给出了 ViewModelProviders 的解释
请帮忙解决
package com.nitin.viewmodellivedata;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelStoreOwner;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
Button buttonAdd;
Button buttonReset;
int score;
ScoreViewModel scoreViewModel = new ViewModelProvider(ScoreViewModel);
public void addScore(View view){
scoreViewModel.addScore();
textView.setText(String.valueOf(scoreViewModel.getScore()));
}
public void resetScore(View view){
scoreViewModel.resetScore();
textView.setText(String.valueOf(scoreViewModel.getScore()));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textView);
scoreViewModel = new ScoreViewModel();
scoreViewModel = new ViewModelProvider(this).get(ScoreViewModel.class);
textView.setText(String.valueOf(scoreViewModel.getScore()));
}
}
确保您的 build.gradle (:app)
文件中有两个依赖项:
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
我正在实例化 ViewModel class 实例,但它没有采用参数 this
。请看我的代码。
这是一个简单的代码,因为我正在尝试学习 ViewModel 和 LiveData
我搜索过,但到处都给出了 ViewModelProviders 的解释 请帮忙解决
package com.nitin.viewmodellivedata;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelStoreOwner;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
Button buttonAdd;
Button buttonReset;
int score;
ScoreViewModel scoreViewModel = new ViewModelProvider(ScoreViewModel);
public void addScore(View view){
scoreViewModel.addScore();
textView.setText(String.valueOf(scoreViewModel.getScore()));
}
public void resetScore(View view){
scoreViewModel.resetScore();
textView.setText(String.valueOf(scoreViewModel.getScore()));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textView);
scoreViewModel = new ScoreViewModel();
scoreViewModel = new ViewModelProvider(this).get(ScoreViewModel.class);
textView.setText(String.valueOf(scoreViewModel.getScore()));
}
}
确保您的 build.gradle (:app)
文件中有两个依赖项:
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'