【问题标题】:Getting problems while showing map from portrait to landscape将地图从纵向显示为横向时出现问题
【发布时间】:2015-04-13 10:41:09
【问题描述】:

当我将平板电脑从纵向模式旋转到横向模式时,我的应用程序崩溃了,这是堆栈跟踪。

04-13 16:06:25.802: E/AndroidRuntime(6231): java.lang.RuntimeException: Unable to destroy activity {info.androidhive.slidingmenu/info.androidhive.slidingmenu.MainActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
04-13 16:06:25.802: E/AndroidRuntime(6231):     at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3273)
04-13 16:06:25.802: E/AndroidRuntime(6231):     at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3291)
04-13 16:06:25.802: E/AndroidRuntime(6231):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3489)
04-13 16:06:25.802: E/AndroidRuntime(6231):     at android.app.ActivityThread.access$700(ActivityThread.java:130)
04-13 16:06:25.802: E/AndroidRuntime(6231):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
04-13 16:06:25.802: E/AndroidRuntime(6231):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-13 16:06:25.802: E/AndroidRuntime(6231):     at android.os.Looper.loop(Looper.java:137)
04-13 16:06:25.802: E/AndroidRuntime(6231):     at android.app.ActivityThread.main(ActivityThread.java:4745)
04-13 16:06:25.802: E/AndroidRuntime(6231):     at java.lang.reflect.Method.invokeNative(Native Method)
04-13 16:06:25.802: E/AndroidRuntime(6231):     at java.lang.reflect.Method.invoke(Method.java:511)
04-13 16:06:25.802: E/AndroidRuntime(6231):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-13 16:06:25.802: E/AndroidRuntime(6231):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-13 16:06:25.802: E/AndroidRuntime(6231):     at dalvik.system.NativeStart.main(Native Method)
04-13 16:06:25.802: E/AndroidRuntime(6231): Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
04-13 16:06:25.802: E/AndroidRuntime(6231):     at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1280)
04-13 16:06:25.802: E/AndroidRuntime(6231):     at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1291)
04-13 16:06:25.802: E/AndroidRuntime(6231):     at android.app.BackStackRecord.commitInternal(BackStackRecord.java:548)
04-13 16:06:25.802: E/AndroidRuntime(6231):     at android.app.BackStackRecord.commit(BackStackRecord.java:532)
04-13 16:06:25.802: E/AndroidRuntime(6231):     at info.androidhive.slidingmenu.PhotosFragment.onDestroyView(PhotosFragment.java:144)
04-13 16:06:25.802: E/AndroidRuntime(6231):     at android.app.Fragment.performDestroyView(Fragment.java:1596)
04-13 16:06:25.802: E/AndroidRuntime(6231):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
04-13 16:06:25.802: E/AndroidRuntime(6231):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
04-13 16:06:25.802: E/AndroidRuntime(6231):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1017)
04-13 16:06:25.802: E/AndroidRuntime(6231):     at android.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:1826)
04-13 16:06:25.802: E/AndroidRuntime(6231):     at android.app.Activity.performDestroy(Activity.java:5171)
04-13 16:06:25.802: E/AndroidRuntime(6231):     at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1109)
04-13 16:06:25.802: E/AndroidRuntime(6231):     at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3260)
04-13 16:06:25.802: E/AndroidRuntime(6231):     ... 12 more

我在我的项目中使用导航抽屉。在抽屉里,我有一个点击打开片段的列表。在那个片段中,我正在使用地图。 map的xml是使用xml中的fragment标签制作的。

这是我的片段代码 p

