【问题标题】:app getting crash while resume app应用程序在恢复应用程序时崩溃
【发布时间】:2016-12-22 11:11:49
【问题描述】:

请任何人帮助我,我是 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.

在恢复应用程序时,我需要加载一个带有地图的片段,

我收到以下错误:

原因:java.lang.IllegalArgumentException: Binary XML file line

39:重复 id 0x7f0f018a、标签 null 或父 id 0xffffffff 与 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();


}

}

谢谢。

【问题讨论】:

标签: android android-fragments drawerlayout onresume


【解决方案1】:

您的应用程序崩溃,因为 Fragment 内的静态 Fragment 无法删除或替换,您只能隐藏或显示此静态 Fragment。 使您的 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);

【讨论】:

    【解决方案2】:

    在重新创建片段视图中的新片段之前,您需要删除片段,因此在onCreatedView 方法中添加==null 条件,

    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;
        }
    

    当你推送上面的片段时添加这个以避免重复

    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();
    }
    

    【讨论】:

      猜你喜欢
      • 2015-09-02
      • 1970-01-01
      • 1970-01-01
      • 2017-06-07
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多