我正在构建一个想要在MainActivity中显示bottomSheetFragment的应用程序。由于我正在该片段中显示一些数据(用户帐户详细信息等),因此我决定实现这一目标的最佳方法是使用modelView。
因此,我在主活动中设置数据,如下所示:
@Override
protected void onStart() {
// Initialize view model for bottom menu.
bottomMenuData = ViewModelProviders.of(this).get(BottomMenuData.class);
bottomMenuData.setGoogleSignInAccount(CurrentGmailAccount);
...
}
现在,我观察到我的bottomSheetFragment中的值发生了变化,如下所示:
// Observe google account.
bottomMenuData.getGoogleSignInAccount().observe(getViewLifecycleOwner(), googleSignInAccount -> {
// Load image.
Glide.with(account_image)
.load(googleSignInAccount.getPhotoUrl())
.apply(new RequestOptions()
.placeholder(R.drawable.sample_image_profile))
.into(account_image);
// Set other account details.
account_name.setText(googleSignInAccount.getDisplayName());
account_email.setText(googleSignInAccount.getEmail());
});
我尝试像这样初始化viewModel:
@Override
public void onAttach(@NonNull Context context) {
bottomMenuData = ViewModelProviders.of(getActivity()).get(BottomMenuData.class);
super.onAttach(context);
}
问题是我在这里遇到异常,说我尝试在onCreateView()之前进行活动。片段的生命周期表示onAttach在onCreateView()之后运行。我也尝试初始化viewModel onViewCreated(),但是代码从未在那里执行!我已经阅读了几乎整个互联网,但是无法正常工作!
感谢您的帮助!
编辑: 这是我的viewModel的源代码:
public class DataModel extends ViewModel {
/** The current gmail account. */
private MutableLiveData<GoogleSignInAccount> currentGmailAccount = new MutableLiveData<>();
/** List containing messages history. */
private MutableLiveData<List<MsgModel>> messagesList = new MutableLiveData<>();
/** Listener to invoke callbacks for item clicks. */
private MutableLiveData<SettingsListener> settingsListener = new MutableLiveData<>();
/** Defines if it is the first run (first onCreate). */
boolean firstRun = true;
/**
* Add a new message to messages list.
* @param message Message to add.
*/
void AddMessage(MsgModel message) {
// Make sure list is initialized.
if (messagesList.getValue() == null) messagesList.setValue(new ArrayList<>());
// Add new message.
messagesList.getValue().add(message);
}
/**
* Defines how many messages are in the messages list.
* @return The count of the messages in the messages list.
*/
int MessagesSize() {
// Make sure list is initialized.
if (messagesList.getValue() == null) messagesList.setValue(new ArrayList<>());
return messagesList.getValue().size();
}
/**
* Get current gmail account as set from {@Link MainActivity}.
* @return Current gmail account. May be null if requested before set.
*/
GoogleSignInAccount GetCurrentGmailAccount() {
return currentGmailAccount.getValue();
}
@NonNull
public MutableLiveData<GoogleSignInAccount> GetGoogleSignInAccount() {
return currentGmailAccount;
}
void SetGoogleSignInAccount(GoogleSignInAccount googleSignInAccount) {
this.currentGmailAccount.setValue(googleSignInAccount);
}
@NonNull
public SettingsListener GetSettingsListener() {
return settingsListener.getValue();
}
void SetSettingsListener(SettingsListener settingsListener) {
this.settingsListener.setValue(settingsListener);
}
@NonNull
MutableLiveData<List<MsgModel>> GetMessagesList() {
// Make sure list is initialized.
if (messagesList.getValue() == null) messagesList.setValue(new ArrayList<>());
return messagesList;
}
}
答案 0 :(得分:1)
getActivity()在您的片段正在准备中并且即将准备就绪时可以为null。将您的ViewModel参考/实例创建移动到[onActivityCreated] [1]
void onActivityCreated (Bundle savedInstanceState) {
bottomMenuData =
ViewModelProviders.of(getActivity()).get(BottomMenuData.class);
}