英文:
Can't access the Fragment View's LifecycleOwner when getView() is null
问题
以下是你要翻译的内容:
我正在尝试从我的ROOM数据库内部观察一个包含所有锻炼名称的列表,并显示在AlertDialog
中。
然而,我收到了以下错误信息:
java.lang.NullPointerException: 尝试调用虚拟方法 'android.app.Application android.app.Activity.getApplication()',但空对象引用
错误消息出现在这里:
childExerciseViewModel = ViewModelProviders.of((FragmentActivity) context).get(ChildExerciseViewModel.class);
我该如何避免这个错误?
AddExerciseDialog
public class AddExerciseDialog extends AppCompatDialogFragment {
private EditText editTextExerciseName;
private ChildExerciseViewModel childExerciseViewModel;
String userEnteredExerciseName;
int exerciseTypeID;
Button cancelBtn;
Button addBtn;
Context context;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
childExerciseViewModel = ViewModelProviders.of((FragmentActivity) context).get(ChildExerciseViewModel.class);
//return view;
return inflater.inflate(R.layout.layout_dialog_add_exercise, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
editTextExerciseName = getView().findViewById(R.id.editTextExerciseName);
addBtn = (Button) getView().findViewById(R.id.addBtn);
cancelBtn = (Button) getView().findViewById(R.id.cancelBtn);
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bundle mArgs = getArguments();
exerciseTypeID = mArgs.getInt("ExerciseTypeID");
userEnteredExerciseName = editTextExerciseName.getText().toString();
childExerciseViewModel.getAllChildExerciseNames().observe((FragmentActivity) context, this::checkIfChildExerciseNameAlreadyExists);
// this checks if child exercise already exists, if so add error toast
}
private void checkIfChildExerciseNameAlreadyExists(List<String> childExercisesList) {
for (int i = 0; i < childExercisesList.size(); i++) {
if (childExercisesList.contains(userEnteredExerciseName)) {
Toast.makeText(getContext(), "An exercise with that name already exists", Toast.LENGTH_SHORT).show();
} else {
final ChildExercises childExercise = new ChildExercises(0, userEnteredExerciseName, null, exerciseTypeID);
childExerciseViewModel.insert(childExercise);
}
}
}
});
}
}
英文:
I am trying to observe a list of all of the exercise names from my ROOM database inside my AlertDialog
.
However I am getting the following error message:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.Application android.app.Activity.getApplication()' on a null object reference
The error message is occurring here:
childExerciseViewModel = ViewModelProviders.of((FragmentActivity) context).get(ChildExerciseViewModel.class);
How can I avoid the error?
AddExerciseDialog
public class AddExerciseDialog extends AppCompatDialogFragment {
private EditText editTextExerciseName;
private ChildExerciseViewModel childExerciseViewModel;
String userEnteredExerciseName;
int exerciseTypeID;
Button cancelBtn;
Button addBtn;
Context context;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
childExerciseViewModel = ViewModelProviders.of((FragmentActivity) context).get(ChildExerciseViewModel.class);
//return view;
return inflater.inflate(R.layout.layout_dialog_add_exercise, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
editTextExerciseName = getView().findViewById(R.id.editTextExerciseName);
addBtn = (Button) getView().findViewById(R.id.addBtn);
cancelBtn = (Button) getView().findViewById(R.id.cancelBtn);
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bundle mArgs = getArguments();
exerciseTypeID = mArgs.getInt("ExerciseTypeID");
userEnteredExerciseName = editTextExerciseName.getText().toString();
childExerciseViewModel.getAllChildExerciseNames().observe((FragmentActivity) context, this::checkIfChildExerciseNameAlreadyExists);
// this checks if child exercise already exists, if so add error toast
}
private void checkIfChildExerciseNameAlreadyExists(List<String> childExercisesList) {
for (int i = 0; i < childExercisesList.size(); i++) {
if (childExercisesList.contains(userEnteredExerciseName)) {
Toast.makeText(getContext(), "An exercise with that name already exists", Toast.LENGTH_SHORT).show();
} else {
final ChildExercises childExercise = new ChildExercises(0, userEnteredExerciseName, null, exerciseTypeID);
childExerciseViewModel.insert(childExercise);
}
}
}
});
}
}
答案1
得分: 12
尝试这样做:
使用 this
代替 getViewLifecycleOwner()
英文:
Try this:
use this
intead of getViewLifecycleOwner()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论