gpt4 book ai didi

java - 在对话框 fragment 中使用观察者会导致对话框窗口第二次打开后程序崩溃

转载 作者:太空宇宙 更新时间:2023-11-04 09:04:20 26 4
gpt4 key购买 nike

我最近问了这个问题,但它有点困惑,所以我想我可以更好地包含所有信息,然后再试一次。

我想在常规 fragment 和对话框 fragment 之间共享数据,以实现此目的,我使用共享 View 模型。正如您从标题中看到的,我第二次打开对话框窗口时软件崩溃了。

这是主要 fragment ,它有一个按钮,单击该按钮会打开一个对话框 fragment 。

public class MainFragment extends Fragment {

private MainViewModel mViewModel;
private View fragmentView;
private SharedViewModel sharedViewModel;


public static MainFragment newInstance() {
return new MainFragment();
}

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
fragmentView = inflater.inflate(R.layout.main_fragment, container, false);
return fragmentView;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mViewModel = ViewModelProviders.of(this).get(MainViewModel.class);

sharedViewModel = new ViewModelProvider(requireActivity()).get(SharedViewModel.class);

Button aButton = fragmentView.findViewById(R.id.button);
aButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sharedViewModel.setColor("Green");
NavDirections action = MainFragmentDirections.actionMainFragmentToBlankFragment();
Navigation.findNavController(getView()).navigate(action);

}
});

}

}

这是当我单击按钮时弹出的对话框 fragment 的代码。

public class BlankFragment extends DialogFragment {
private View dialogView;
private SharedViewModel sharedViewModel;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater layoutInflater = requireActivity().getLayoutInflater();
dialogView = layoutInflater.inflate(R.layout.blank_fragment, null);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(dialogView);

return builder.create();
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
sharedViewModel = new ViewModelProvider(requireActivity()).
get(SharedViewModel.class); //gets the shared view model from the associsated fragment.
MutableLiveData<String> tableColor = sharedViewModel.getColor();
tableColor.observe(getParentFragment().getViewLifecycleOwner(), new Observer<String>() {
@Override
public void onChanged(String s) {
Toast.makeText(getActivity(), "IF I GET HERE TWICE I DIE", Toast.LENGTH_SHORT).show();
}

});
}
}

正如您可能已经看到的,我使用导航图在它们之间导航。

共享 View 模型如下所示

public class SharedViewModel extends ViewModel {
private MutableLiveData<String> color = new MutableLiveData<>();


public void setColor(String color){
this.color.setValue(color);
}
public MutableLiveData<String> getColor(){
return color;
}

}

错误消息如下:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.dialogfragmentcrashing, PID: 32620 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference at android.widget.Toast.(Toast.java:121) at android.widget.Toast.makeText(Toast.java:286) at android.widget.Toast.makeText(Toast.java:276) at com.example.dialogfragmentcrashing.BlankFragment$1.onChanged(BlankFragment.java:47) at com.example.dialogfragmentcrashing.BlankFragment$1.onChanged(BlankFragment.java:44) at androidx.lifecycle.LiveData.considerNotify(LiveData.java:131) at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:149) at androidx.lifecycle.LiveData.setValue(LiveData.java:307) at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:50) at com.example.dialogfragmentcrashing.SharedViewModel.setColor(SharedViewModel.java:11) at com.example.dialogfragmentcrashing.MainFragment$1.onClick(MainFragment.java:49) at android.view.View.performClick(View.java:7155) at android.view.View.performClickInternal(View.java:7124) at android.view.View.access$3500(View.java:808) at android.view.View$PerformClick.run(View.java:27370) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:359) at android.app.ActivityThread.main(ActivityThread.java:7418) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)

最佳答案

getParentFragment().getViewLifecycleOwner() 始终是错误的 LifecycleOwner,因为它在 DialogFragment 被销毁时不会被销毁。将所有代码移至 onCreate() 并使用 this:

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedViewModel = new ViewModelProvider(requireActivity()).
get(SharedViewModel.class); //gets the shared view model from the associsated fragment.
MutableLiveData<String> tableColor = sharedViewModel.getColor();
tableColor.observe(this, new Observer<String>() {
@Override
public void onChanged(String s) {
Toast.makeText(getActivity(), "IF I GET HERE TWICE I DIE", Toast.LENGTH_SHORT).show();
}

});
}

关于java - 在对话框 fragment 中使用观察者会导致对话框窗口第二次打开后程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60419492/

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