My application has four fragments. When I running the application first time, everything is OK, but when jump back from other fragment, the logcat said "Nullpointerexception". Because I need use map at two different fragment, so I remove R.id.patrol_map_fragment
map at onDestroyView()
.
This is all Log :
E/AndroidRuntime(5541): FATAL EXCEPTION: main
E/AndroidRuntime(5541): java.lang.NullPointerException
E/AndroidRuntime(5541): at com.mt.fragment.PatrolFragment.findView(PatrolFragment.java:84)
E/AndroidRuntime(5541): at com.mt.fragment.PatrolFragment.onCreateView(PatrolFragment.java:69)
E/AndroidRuntime(5541): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
E/AndroidRuntime(5541): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
E/AndroidRuntime(5541): at android.app.BackStackRecord.run(BackStackRecord.java:635)
E/AndroidRuntime(5541): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1404)
E/AndroidRuntime(5541): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
E/AndroidRuntime(5541): at android.os.Handler.handleCallback(Handler.java:615)
E/AndroidRuntime(5541): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(5541): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(5541): at android.app.ActivityThread.main(ActivityThread.java:4914)
E/AndroidRuntime(5541): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(5541): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(5541): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:808)
E/AndroidRuntime(5541): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
E/AndroidRuntime(5541): at dalvik.system.NativeStart.main(Native Method)
PatrolFrgament.java:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getActivity();
if (mBaseView != null) {
ViewGroup parent = (ViewGroup) mBaseView.getParent();
if (parent != null)
parent.removeView(mBaseView);
}
try {
mBaseView= inflater.inflate(R.layout.fragment_patrol, container, false);
} catch (InflateException e) {
}
findView();
init();
return mBaseView;
}
private void findView() {
// TODO Auto-generated method stub
FragmentManager manager = getActivity().getFragmentManager();
Fragment f = manager.findFragmentById(R.id.map_fragment);
View view = f.getView();
mTitleBarView = (TitleBarView) view.findViewById(R.id.title_bar);
btn_patrol_camera = (Button) mTitleBarView
.findViewById(R.id.title_btn_right);
//*** This is the ERROR line ***/
minfoView = (View) mBaseView.findViewById(R.id.patrol_currinfo);
rl_patrol_type = (RelativeLayout) mBaseView
.findViewById(R.id.rl_patrol_type);
rl_patrolInfo_Normal = (RelativeLayout) minfoView
.findViewById(R.id.trl11);
rl_patrolInfo_Warning = (RelativeLayout) minfoView
.findViewById(R.id.trl12);
rl_patrolInfo_NoTax = (RelativeLayout) minfoView
.findViewById(R.id.trl21);
rl_patrolInfo_NoCert = (RelativeLayout) minfoView
.findViewById(R.id.trl22);
spinner = (Spinner) mBaseView
.findViewById(R.id.Spinner_patrol_mapRadius);
}
... ...
@Override
public void onDestroyView() {
// TODO Auto-generated method stub
super.onDestroyView();
Map f = (Map) getFragmentManager().findFragmentById(R.id.patrol_map_fragment);
if (f != null)
getFragmentManager().beginTransaction().remove(f).commit();
}
This is my fragment_patrol.xml layout file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="vertical"
android:background="@color/whites">
<RelativeLayout
android:id="@+id/rl_patrol_map"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5" >
<fragment
android:id="@+id/map_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.mt.patrol.Map" />
</RelativeLayout>
... ...
<include
android:id="@+id/patrol_currinfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ll_patrol_devide"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
layout="@layout/patrol_currinfo" />
</LinearLayout>
--- Update --- I have tried to delete try.catch{} block at onCreateView()
, direct write this:
mBaseView = inflater.inflate(R.layout.fragment_patrol, container,false);
But the error is Binary XML file line #15: Error inflating class fragment , that is this at layout XML file.
<fragment
android:id="@+id/map_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.mt.patrol.Map" />
I can't comment so I'll write it here.
I was getting this after updating to com.android.support:appcompat-v7:22.0.0
.
I'm not entirely sure what changed in the new appcompat
to make this happen but this is what I did to fix it.
I changed my
((ViewGroup) mBaseView).removeView(mBaseView);
to
container.removeView(view);
I also tried removing the line altogether and that worked too.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.