目前这是我的活动类
Fragment fragment = null;
switch (position) {
case 0:
fragment = new FragmentTripPlan();
break;
case 1:
fragment = new FragmentCulturalHistorical();
Toast.makeText(this, "cultural", Toast.LENGTH_SHORT).show();
break;
case 2:
fragment = new FragmentLeisureRecreational();
Toast.makeText(this, "Leisure", Toast.LENGTH_SHORT).show();
break;
case 3:
fragment = new FragmentLipaChurches();
break;
case 4:
fragment = new FragmentNaturalSite();
Bundle bundle = new Bundle();
bundle.putString("edttext", "From Activity");
FragmentNaturalSite fragobj = new FragmentNaturalSite();
fragobj.setArguments(bundle);
break;
case 5:
fragment = new FragmentCreateReport();
break;
case 6:
fragment = new FragmentAbout();
break;
default:
break;
这是我的片段类
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_lipa_churches, container, false);
// ******* vvvvvv
Bundle bundle = getArguments();
String strtext = bundle .getString("edttext");
return rootView;
}
我在日志跟踪
中遇到了错误01-10 13:51:43.020: E/AndroidRuntime(13708): FATAL EXCEPTION: main
01-10 13:51:43.020: E/AndroidRuntime(13708): java.lang.NullPointerException
01-10 13:51:43.020: E/AndroidRuntime(13708): at com.c4a.itravellipaph.FragmentLipaChurches.onCreateView(FragmentLipaChurches.java:26)
01-10 13:51:43.020: E/AndroidRuntime(13708): at android.app.Fragment.performCreateView(Fragment.java:1695)
01-10 13:51:43.020: E/AndroidRuntime(13708): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
01-10 13:51:43.020: E/AndroidRuntime(13708): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
01-10 13:51:43.020: E/AndroidRuntime(13708): at android.app.BackStackRecord.run(BackStackRecord.java:682)
01-10 13:51:43.020: E/AndroidRuntime(13708): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1455)
01-10 13:51:43.020: E/AndroidRuntime(13708): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
01-10 13:51:43.020: E/AndroidRuntime(13708): at android.os.Handler.handleCallback(Handler.java:725)
01-10 13:51:43.020: E/AndroidRuntime(13708): at android.os.Handler.dispatchMessage(Handler.java:92)
01-10 13:51:43.020: E/AndroidRuntime(13708): at android.os.Looper.loop(Looper.java:137)
01-10 13:51:43.020: E/AndroidRuntime(13708): at android.app.ActivityThread.main(ActivityThread.java:5279)
01-10 13:51:43.020: E/AndroidRuntime(13708): at java.lang.reflect.Method.invokeNative(Native Method)
01-10 13:51:43.020: E/AndroidRuntime(13708): at java.lang.reflect.Method.invoke(Method.java:511)
01-10 13:51:43.020: E/AndroidRuntime(13708): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
01-10 13:51:43.020: E/AndroidRuntime(13708): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
01-10 13:51:43.020: E/AndroidRuntime(13708): at dalvik.system.NativeStart.main(Native Method)
问题。
我该如何解决这个问题?
如何将活动中的值或数据传递给片段?
还有其他方式传递价值吗?
答案 0 :(得分:1)
我不知道谁是fragobj
,但在case 4
我认为你有错字,而不是:
FragmentLipaChurches fragobj = new FragmentLipaChurches();
fragobj.setArguments(bundle);
你应该:
fragment.setArguments(bundle);
至少FragmentLipaChurches
应该以不同的方式处理,因为您正在创建FragmentNaturalSite
的实例,但是您要将参数设置为FragmentLipaChurches
。所以FragmentNaturalSite
没有设置参数,这就是NPE的原因。
答案 1 :(得分:1)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle b = getActivity().getIntent().getExtras();
String strtext = getArguments().getString("edttext");
View view = inflater.inflate(R.layout.categoryfragment, container, false);
return view;
}
答案 2 :(得分:0)
从活动传递值使用此
Bundle bun = new Bundle();
bun.putString("pathimage", path);
fragment_setting.setArguments(bun);
获取值在片段
中的onCreateView中使用它Bundle b = getArguments();
String path = b.getString("pathimage");