2016-12-22 12 views
1

Пожалуйста, любой один помочь мне, я новичок в Android Studio, мое приложение получает сбой при резюме приложения,приложение получает сбой при резюме приложение

У меня есть три различных макета фрагмента, которые имеют

<fragment 
    android:id="@+id/map_online" 
    android:name="com.google.android.gms.maps.SupportMapFragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@+id/action_bar" /> with different ID. 

время резюме приложения мне нужно загрузить один фрагмент с картой,

Я получаю следующее сообщение об ошибке:

Caused by: java.lang.IllegalArgumentException: Binary XML file line

39: Duplicate id 0x7f0f018a, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.SupportMapFragment

Пожалуйста, любой из них, как решить эту проблему.

Это мой фрагмент класс:

public class ClientRequestFragment extends Fragment implements OnMapReadyCallback { 

View clientRequestView; 
private static final String TAG = ClientRequestFragment.class.getSimpleName(); 
GoogleMap googleMap; 
@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    SupportMapFragment mapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map_online); 
    mapFragment.getMapAsync(this); 


} 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

} 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    LayoutInflater li = (LayoutInflater) getActivity().getSystemService(
      Context.LAYOUT_INFLATER_SERVICE); 

    clientRequestView = li.inflate(R.layout.offline_fragment, container, false); 

    return clientRequestView; 
} 

public void addFragment(Fragment fragment, boolean addToBackStack, 
         String tag, boolean isAnimate) { 
    FragmentManager manager = getActivity().getSupportFragmentManager(); 
    FragmentTransaction ft = manager.beginTransaction(); 
    if (isAnimate) { 
     ft.setCustomAnimations(R.anim.slide_in_right, 
       R.anim.slide_out_left, R.anim.slide_in_left, 
       R.anim.slide_out_right); 
    } 

    if (addToBackStack) { 
     ft.addToBackStack(tag); 
    } 
    ft.replace(R.id.content_frame, fragment, tag); 
    ft.commitAllowingStateLoss(); 


} 
@Override 
public void onClick(View view) { 

} 

@Override 
public void onMapReady(GoogleMap googleMap) { 

    this.googleMap = googleMap; 
    //googleMap.setMyLocationEnabled(true); 
/* try { 
     // Customise the styling of the base map using a JSON object defined 
     // in a raw resource file. 
     boolean success = googleMap.setMapStyle(
       MapStyleOptions.loadRawResourceStyle(
         getActivity(), R.raw.retro_style_json)); 

     if (!success) { 
      Log.e(TAG, "Style parsing failed."); 
     } 
    } catch (Resources.NotFoundException e) { 
     Log.e(TAG, "Can't find style. Error: ", e); 
    }*/ 


} 

@Override 
public void onDestroyView() { 
    super.onDestroyView(); 
    FragmentManager fm = getActivity().getSupportFragmentManager(); 
    Fragment fragment = (fm.findFragmentById(R.id.map_online)); 
    FragmentTransaction ft = fm.beginTransaction(); 
    ft.remove(fragment); 
    ft.commit(); 

} 

@Override 
public void onPause() { 
    super.onPause(); 


} 

} 

Спасибо.

+0

проверьте это http://stackoverflow.com/questions/14083950/duplicate-id-tag-null-or-parent-id-with-another-fragment-for-com-google-androi –

+1

Можем ли мы увидеть ваш фрагмент код? –

+0

http://stackoverflow.com/questions/14928833/android-app-error-duplicate-id-0x7f04000f-tag-null-or-parent-id-0x0-with-ano –

ответ

1

Ваше приложение получает сообщение об ошибке, поскольку статический фрагмент внутри фрагмента не может быть удален или заменен, вы можете только скрыть или показать этот статический фрагмент. сделать файл XML, как это:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/coreLayout" 
    android:orientation="vertical" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical"> 

     <FrameLayout 
      android:id="@+id/fragment_home_container" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

</LinearLayout> 

в файле fragment.java написать этот код:

PlaceAutocompleteFragment autocompleteFragmentSourceAddress = new PlaceAutocompleteFragment(); 
    replaceFragment(autocompleteFragmentSourceAddress, R.id.fragment_home_container); 
0

Вам нужно удалить фрагмент, прежде чем нажать новый на ваш взгляд фрагмент был создан заново поэтому добавьте ==null условия в onCreatedView методы

View mainView;// this should be global variable. 

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     if(mainview==null){ 
      mainView = inflater.inflate(R.layout.fragment_layout, container, false); 
      initializeView();//initialize all views in this method instead of onActivityCreated 
     } 
     return mainView; 
    } 

Add this when you push above fragment to avoid duplication

try { 
    android.app.Fragment fragment = getFragmentManager().findFragmentById(R.id.map_online); 
    if (fragment != null) { 
     android.app.FragmentTransaction ft = getFragmentManager().beginTransaction(); 
     ft.remove(fragment); 
     ft.commit(); 
    } 
} catch (Exception e) { 
    e.printStackTrace(); 
}