ViewPager中的MapFragment在更改方向上崩溃

时间:2014-08-11 17:25:12

标签: java android google-maps fragment screen-orientation

当我更改屏幕方向时,应用程序崩溃。错误在map.java第23行。事实上,如果我删除这一行,当我更改屏幕的方向时,应用程序不会崩溃,但如果我转到Tab3然后返回到Map(Tab),则不会崩溃)。

有谁知道这个问题的解决方案?

MainActivity.java

package com.example.map;

import com.example.map.TabsPagerAdapter;

import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.app.ActionBar.Tab;
import android.os.Bundle;

public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {

    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    // Tab titles
    private String[] tabs = { "Map", "Tab2", "Tasb3" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initilization
        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);       
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayShowTitleEnabled(true);

        // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
        }

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

    @Override
    public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction arg1) {
        // TODO Auto-generated method stub
         viewPager.setCurrentItem(tab.getPosition());

    }

    @Override
    public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }
}

TabsPagerAdapter.java

package com.example.map;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class TabsPagerAdapter extends FragmentPagerAdapter {

    public TabsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int index) {
        switch (index) {
        case 0:
            return new map();
        case 1:
            return new tab2();
        case 2:
            return new tab3();
        }

        return null;
    }

    @Override
    public int getCount() {
        return 3;
    }
}

map.java

package com.example.map;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class map extends Fragment {

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

        return inflater.inflate(R.layout.map, container, false);
    }

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

        //This is the line that makes the application crash when changing the screen orientation
        getFragmentManager().beginTransaction().remove(getFragmentManager().findFragmentById(R.id.location_map)).commit();
    }   
}

activity_main.xml中

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</android.support.v4.view.ViewPager>

map.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <fragment
        android:id="@+id/location_map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment" />

</RelativeLayout>

logcat的

08-11 19:39:26.639: D/AndroidRuntime(24966): Shutting down VM
08-11 19:39:26.639: W/dalvikvm(24966): threadid=1: thread exiting with uncaught exception (group=0x41616d88)
08-11 19:39:26.669: E/AndroidRuntime(24966): FATAL EXCEPTION: main
08-11 19:39:26.669: E/AndroidRuntime(24966): Process: com.example.map, PID: 24966
08-11 19:39:26.669: E/AndroidRuntime(24966): java.lang.RuntimeException: Unable to destroy activity {com.example.map/com.example.map.MainActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
08-11 19:39:26.669: E/AndroidRuntime(24966):    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3601)
08-11 19:39:26.669: E/AndroidRuntime(24966):    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3619)
08-11 19:39:26.669: E/AndroidRuntime(24966):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3819)
08-11 19:39:26.669: E/AndroidRuntime(24966):    at android.app.ActivityThread.access$900(ActivityThread.java:144)
08-11 19:39:26.669: E/AndroidRuntime(24966):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1252)
08-11 19:39:26.669: E/AndroidRuntime(24966):    at android.os.Handler.dispatchMessage(Handler.java:102)
08-11 19:39:26.669: E/AndroidRuntime(24966):    at android.os.Looper.loop(Looper.java:212)
08-11 19:39:26.669: E/AndroidRuntime(24966):    at android.app.ActivityThread.main(ActivityThread.java:5151)
08-11 19:39:26.669: E/AndroidRuntime(24966):    at java.lang.reflect.Method.invokeNative(Native Method)
08-11 19:39:26.669: E/AndroidRuntime(24966):    at java.lang.reflect.Method.invoke(Method.java:515)
08-11 19:39:26.669: E/AndroidRuntime(24966):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-11 19:39:26.669: E/AndroidRuntime(24966):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
08-11 19:39:26.669: E/AndroidRuntime(24966):    at dalvik.system.NativeStart.main(Native Method)
08-11 19:39:26.669: E/AndroidRuntime(24966): Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
08-11 19:39:26.669: E/AndroidRuntime(24966):    at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1360)
08-11 19:39:26.669: E/AndroidRuntime(24966):    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1378)
08-11 19:39:26.669: E/AndroidRuntime(24966):    at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
08-11 19:39:26.669: E/AndroidRuntime(24966):    at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
08-11 19:39:26.669: E/AndroidRuntime(24966):    at com.example.map.map.onDestroyView(map.java:23)
08-11 19:39:26.669: E/AndroidRuntime(24966):    at android.support.v4.app.Fragment.performDestroyView(Fragment.java:1709)
08-11 19:39:26.669: E/AndroidRuntime(24966):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1011)
08-11 19:39:26.669: E/AndroidRuntime(24966):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
08-11 19:39:26.669: E/AndroidRuntime(24966):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1103)
08-11 19:39:26.669: E/AndroidRuntime(24966):    at android.support.v4.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:1938)
08-11 19:39:26.669: E/AndroidRuntime(24966):    at android.support.v4.app.FragmentActivity.onDestroy(FragmentActivity.java:336)
08-11 19:39:26.669: E/AndroidRuntime(24966):    at android.app.Activity.performDestroy(Activity.java:5403)
08-11 19:39:26.669: E/AndroidRuntime(24966):    at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1117)
08-11 19:39:26.669: E/AndroidRuntime(24966):    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3588)
08-11 19:39:26.669: E/AndroidRuntime(24966):    ... 12 more
08-11 19:39:35.659: I/Process(24966): Sending signal. PID: 24966 SIG: 9

