无法在getView()为null时访问Fragment View的LifecycleOwner。

huangapple go评论90阅读模式
英文:

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 &#39;android.app.Application android.app.Activity.getApplication()&#39; 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(&quot;ExerciseTypeID&quot;);
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&lt;String&gt; childExercisesList) {
for (int i = 0; i &lt; childExercisesList.size(); i++) {
if (childExercisesList.contains(userEnteredExerciseName)) {
Toast.makeText(getContext(), &quot;An exercise with that name already exists&quot;, 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()

huangapple
  • 本文由 发表于 2020年8月25日 19:39:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63578050.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定