我试图将一个片段插入到另一个片段中,并且我已经成功完成了这个,直到我第一次吃午餐时它的主要片段正在工作但是当我试图重新加载时片段应用程序崩溃,我有这个错误:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.source_destination,container, false);
PlaceAutocompleteFragment sourcr_autocompleteFragment = (PlaceAutocompleteFragment)
getActivity().getFragmentManager()
.findFragmentById(R.id.sourcr_autocomplete_fragment);
return rootView;
// List item
}
这是我的片段
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dp"
android:background="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_medium"
android:layout_marginBottom="@dimen/margin_medium">
<fragment
android:id="@+id/sourcr_autocomplete_fragment"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.v7.widget.CardView>
</LinearLayout>
这是我的XML
$(document).ready(function(){
$("#panel").on('touchstart', function(e){
e.preventDefault();
$("#start-message").append("<p>Touch started</p>");
});
$("#panel").on('touchend', function(e){
e.preventDefault();
$("#end-message").append("<p>Touch end</p>");
});
$("#panel").on('touchmove', function(e){
e.preventDefault();
$("#move-message").append("<p>Touch moving...</p>");
});
$("#panel").on('touchleave', function(e){
e.preventDefault();
$("#leave-message").append("<p>Touch leaved.</p>");
});
$("#panel").on('touchcancel', function(e){
e.preventDefault();
$("#cancel-message").append("<p>Touch cancelled.</p>");
});
});
答案 0 :(得分:5)
我遇到了同样的问题,可以通过删除 onDismiss 回调中的片段来解决它。
@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
if (getActivity() != null) {
FragmentManager fragmentManager = getActivity().getFragmentManager();
Fragment fragment = fragmentManager.findFragmentById(R.id.sourcr_autocomplete_fragment);
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(fragment);
fragmentTransaction.commit();
}
}
答案 1 :(得分:3)
覆盖片段中的onDestroyView方法
public void onDestroyView() {
super.onDestroyView();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
Fragment fragment = fragmentManager.findFragmentById(R.id.sourcr_autocomplete_fragment);
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(fragment);
fragmentTransaction.commit();
}
科特林:
override fun onDestroyView() {
super.onDestroyView()
activity?.supportFragmentManager?.also { fragmentManager ->
fragmentManager.findFragmentById(R.id.aboutFragment)?.also { fragment ->
fragmentManager.beginTransaction().remove(fragment).commit()
}
}
}