好的,我按如下方式更改了代码,但问题仍未解决(onSaveInstanceState后无法执行此操作)

MainActivity.java

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;

public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {

    public static FragmentManager fragManager;

    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    // Tab titles
    private String[] tabs = { "Map", "Tab2", "Tab3" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fragManager = getSupportFragmentManager();

        // Initilization
        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(fragManager);

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);       
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayShowTitleEnabled(true);

        // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
        }

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

    @Override
    public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction arg1) {
        // TODO Auto-generated method stub
         viewPager.setCurrentItem(tab.getPosition());

    }

    @Override
    public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }
}

map.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.SupportMapFragment;

public class discover extends Fragment {

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

        View v = inflater.inflate(R.layout.discover, container, false);
        Log.i("prova", "onCreateView");
        return v;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        SupportMapFragment f = (SupportMapFragment) MainActivity.fragManager.findFragmentById(R.id.location_map);

        if(f != null)
        {
            Log.i("prova", "onDestroyView");
            MainActivity.fragManager.beginTransaction().remove(MainActivity.fragManager.findFragmentById(R.id.location_map)).commit();

        }
    }
}

logcat的

08-14 13:46:39.653: I/prova(19209): onDestroyView
08-14 13:46:39.653: D/AndroidRuntime(19209): Shutting down VM
08-14 13:46:39.653: W/dalvikvm(19209): threadid=1: thread exiting with uncaught exception (group=0x41616d88)
08-14 13:46:39.703: D/dalvikvm(19209): GC_FOR_ALLOC freed 638K, 12% free 49716K/56272K, paused 26ms, total 26ms
08-14 13:46:39.703: E/AndroidRuntime(19209): FATAL EXCEPTION: main
08-14 13:46:39.703: E/AndroidRuntime(19209): Process: com.example.map, PID: 19209
08-14 13:46:39.703: E/AndroidRuntime(19209): java.lang.RuntimeException: Unable to destroy activity {com.example.map/com.example.map.MainActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
08-14 13:46:39.703: E/AndroidRuntime(19209):    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3601)
08-14 13:46:39.703: E/AndroidRuntime(19209):    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3619)
08-14 13:46:39.703: E/AndroidRuntime(19209):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3819)
08-14 13:46:39.703: E/AndroidRuntime(19209):    at android.app.ActivityThread.access$900(ActivityThread.java:144)
08-14 13:46:39.703: E/AndroidRuntime(19209):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1252)
08-14 13:46:39.703: E/AndroidRuntime(19209):    at android.os.Handler.dispatchMessage(Handler.java:102)
08-14 13:46:39.703: E/AndroidRuntime(19209):    at android.os.Looper.loop(Looper.java:212)
08-14 13:46:39.703: E/AndroidRuntime(19209):    at android.app.ActivityThread.main(ActivityThread.java:5151)
08-14 13:46:39.703: E/AndroidRuntime(19209):    at java.lang.reflect.Method.invokeNative(Native Method)
08-14 13:46:39.703: E/AndroidRuntime(19209):    at java.lang.reflect.Method.invoke(Method.java:515)
08-14 13:46:39.703: E/AndroidRuntime(19209):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-14 13:46:39.703: E/AndroidRuntime(19209):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
08-14 13:46:39.703: E/AndroidRuntime(19209):    at dalvik.system.NativeStart.main(Native Method)
08-14 13:46:39.703: E/AndroidRuntime(19209): Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
08-14 13:46:39.703: E/AndroidRuntime(19209):    at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1360)
08-14 13:46:39.703: E/AndroidRuntime(19209):    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1378)
08-14 13:46:39.703: E/AndroidRuntime(19209):    at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
08-14 13:46:39.703: E/AndroidRuntime(19209):    at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
08-14 13:46:39.703: E/AndroidRuntime(19209):    at com.example.map.discover.onDestroyView(discover.java:31)
08-14 13:46:39.703: E/AndroidRuntime(19209):    at android.support.v4.app.Fragment.performDestroyView(Fragment.java:1709)
08-14 13:46:39.703: E/AndroidRuntime(19209):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1011)
08-14 13:46:39.703: E/AndroidRuntime(19209):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
08-14 13:46:39.703: E/AndroidRuntime(19209):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1103)
08-14 13:46:39.703: E/AndroidRuntime(19209):    at android.support.v4.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:1938)
08-14 13:46:39.703: E/AndroidRuntime(19209):    at android.support.v4.app.FragmentActivity.onDestroy(FragmentActivity.java:336)
08-14 13:46:39.703: E/AndroidRuntime(19209):    at android.app.Activity.performDestroy(Activity.java:5403)
08-14 13:46:39.703: E/AndroidRuntime(19209):    at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1117)
08-14 13:46:39.703: E/AndroidRuntime(19209):    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3588)
08-14 13:46:39.703: E/AndroidRuntime(19209):    ... 12 more
08-14 13:46:43.183: I/Process(19209): Sending signal. PID: 19209 SIG: 9

