ViewModel class 不断地 returns 运行时和 IllegalAccessException

ViewModel class constantly returns Runtime and IllegalAccessException

我昨天发布了这个问题,但我认为我可能不是很清楚这个问题。

在我使用 ViewModel link 与 MainActivity 使用 ViewProvider 的行中,代码 returns 运行时异常。此外,该代码还 returns 消息“无法在 com.example.viewmodelpractice.ViewModelStorage 处创建 class 的实例”。

错误信息具体指向这一行:

viewModelStorage = new ViewModelProvider(this).get(ViewModelStorage.class);

这是 class 的其余部分:

ViewModelStorage.java

package com.example.viewmodelpractice;

import androidx.lifecycle.ViewModel;

class ViewModelStorage extends ViewModel {

private String name;
private String age;

String getName() {
    return name;
}

void setName(String name){
    this.name = name;
}

String getAge() {
    return age;
}

void setAge(String age){
    this.age = age;
}
}

主要Activity.java

package com.example.viewmodelpractice;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private EditText nameEditText;
    private EditText ageEditText;
    private TextView resultTextView;

    private ViewModelStorage viewModelStorage;

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

        nameEditText = findViewById(R.id.name_notice_editText);
        ageEditText = findViewById(R.id.age_notice_editText);
        resultTextView = findViewById(R.id.result_textView);

        viewModelStorage = new ViewModelProvider(this).get(ViewModelStorage.class);
        storeDataMethod();
    }

    private void storeDataMethod() {
        viewModelStorage.setName(nameEditText.getText().toString());
        viewModelStorage.setAge(ageEditText.getText().toString());
        getDataMethod();
    }

    private void getDataMethod() {
        resultTextView.setText(("Hey there, ").concat(viewModelStorage.getName().concat(". You are ").concat(viewModelStorage.getAge()).concat(" years old.")));
    }
}

错误信息

2020-05-04 12:06:38.744 20688-20688/com.example.viewmodelpractice E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.viewmodelpractice, PID: 20688
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.viewmodelpractice/com.example.viewmodelpractice.MainActivity}: java.lang.RuntimeException: Cannot create an instance of class com.example.viewmodelpractice.ViewModelStorage
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:201)
    at android.app.ActivityThread.main(ActivityThread.java:6823)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

Caused by: java.lang.RuntimeException: Cannot create an instance of class com.example.viewmodelpractice.ViewModelStorage
        at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:223)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
        at com.example.viewmodelpractice.MainActivity.onCreate(MainActivity.java:27)
        at android.app.Activity.performCreate(Activity.java:7224)
        at android.app.Activity.performCreate(Activity.java:7213)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2926)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:201) 
        at android.app.ActivityThread.main(ActivityThread.java:6823) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873) 

Caused by: java.lang.IllegalAccessException: java.lang.Class<com.example.viewmodelpractice.ViewModelStorage> is not accessible from java.lang.Class<androidx.lifecycle.ViewModelProvider$NewInstanceFactory>
        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.viewmodelpractice.MainActivity.onCreate(MainActivity.java:27) 
        at android.app.Activity.performCreate(Activity.java:7224) 
        at android.app.Activity.performCreate(Activity.java:7213) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2926) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:201) 
        at android.app.ActivityThread.main(ActivityThread.java:6823) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

而不是这个:

class ViewModelStorage extends ViewModel {

像这样:

public class ViewModelStorage extends ViewModel {

因此,您收到 IllegalAccessException!