gpt4 book ai didi

ViewModelProviders.of(this).get(ViewModel.class) 出现 java 错误;

转载 作者:行者123 更新时间:2023-12-01 16:40:12 24 4
gpt4 key购买 nike

我试图在 android studio 中制作一个新片段,每当我构建我的应用程序时,我都会收到此错误:

/Users/****/AndroidStudioProjects/DMSStemApp8/app/src/main/java/com/example/dmsstemapp8/ui/Events/EventsFragment.java:24: error: no suitable method found for get(Class<EventsViewModel>)
ViewModelProviders.of(this).get(EventsViewModel.class);
^
method ViewModelProvider.<T#1>get(Class<T#1>) is not applicable
(inferred type does not conform to upper bound(s)
inferred: EventsViewModel
upper bound(s): ViewModel)
method ViewModelProvider.<T#2>get(String,Class<T#2>) is not applicable
(cannot infer type-variable(s) T#2
(actual and formal argument lists differ in length))
where T#1,T#2 are type-variables:
T#1 extends ViewModel declared in method <T#1>get(Class<T#1>)
T#2 extends ViewModel declared in method <T#2>get(String,Class<T#2>)

这个错误本身很令人困惑,我找不到解决方案,所以这也是我的 java 代码

package com.example.dmsstemapp8.ui.Events;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.example.dmsstemapp8.R;

public class EventsFragment extends Fragment {

private EventsViewModel EventsViewModel;

public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
EventsViewModel =
ViewModelProviders.of(this).get(EventsViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
final TextView textView = root.findViewById(R.id.text_events);
EventsViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
textView.setText(s);
}
});
return root;
}
}

如果您需要其他任何东西来解决此问题,请询问

最佳答案

最终我只是做错了所有事情,当我在 android studio 页面上查找片段时,我发现这是有效的:

public class EventsFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_events, container, false);
}
}

关于ViewModelProviders.of(this).get(ViewModel.class) 出现 java 错误;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61872859/

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