ublic View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

         rootView = inflater.inflate(R.layout.fragment_photos, container, false);
        googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                R.id.currentmap)).getMap();
        geocoder = new Geocoder(getActivity(), Locale.getDefault());
        // Enabling MyLocation Layer of Google Map
        googleMap.setMyLocationEnabled(true);               


         // Getting LocationManager object from System Service LOCATION_SERVICE
        LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);

        // Creating a criteria object to retrieve provider
        Criteria criteria = new Criteria();

        // Getting the name of the best provider
        String provider = locationManager.getBestProvider(criteria, true);

        // Getting Current Location
        Location location = locationManager.getLastKnownLocation(provider);

        if(location!=null){
                onLocationChanged(location);
        }

       locationManager.requestLocationUpdates(provider, 20000, 0, this);

        return rootView;
    }
     private String getCompleteAddressString(double LATITUDE, double LONGITUDE) {
         String strAdd = "";

         try {
             List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
             if (addresses != null) {
                 Address returnedAddress = addresses.get(0);
                 StringBuilder strReturnedAddress = new StringBuilder("");

                 for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
                     strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
                 }
                 strAdd = strReturnedAddress.toString();
                 tvLocation.setText(strAdd);    
                 Log.w("My Current loction address", "" + strReturnedAddress.toString());
             } else {
                 Log.w("My Current loction address", "No Address returned!");
             }
         } catch (Exception e) {
             e.printStackTrace();
             Log.w("My Current loction address", "Canont get Address!");
         }
         return strAdd;
     }

    @Override
    public void onLocationChanged(Location location) {

        tvLocation = (TextView) rootView.findViewById(R.id.tv_location);

        // Getting latitude of the current location
        double latitude = location.getLatitude();

        // Getting longitude of the current location
        double longitude = location.getLongitude();     

        // Creating a LatLng object for the current location
        LatLng latLng = new LatLng(latitude, longitude);

        // Showing the current location in Google Map
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

        // Zoom in the Google Map
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(20));

        // Setting latitude and longitude in the TextView tv_location
        getCompleteAddressString(latitude,longitude);


    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub      
    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub      
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub      
    }
    @Override
    public void onDestroyView() {

        super.onDestroyView(); 
        Log.v("in on destroy","destroy");

        Fragment fragment = (getFragmentManager().findFragmentById(R.id.currentmap));  
        if (fragment != null){
            Log.v("in on destroy","removing");
          getFragmentManager().beginTransaction()
            .remove(fragment)
            .commit(); 
            Log.v("in on destroy","completed");
            }      

       Log.v("value is", val+"");
}
}

这个问题只出现在地图片段上。我没有使用地图的其他片段在方向变化上工作正常

【问题讨论】:

    标签: android google-maps android-fragments navigation-drawer


    【解决方案1】:

    这是bug,插入此代码:

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        //No call for super(). Bug on API Level > 11.
    }
    

    【讨论】:

    • 在片段中还是在活动中?因为我参加了一项活动(具有抽屉布局)并且片段具有地图
    • 我猜Fragment 是在您的应用中处理旋转的那个
    • 仍然遇到同样的异常
    • 如果你把它放在活动中?
    【解决方案2】:

    您必须实现 MapViews 生命周期调用:

    将此添加到您的代码中:

    private MapView mapView;
    private GoogleMap googleMap;
    

    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    
    
    rootView = inflater.inflate(R.layout.fragment_photos, container, false)
    mapView = (MapView) rootView.findViewById(R.id.currentmap);
    
    try {
         MapsInitializer.initialize(getActivity());
         mapView.onCreate(savedInstanceState);
    
    
        } catch (GooglePlayServicesNotAvailableException e) {
           //Google play services not availabe
        }
    if(mapView!= null){
    
    
    
    googleMap = mapView.getMap();
    }
    

    //你的代码在这里

    }

    @Override
    public void onResume() {
        super.onResume();
        if(mapView!=null)
        mapView.onResume();
    
    }
    
    
    @Override
    public void onPause() {
        super.onPause();
        if(mapView!=null)
        mapView.onPause();
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        if(mapView!=null)
        mapView.onDestroy();
    }
    
    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mapView.onLowMemory();
    }
    

    使用 LinearLayout 或 RelativeLayout 作为 mapview 的父级:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <com.google.android.gms.maps.MapView
        android:id="@+id/currentmap"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
    </RelativeLayout>
    

    【讨论】:

    • currentmap 是片段 id 而不是 mapview
    • 您可以将地图视图放入 LinearLayout 或 RealtiveLayout:
    • 我认为这与线性布局无关。问题是我们需要在方向更改时保存地图状态,而在我的情况下,我不知道该怎么做。我猜它的答案不在SO中。看到很多这样的问题,但没有一个得到很好的回答
    【解决方案3】:

    我终于找到了解决方案。我需要在清单中添加这个

    <activity 
            android:theme="@style/AppTheme" 
            android:name="MyFragmentActivity" 
            android:label="@string/fragmentActivity" 
            android:configChanges="orientation|screenSize">
        </activity>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-14
      • 2011-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-06
      相关资源
      最近更新 更多