java - 我想从 viewpager 中的片段中获取 EditText 的所有值并想在活动中使用它们?
问题描述
下面是我想从中获取值的片段类,但它给了我错误。
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_register_company, container, false);
bindViews();
return rootView;
}
private void bindViews() {
companyName = rootView.findViewById(R.id.companyName);
compAddress1 = rootView.findViewById(R.id.compAddress1);
compAddress2 = rootView.findViewById(R.id.compAddress2);
compCountry = rootView.findViewById(R.id.compCountry);
compState = rootView.findViewById(R.id.compState);
compCity = rootView.findViewById(R.id.compCity);
compWebsite = rootView.findViewById(R.id.compWebsite);
compEmail = rootView.findViewById(R.id.compEmail);
compPhone = rootView.findViewById(R.id.compPhone);
}
public HashMap<String, String> getCompanyDetails() {
companyNameString = companyName.getText().toString();
compAddress1String = compAddress1.getText().toString();
compAddress2String = compAddress2.getText().toString();
compCountryString = compCountry.getText().toString();
compStateString = compState.getText().toString();
compCityString = compCity.getText().toString();
compWebsiteString = compWebsite.getText().toString();
compEmailString = compEmail.getText().toString();
compPhoneString = compPhone.getText().toString();
HashMap<String, String> map = new HashMap<>();
map.put("companyNameString", companyNameString);
map.put("compAddress1String", compAddress1String);
map.put("compAddress2String", compAddress2String);
map.put("compCountryString", compCountryString);
map.put("compStateString", compStateString);
map.put("compCityString", compCityString);
map.put("compWebsiteString", compWebsiteString);
map.put("compEmailString", compEmailString);
map.put("compPhoneString", compPhoneString);
return map;
}
这是我试图从中获取值的主要活动中的函数。
private void collectData() {
HashMap<String, String> compMap= new RegisterCompanyFragment().getCompanyDetails();
companyNameString = compMap.get("companyNameString");
compAddress1String = compMap.get("compAddress1String");
compAddress2String = compMap.get("compAddress2String");
compCountryString = compMap.get("compCountryString");
compStateString = compMap.get("compStateString");
compCityString = compMap.get("compCityString");
compWebsiteString = compMap.get("compWebsiteString");
compEmailString = compMap.get("compEmailString");
compPhoneString = compMap.get("compPhoneString");
}
这是错误,我猜这个错误是因为编辑文本没有绑定到视图,我仍然在尝试使用它。因此,请通过提供一种从片段中访问编辑文本值的方法来帮助我解决这个问题。欲了解更多信息,请在下面发表评论。谢谢你。
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.jagmad.jhea, PID: 30128
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.jagmad.jhea.Registration.RegisterCompanyFragment.getCompanyDetails(RegisterCompanyFragment.java:75)
at com.jagmad.jhea.Registration.CompanyRegistration.collectData(CompanyRegistration.java:186)
at com.jagmad.jhea.Registration.CompanyRegistration.onOptionsItemSelected(CompanyRegistration.java:139)
at android.app.Activity.onMenuItemSelected(Activity.java:3208)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:407)
at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195)
at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:108)
at android.support.v7.app.AppCompatDelegateImplV9.onMenuItemSelected(AppCompatDelegateImplV9.java:674)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:171)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:973)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:963)
at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:624)
at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:150)
at android.view.View.performClick(View.java:5612)
at android.view.View$PerformClick.run(View.java:22288)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
解决方案
推荐阅读
- javascript - jQuery FadeIn() 等不起作用,浏览器认为它们根本不起作用
- python - 如果发生异常,删除 JSON 文件
- go - 如何锁定地图并修复数据竞争
- php - 在文件名旁边添加图标
- javascript - React-Native 动态改变 fetch url
- python - 如何将模型导入我的控制台,以便我可以从我的数据库中查找它的实例?
- security - 密码中方法 update() 和 dofinal() 之间的区别
- c++ - Stack的链表实现
- php - PHP imap 移动和删除消息
- javascript - 如何强制 midori 不显示“UTC+1”?