2 个答案:

答案 0 :(得分:0)

我正在这样做。这是为了在你的情况下将一个标签移动到另一个标签,并在此网站上进行轮换搜索和saveinstantsate。当你保存当前状态时,不要调用ondestroyview方法

 package com.sunil.assignment;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.sunil.assignment.R.drawable;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class First extends Fragment{
    FragmentManager man;

    private GoogleMap googleMap;
    MapFragment mMapFragment;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View v=inflater.inflate(R.layout.firstscreen, container, false);


         try {
                // Loading map
                initilizeMap();

            } catch (Exception e)
            {
                e.printStackTrace();


            }



            LatLng mumbai=new LatLng(18.9300, 72.8200);
         googleMap.getUiSettings().setZoomControlsEnabled(true);

         googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mumbai, 13));

           // googleMap.addMarker(new MarkerOptions().title("Mumbai").snippet("Dream City").position(mumbai));
         // latitude and longitude
            double latitude = 18.9300;
            double longitude = 72.8200;

            // create marker
            final MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello");




            // adding marker
            googleMap.addMarker(marker);

           googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {

            @Override
            public boolean onMarkerClick(Marker arg0) {
                // TODO Auto-generated method stub

                 Toast.makeText(getActivity(), "Churchgate", Toast.LENGTH_SHORT).show();

                googleMap.addMarker( marker.
                        icon(BitmapDescriptorFactory.fromResource(R.drawable.images)).title("Mumbai").snippet("Dream City").
                    position(new LatLng(18.9300,72.8200)));

                  return true;
            }
                });  

            return v;
    }

        /**
         * function to load map. If map is not created it will create it for you
         * */
        private void initilizeMap() {
           /*
             mMapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);

             if (mMapFragment == null) {
                   FragmentManager fragmentManager = getFragmentManager();
                   FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                   mMapFragment = MapFragment.newInstance();
                   fragmentTransaction.replace(R.id.map, mMapFragment).commit();

                   Toast.makeText(getActivity(), "null", Toast.LENGTH_SHORT).show();
                     }

             if (mMapFragment != null)
             {
                 googleMap = mMapFragment.getMap();

                 Toast.makeText(getActivity(), "hi", Toast.LENGTH_SHORT).show();
                 if (googleMap != null)
                        googleMap.setOnMapClickListener(new GoogleMap. OnMapClickListener()
                        {
                         @Override
                         public void onMapClick(LatLng point)
                         {
                          //TODO: your onclick stuffs
                             Toast.makeText(getActivity(), "hi", Toast.LENGTH_SHORT).show();
                         }
                        });
             }   */

            if (googleMap == null) {



                googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                        R.id.map)).getMap();   

                // check if map is created successfully or not
                if (googleMap == null) {
                    Toast.makeText(getActivity(),
                            "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                            .show();
                }
            }   
        }


        @Override
        public void onDestroyView() {
            super.onDestroyView();
            MapFragment f = (MapFragment) getFragmentManager()
                                                 .findFragmentById(R.id.map);
            if (f != null) 
                getFragmentManager().beginTransaction().remove(f).commit();
        }


    }

答案 1 :(得分:0)

我通过添加try catch解决了这个问题,现在应用程序没有崩溃,但它不是一个优雅的解决方案,任何人都知道其他解决方案?

<强> map.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class discover extends Fragment {

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

        View v = inflater.inflate(R.layout.discover, container, false);

        return v;
    }


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

        try
        {
            MainActivity.fragManager.beginTransaction().remove(MainActivity.fragManager.findFragmentById(R.id.location_map)).commit();
        }
        catch(Exception e)
        {
        }
    }
}