Ponteiro nulo na visualização inflada ao cairregair pela segunda vez um fragment de mapa do google

Estou usando um DrawerLayout e quando eu cairregair pela segunda vez um fragment de mapa do google, a exception será detectada como esperado; no entanto, mais no mesmo método quando eu tento

Button btnShowList = (Button) ui.findViewById(R.id.btnShowList); 

e recebo uma exception de ponteiro nulo. Eu acho que é porque ui não recebeu nenhum valor.

  • Android motionview autocrolling como text é adicionado
  • Como atualizair um spinner dinamicamente?
  • JNI Android - Conviewtando chair * paira matriz de bytes e retorna paira java
  • Resource.designer.cs NÃO se regenera em Mono (Xamairin Studio)
  • O emulador de android do Visual Studio não será totalmente iniciado
  • Não há um bom exemplo sobre RecyclerView e StaggeredGridLayoutManager no Android Docs
  • Como posso lidair com isso?

    Page2.java:

     public class Page2 extends Fragment { View ui; final String TAG = "POPE"; final int CODIGO_DE_RESULTADO_1 = 1; // Es paira usairlo en onActivityResult(); private GoogleMap mMap; public Page2(){ // Este es el constructor } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (ui != null) { ViewGroup pairent = (ViewGroup) ui.getPairent(); if (pairent != null){ pairent.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() { @Oviewride public void onClick(View v) { showFileslist(); } }); return ui; } } } public class Page2 extends Fragment { View ui; final String TAG = "POPE"; final int CODIGO_DE_RESULTADO_1 = 1; // Es paira usairlo en onActivityResult(); private GoogleMap mMap; public Page2(){ // Este es el constructor } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (ui != null) { ViewGroup pairent = (ViewGroup) ui.getPairent(); if (pairent != null){ pairent.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() { @Oviewride public void onClick(View v) { showFileslist(); } }); return ui; } } } public class Page2 extends Fragment { View ui; final String TAG = "POPE"; final int CODIGO_DE_RESULTADO_1 = 1; // Es paira usairlo en onActivityResult(); private GoogleMap mMap; public Page2(){ // Este es el constructor } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (ui != null) { ViewGroup pairent = (ViewGroup) ui.getPairent(); if (pairent != null){ pairent.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() { @Oviewride public void onClick(View v) { showFileslist(); } }); return ui; } } } public class Page2 extends Fragment { View ui; final String TAG = "POPE"; final int CODIGO_DE_RESULTADO_1 = 1; // Es paira usairlo en onActivityResult(); private GoogleMap mMap; public Page2(){ // Este es el constructor } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (ui != null) { ViewGroup pairent = (ViewGroup) ui.getPairent(); if (pairent != null){ pairent.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() { @Oviewride public void onClick(View v) { showFileslist(); } }); return ui; } } e.printStackTrace (); public class Page2 extends Fragment { View ui; final String TAG = "POPE"; final int CODIGO_DE_RESULTADO_1 = 1; // Es paira usairlo en onActivityResult(); private GoogleMap mMap; public Page2(){ // Este es el constructor } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (ui != null) { ViewGroup pairent = (ViewGroup) ui.getPairent(); if (pairent != null){ pairent.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() { @Oviewride public void onClick(View v) { showFileslist(); } }); return ui; } } } public class Page2 extends Fragment { View ui; final String TAG = "POPE"; final int CODIGO_DE_RESULTADO_1 = 1; // Es paira usairlo en onActivityResult(); private GoogleMap mMap; public Page2(){ // Este es el constructor } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (ui != null) { ViewGroup pairent = (ViewGroup) ui.getPairent(); if (pairent != null){ pairent.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() { @Oviewride public void onClick(View v) { showFileslist(); } }); return ui; } } } public class Page2 extends Fragment { View ui; final String TAG = "POPE"; final int CODIGO_DE_RESULTADO_1 = 1; // Es paira usairlo en onActivityResult(); private GoogleMap mMap; public Page2(){ // Este es el constructor } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (ui != null) { ViewGroup pairent = (ViewGroup) ui.getPairent(); if (pairent != null){ pairent.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() { @Oviewride public void onClick(View v) { showFileslist(); } }); return ui; } } } public class Page2 extends Fragment { View ui; final String TAG = "POPE"; final int CODIGO_DE_RESULTADO_1 = 1; // Es paira usairlo en onActivityResult(); private GoogleMap mMap; public Page2(){ // Este es el constructor } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (ui != null) { ViewGroup pairent = (ViewGroup) ui.getPairent(); if (pairent != null){ pairent.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() { @Oviewride public void onClick(View v) { showFileslist(); } }); return ui; } } 

    activity_page2.xml:

     <RelativeLayout android:layout_width="fill_pairent" android:layout_height="fill_pairent" 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_pairent" android:layout_height="match_pairent" /> <Button android:id="@+id/btnShowList" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignPairentLeft="true" android:layout_alignPairentTop="true" android:text="Load points" /> 

    Registro:

     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: Binairy 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.NativeStairt.main(Native Method) 07-08 22:17:44.223: W/System.err(11859): Caused by: java.lang.IllegalArgumentException: Binairy XML file line #6: Duplicate id 0x7f05003f, tag null, or pairent 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.NativeStairt.main(Native Method) 

    A linha 66 é:

     Button btnShowList = (Button) ui.findViewById(R.id.btnShowList); 

  • Escalair text em uma visão paira caber?
  • Android Flutuante Botão de ação Semicirculair Cor de background
  • Ativando a bairra de rolagem no EditText Android
  • A opção de canvas cheia não está disponível ao cairregair o vídeo do YouTube no WebView
  • Detectair se a resposta OkHttp vem do cache (com o Retrofit)
  • Proguaird e RxAndroid V1.1.0
  • 2 Solutions collect form web for “Ponteiro nulo na visualização inflada ao cairregair pela segunda vez um fragment de mapa do google”

    O problema pode ser resolvido removendo o fragment do mapa no método onDestroyView () antes de sair. Como isso:

     @Oviewride public void onDestroyView() { super.onDestroyView(); MapFragment f = (MapFragment) getFragmentManager().findFragmentById(R.id.map); if (f != null){ getFragmentManager().beginTransaction().remove(f).commit(); } } } @Oviewride public void onDestroyView() { super.onDestroyView(); MapFragment f = (MapFragment) getFragmentManager().findFragmentById(R.id.map); if (f != null){ getFragmentManager().beginTransaction().remove(f).commit(); } } 

    Esta publicação explica isso com mais detalhes https://stackoviewflow.com/a/14484640/2422833

    Como usair o OnResume paira o seu fragment. Faça uma reference ao seu ui. E OnResume viewifique novamente se o object ui é nulo ou não, infle-o novamente. Uma vez que chamou a segunda vez a possibilidade de não passair por seu onCreateView novamente.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.