您好我正在片段中创建地图,我正在调用地图,下面我调用了listview和gridview。
第一次显示地图并且我获得了一个列表视图,但是当我转到不同的片段并返回到相同的片段时,我收到此错误
06-04 15:35:32.360: E/AndroidRuntime(22282): FATAL EXCEPTION: main
06-04 15:35:32.360: E/AndroidRuntime(22282): android.view.InflateException: Binary XML file line #12: Error inflating class fragment
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
06-04 15:35:32.360: E/AndroidRuntime(22282): at com.dext.shoply.fragment.MapFragmentTest.onCreateView(MapFragmentTest.java:61)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:478)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.view.View.measure(View.java:16841)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.view.View.measure(View.java:16841)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.view.View.measure(View.java:16841)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.view.View.measure(View.java:16841)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.view.View.measure(View.java:16841)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
06-04 15:35:32.360: E/AndroidRuntime(22282): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.view.View.measure(View.java:16841)
06-04 15:35:32.360: E/AndroidRuntime(22282): at com.jeremyfeinstein.slidingmenu.lib.CustomViewAbove.onMeasure(CustomViewAbove.java:484)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.view.View.measure(View.java:16841)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.view.View.measure(View.java:16841)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-04 15:35:32.360: E/AndroidRuntime(22282): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2586)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.view.View.measure(View.java:16841)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2189)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1352)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1535)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1249)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6364)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.view.Choreographer.doCallbacks(Choreographer.java:591)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.view.Choreographer.doFrame(Choreographer.java:561)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.os.Handler.handleCallback(Handler.java:730)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.os.Handler.dispatchMessage(Handler.java:92)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.os.Looper.loop(Looper.java:137)
06-04 15:35:32.360: E/AndroidRuntime(22282): at android.app.ActivityThread.main(ActivityThread.java:5455)
06-04 15:35:32.360: E/AndroidRuntime(22282): at java.lang.reflect.Method.invokeNative(Native Method)
06-04 15:35:32.360: E/AndroidRuntime(22282): at java.lang.reflect.Method.invoke(M
这是我用于地图的代码
public class MapFragmentTest extends Fragment implements ShareItemClicked {
GoogleMap mapView;
private static View view;
public static ListView listView;
public static GridView gridview;
private GridAdapter gridAdapter;
/**
* Note that this may be null if the Google Play services APK is not
* available.
*/
private static GoogleMap mMap;
private static Double latitude, longitude;
public MapFragmentTest() {
// TODO Auto-generated constructor stub
}
// public static MapFragmentTest newInstance() {
// MapFragmentTest fragment = new MapFragmentTest();
// return fragment;
// }
// @Override
// public void onCreate(Bundle arg0) {
// super.onCreate(arg0);
// }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_map,container, false);
// Passing harcoded values for latitude & longitude. Please change as
// per your need. This is just used to drop a Marker on the Map
latitude = 13.014471679122448;
longitude = 80.20384224075353;
// 13.014471679122448,80.20384224075353
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getActivity()
.getSupportFragmentManager().findFragmentById(
R.id.location_map)).getMap();
// Check if we were successful in obtaining the map.
if (mMap != null)
setUpMap();
}
// setUpMapIfNeeded(view, savedInstanceState); // For setting up the
// MapFragment
listView = (ListView) view.findViewById(R.id.list);
gridview = (GridView) view.findViewById(R.id.mapgrid);
// Defined Array values to show in ListView
String[] values = new String[] { "Android List View",
"Adapter implementation", "Simple List View In Android",
"Create List View Android", "Android Example",
"List View Source Code", "List View Array Adapter",
"Android Example List View" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity()
.getApplicationContext(), android.R.layout.simple_list_item_1,
android.R.id.text1, values) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView textView = (TextView) view
.findViewById(android.R.id.text1);
/* YOUR CHOICE OF COLOR */
textView.setTextColor(Color.BLUE);
return view;
}
};
// Assign adapter to ListView
gridview.setAdapter(new GridAdapter(getActivity(), Constant.hotDeals,
this));
listView.setAdapter(adapter);
return view;
}
/*****
* Sets up the map if it is possible to do so
*
* @param view2
* @return
*****/
/*
* public static void setUpMapIfNeeded(View view2, Bundle
* savedInstanceState) { // Do a null check to confirm that we have not
* already instantiated the map. if (mMap == null) { // Try to obtain the
* map from the SupportMapFragment. mMap = ((SupportMapFragment)
* getActivity().getSupportFragmentManager()
* .findFragmentById(R.id.location_map)).getMap(); // Check if we were
* successful in obtaining the map. if (mMap != null) setUpMap(); } }
*/
/**
* This is where we can add markers or lines, add listeners or move the
* camera.
* <p>
* This should only be called once and when we are sure that {@link #mMap}
* is not null.
*/
private static void setUpMap() {
// For showing a move to my loction button
mMap.setMyLocationEnabled(true);
// For dropping a marker at a point on the Map
mMap.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude)).title("My Home")
.snippet("Home Address"));
// For zooming automatically to the Dropped PIN Location
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
latitude, longitude), 12.0f));
mMap.setOnMarkerClickListener(new OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker arg0) {
// TODO Auto-generated method stub
listView.setVisibility(View.GONE);
gridview.setVisibility(View.VISIBLE);
return true;
}
});
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
if (mMap != null)
setUpMap();
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getActivity()
.getSupportFragmentManager().findFragmentById(
R.id.location_map)).getMap();
// Check if we were successful in obtaining the map.
if (mMap != null)
setUpMap();
}
}
@Override
public void shareItemClicked() {
// TODO Auto-generated method stub
gridAdapter.closeOther();
}
@Override
public void shareSponserItemClicked() {
// TODO Auto-generated method stub
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
FragmentManager fm = getActivity().getSupportFragmentManager();
Fragment fragment = (fm.findFragmentById(R.id.location_map));
if (fragment.isResumed()) {
FragmentTransaction ft = fm.beginTransaction();
ft.remove(fragment);
ft.commit();
}
super.onDestroy();
}
}
我的xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<fragment
android:id="@+id/location_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible">
</ListView>
<GridView
android:id="@+id/mapgrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:gravity="center"
android:horizontalSpacing="2dip"
android:numColumns="auto_fit"
android:scrollbars="none"
android:smoothScrollbar="true"
android:verticalSpacing="2dip"
android:visibility="gone">
</GridView>
</LinearLayout>
我从另一个片段调用mapfragment类 使用此方法
@Override
public Fragment getItem(int position) {
if (position != 1)
return TabChildFragment.newInstance(position, getActivity());
else
return new MapFragmentTest();
}
你们可以帮忙指出我哪里出错吗?
答案 0 :(得分:3)
试试此onDestroyView()
并移除onDestroy()
@Override
public void onDestroyView()
{
try{
SupportMapFragment fragment = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.location_map));
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
}catch(Exception e){
}
super.onDestroyView();
}
答案 1 :(得分:0)
if(yourfragment!== null){getActivity()。getFragmentManager()。beginTransaction()。remove(yourfragment).commit(); 。getFragmentManager()popBackStack();
}}
使用此代码重新点击片段。