Dagger2 在调用 ViewModel 的 setText 上崩溃
Dagger2 crashing on setText calling ViewModel
应用程序构建但在 运行 时因空对象引用而崩溃。一直在尝试调试它但找不到错误,但我知道如果我删除 textview 上的 setText 调用一个方法来检索一个字符串,然后通过一个片段显示在 MainActivity 中(如果我删除它不会崩溃)这让我相信我的空对象与我没有实例化视图模型的方式有关。不确定如何执行此操作。
尝试从 stacktrace 中删除和定位 bufg,它指出空对象引用在片段的 onActivityCreated 中,但 Dagger 生成代码,这是我迷路的地方,就好像我没有 运行 它构建和显示没有编译时错误。
public class HomeFragment extends Fragment {
@Inject
HomeViewModel homeViewModel;
TextView tvFrag;
public HomeFragment() {
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.fragment_home, container, false);
return v;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
tvFrag = getActivity().findViewById(R.id.tv_frag);
tvFrag.setText(homeViewModel.getConnection()); //HERE
}
}
@FragmentScope
public class HomeViewModel extends ViewModel {
private DatabaseService databaseService;
private NetworkService networkService;
private NetworkHelper networkHelper;
@Inject
public HomeViewModel(DatabaseService ds, NetworkService ns, NetworkHelper nh){
this.databaseService = ds;
this.networkService = ns;
this.networkHelper = nh;
}
public String getConnection(){
return "Ben Mohammad Connected";
}
}
GitHub Link - https://github.com/BenMohammad/LearnDagger-STAGE_2-with-dagger-better
应调用 viewModel 中的方法 getConnection() 并检索字符串并显示在已添加到 MainActivity 的片段上。
谢谢
您试图在父 Activity 中查找 tv_frag
而不是当前片段。
更改为:
tvFrag = getView().findViewById(R.id.tv_frag);
tvFrag.setText(homeViewModel.getConnection());
您没有在片段中注入 HomeViewModel
。
尝试在 onCreate
方法中使用 AndroidSupportInjection.inject(this);
。
方法应该是这样的
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidSupportInjection.inject(this);
}
您尚未在该视图中执行依赖项注入。可能是:
AndroidSupportInjection.inject(this);
或:
((YourApplicationClass) getActivity().getApplication()).getApplicationComponent().inject(this);
(如果您没有将 Dagger 用于 Android ),在您的 onCreate
方法中。
在那种情况下,Dagger 会知道它有一些依赖项要注入到该片段中,在您的例子中是 HomeViewModel
。否则 HomeViewModel
仍然为空,直到您执行 DI
好的,我认为答案(包括我的)比你更让你困惑。
ViewModel 必须像这样初始化(我不知道你的模块中有什么,但这是正确的方法):在 onCreate
方法中:
HomeViewModel homeviewModel = ViewModelProviders.of(this).get(HomeViewModel.class);
如果您需要将依赖项传递给您的 viewmodel,请使用 ViewModel Factory。 Google 你自己或参考 this link
应用程序构建但在 运行 时因空对象引用而崩溃。一直在尝试调试它但找不到错误,但我知道如果我删除 textview 上的 setText 调用一个方法来检索一个字符串,然后通过一个片段显示在 MainActivity 中(如果我删除它不会崩溃)这让我相信我的空对象与我没有实例化视图模型的方式有关。不确定如何执行此操作。
尝试从 stacktrace 中删除和定位 bufg,它指出空对象引用在片段的 onActivityCreated 中,但 Dagger 生成代码,这是我迷路的地方,就好像我没有 运行 它构建和显示没有编译时错误。
public class HomeFragment extends Fragment {
@Inject
HomeViewModel homeViewModel;
TextView tvFrag;
public HomeFragment() {
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.fragment_home, container, false);
return v;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
tvFrag = getActivity().findViewById(R.id.tv_frag);
tvFrag.setText(homeViewModel.getConnection()); //HERE
}
}
@FragmentScope
public class HomeViewModel extends ViewModel {
private DatabaseService databaseService;
private NetworkService networkService;
private NetworkHelper networkHelper;
@Inject
public HomeViewModel(DatabaseService ds, NetworkService ns, NetworkHelper nh){
this.databaseService = ds;
this.networkService = ns;
this.networkHelper = nh;
}
public String getConnection(){
return "Ben Mohammad Connected";
}
}
GitHub Link - https://github.com/BenMohammad/LearnDagger-STAGE_2-with-dagger-better
应调用 viewModel 中的方法 getConnection() 并检索字符串并显示在已添加到 MainActivity 的片段上。
谢谢
您试图在父 Activity 中查找 tv_frag
而不是当前片段。
更改为:
tvFrag = getView().findViewById(R.id.tv_frag);
tvFrag.setText(homeViewModel.getConnection());
您没有在片段中注入 HomeViewModel
。
尝试在 onCreate
方法中使用 AndroidSupportInjection.inject(this);
。
方法应该是这样的
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidSupportInjection.inject(this);
}
您尚未在该视图中执行依赖项注入。可能是:
AndroidSupportInjection.inject(this);
或:
((YourApplicationClass) getActivity().getApplication()).getApplicationComponent().inject(this);
(如果您没有将 Dagger 用于 Android ),在您的 onCreate
方法中。
在那种情况下,Dagger 会知道它有一些依赖项要注入到该片段中,在您的例子中是 HomeViewModel
。否则 HomeViewModel
仍然为空,直到您执行 DI
好的,我认为答案(包括我的)比你更让你困惑。
ViewModel 必须像这样初始化(我不知道你的模块中有什么,但这是正确的方法):在 onCreate
方法中:
HomeViewModel homeviewModel = ViewModelProviders.of(this).get(HomeViewModel.class);
如果您需要将依赖项传递给您的 viewmodel,请使用 ViewModel Factory。 Google 你自己或参考 this link