我正在使用DrawerLayout,当我第二次加载谷歌地图片段时,异常将按预期捕获;但是,当我尝试
时,进一步采用相同的方法Button btnShowList = (Button) ui.findViewById(R.id.btnShowList);
我得到一个空指针异常。我想这是因为ui
没有分配任何值。
我该如何处理?
Page2.java:
public class Page2 extends Fragment {
View ui;
final String TAG = "POPE";
final int CODIGO_DE_RESULTADO_1 = 1; // Es para usarlo en onActivityResult();
private GoogleMap mMap;
public Page2(){
// Este es el constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (ui != null) {
ViewGroup parent = (ViewGroup) ui.getParent();
if (parent != null){
parent.removeView(ui);
}
}
try {
ui = inflater.inflate(R.layout.activity_page2,container,false);
} catch (InflateException e) {
Log.i(TAG,"HERE COMES THE STACK TRACE");
e.printStackTrace();
}
FragmentManager fm = getFragmentManager();
mMap = ((MapFragment) fm.findFragmentById(R.id.map)).getMap();
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMap.setMyLocationEnabled(true);
Button btnShowList = (Button) ui.findViewById(R.id.btnShowList);
btnShowList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showFileslist();
}
});
return ui;
}
}
activity_page2.xml:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/btnShowList"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Load points" />
日志:
07-08 22:17:44.203: I/POPE(11859): HERE COMES THE STACK TRACE
07-08 22:17:44.213: W/System.err(11859): android.view.InflateException: Binary XML file line #6: Error inflating class fragment
07-08 22:17:44.213: W/System.err(11859): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
07-08 22:17:44.213: W/System.err(11859): at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
07-08 22:17:44.213: W/System.err(11859): at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
07-08 22:17:44.213: W/System.err(11859): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
07-08 22:17:44.213: W/System.err(11859): at com.jorgeandresserrano.gpstools.Page2.onCreateView(Page2.java:52)
07-08 22:17:44.213: W/System.err(11859): at android.app.Fragment.performCreateView(Fragment.java:1695)
07-08 22:17:44.213: W/System.err(11859): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
07-08 22:17:44.213: W/System.err(11859): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
07-08 22:17:44.213: W/System.err(11859): at android.app.BackStackRecord.run(BackStackRecord.java:682)
07-08 22:17:44.213: W/System.err(11859): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1455)
07-08 22:17:44.223: W/System.err(11859): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
07-08 22:17:44.223: W/System.err(11859): at android.os.Handler.handleCallback(Handler.java:725)
07-08 22:17:44.223: W/System.err(11859): at android.os.Handler.dispatchMessage(Handler.java:92)
07-08 22:17:44.223: W/System.err(11859): at android.os.Looper.loop(Looper.java:137)
07-08 22:17:44.223: W/System.err(11859): at android.app.ActivityThread.main(ActivityThread.java:5293)
07-08 22:17:44.223: W/System.err(11859): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 22:17:44.223: W/System.err(11859): at java.lang.reflect.Method.invoke(Method.java:511)
07-08 22:17:44.223: W/System.err(11859): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-08 22:17:44.223: W/System.err(11859): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-08 22:17:44.223: W/System.err(11859): at dalvik.system.NativeStart.main(Native Method)
07-08 22:17:44.223: W/System.err(11859): Caused by: java.lang.IllegalArgumentException: Binary XML file line #6: Duplicate id 0x7f05003f, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment
07-08 22:17:44.223: W/System.err(11859): at android.app.Activity.onCreateView(Activity.java:4863)
07-08 22:17:44.233: W/System.err(11859): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:686)
07-08 22:17:44.233: W/System.err(11859): ... 19 more
07-08 22:17:44.233: D/AndroidRuntime(11859): Shutting down VM
07-08 22:17:44.233: W/dalvikvm(11859): threadid=1: thread exiting with uncaught exception (group=0x410feac8)
07-08 22:17:44.233: E/AndroidRuntime(11859): FATAL EXCEPTION: main
07-08 22:17:44.233: E/AndroidRuntime(11859): java.lang.NullPointerException
07-08 22:17:44.233: E/AndroidRuntime(11859): at com.myname.myapp.Page2.onCreateView(Page2.java:66)
07-08 22:17:44.233: E/AndroidRuntime(11859): at android.app.Fragment.performCreateView(Fragment.java:1695)
07-08 22:17:44.233: E/AndroidRuntime(11859): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
07-08 22:17:44.233: E/AndroidRuntime(11859): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
07-08 22:17:44.233: E/AndroidRuntime(11859): at android.app.BackStackRecord.run(BackStackRecord.java:682)
07-08 22:17:44.233: E/AndroidRuntime(11859): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1455)
07-08 22:17:44.233: E/AndroidRuntime(11859): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
07-08 22:17:44.233: E/AndroidRuntime(11859): at android.os.Handler.handleCallback(Handler.java:725)
07-08 22:17:44.233: E/AndroidRuntime(11859): at android.os.Handler.dispatchMessage(Handler.java:92)
07-08 22:17:44.233: E/AndroidRuntime(11859): at android.os.Looper.loop(Looper.java:137)
07-08 22:17:44.233: E/AndroidRuntime(11859): at android.app.ActivityThread.main(ActivityThread.java:5293)
07-08 22:17:44.233: E/AndroidRuntime(11859): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 22:17:44.233: E/AndroidRuntime(11859): at java.lang.reflect.Method.invoke(Method.java:511)
07-08 22:17:44.233: E/AndroidRuntime(11859): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-08 22:17:44.233: E/AndroidRuntime(11859): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-08 22:17:44.233: E/AndroidRuntime(11859): at dalvik.system.NativeStart.main(Native Method)
第66行是:
Button btnShowList = (Button) ui.findViewById(R.id.btnShowList);
答案 0 :(得分:23)
可以通过在离开前删除onDestroyView()方法中的地图片段来解决问题。像这样:
@Override
public void onDestroyView() {
super.onDestroyView();
MapFragment f = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
if (f != null){
getFragmentManager().beginTransaction().remove(f).commit();
}
}
这篇文章详细解释了这一点https://stackoverflow.com/a/14484640/2422833
答案 1 :(得分:0)
如何为您的片段使用OnResume
。引用你的ui。并且OnResume再次检查ui对象是否为null然后再次膨胀它。因为它第二次调用它可能不会再次通过你的onCreateView。