如何从片段中获取 getArguments 数据?
How to get the getArguments data from a fragment?
我正在为我的 application.and 使用一个片段,我还在我的片段中添加了一个包,但是当我想从包中获取添加的字符串时,编译器向我显示一个空指针错误,如下所示
Process: com.softmaker.gazi_opu.personalhealthcare, PID: 3253
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
at com.softmaker.gazi_opu.personalhealthcare.fragment.GeneralInformationFragment.setValue(GeneralInformationFragment.java:67)
at com.softmaker.gazi_opu.personalhealthcare.fragment.GeneralInformationFragment.onCreateView(GeneralInformationFragment.java:59)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:458)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
下面给出了我添加的代码和获取添加的数据代码
public static GeneralInformationFragment getInstance(String profileID){
Bundle bundle = new Bundle();
bundle.putString("profileId",profileID);
GeneralInformationFragment generalFragment = new GeneralInformationFragment();
generalFragment.setArguments(bundle);
return generalFragment;
}
现在从 onCreateMethod 中的这个片段获取数据
String id = getArguments().getString("profileId");
Log.e("ID :", "" + id.toString());
谁能推荐我一下。我哪里弄错了?
总是在转换为 String 之前检查是否为 null
String id = getArguments().getString("profileId");
if(id!=null){
Log.e("ID :", "" + id.toString());
}
我总是这样传递数据并且工作完美:
public class CategoryResultFragment extends BaseListFragment {
private static final String CATEGORY_ID = "EXTRA_CATEGORY_ID";
private static final String CATEGORY_SLUG = "EXTRA_CATEGORY_SLUG";
private String mCategoryId;
private String mCategorySlug;
public static CategoryResultFragment newInstance(String categoryId, String categorySlug) {
Bundle bundle = new Bundle();
bundle.putString(CATEGORY_ID, categoryId);
bundle.putString(CATEGORY_SLUG, categorySlug);
CategoryResultFragment categoryResultFragment = new CategoryResultFragment();
categoryResultFragment.setArguments(bundle);
return categoryResultFragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (getArguments() != null) {
mCategoryId = getArguments().getString(CATEGORY_ID);
mCategorySlug = getArguments().getString(CATEGORY_SLUG);
}
return super.onCreateView(inflater, container, savedInstanceState);
}
}
我正在为我的 application.and 使用一个片段,我还在我的片段中添加了一个包,但是当我想从包中获取添加的字符串时,编译器向我显示一个空指针错误,如下所示
Process: com.softmaker.gazi_opu.personalhealthcare, PID: 3253
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
at com.softmaker.gazi_opu.personalhealthcare.fragment.GeneralInformationFragment.setValue(GeneralInformationFragment.java:67)
at com.softmaker.gazi_opu.personalhealthcare.fragment.GeneralInformationFragment.onCreateView(GeneralInformationFragment.java:59)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:458)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
下面给出了我添加的代码和获取添加的数据代码
public static GeneralInformationFragment getInstance(String profileID){
Bundle bundle = new Bundle();
bundle.putString("profileId",profileID);
GeneralInformationFragment generalFragment = new GeneralInformationFragment();
generalFragment.setArguments(bundle);
return generalFragment;
}
现在从 onCreateMethod 中的这个片段获取数据
String id = getArguments().getString("profileId");
Log.e("ID :", "" + id.toString());
谁能推荐我一下。我哪里弄错了?
总是在转换为 String 之前检查是否为 null
String id = getArguments().getString("profileId");
if(id!=null){
Log.e("ID :", "" + id.toString());
}
我总是这样传递数据并且工作完美:
public class CategoryResultFragment extends BaseListFragment {
private static final String CATEGORY_ID = "EXTRA_CATEGORY_ID";
private static final String CATEGORY_SLUG = "EXTRA_CATEGORY_SLUG";
private String mCategoryId;
private String mCategorySlug;
public static CategoryResultFragment newInstance(String categoryId, String categorySlug) {
Bundle bundle = new Bundle();
bundle.putString(CATEGORY_ID, categoryId);
bundle.putString(CATEGORY_SLUG, categorySlug);
CategoryResultFragment categoryResultFragment = new CategoryResultFragment();
categoryResultFragment.setArguments(bundle);
return categoryResultFragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (getArguments() != null) {
mCategoryId = getArguments().getString(CATEGORY_ID);
mCategorySlug = getArguments().getString(CATEGORY_SLUG);
}
return super.onCreateView(inflater, container, savedInstanceState);
}
}