ViewPager内的MapFragment在改变方向时崩溃
Posted
技术标签:
【中文标题】ViewPager内的MapFragment在改变方向时崩溃【英文标题】:MapFragment inside ViewPager crash on change orientation 【发布时间】:2014-10-04 14:49:10 【问题描述】:当我更改屏幕方向时,应用程序崩溃。错误出现在 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_
android:layout_>
</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_
android:layout_ >
<fragment
android:id="@+id/location_map"
android:layout_
android:layout_
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
【问题讨论】:
显示 logcat 跟踪。 把log cat 放入提问而不是回答,这样得到答案的机会越来越高了!! 好的,我改了答案... 【参考方案1】:我正在这样做。这是为了在您的情况下将一个选项卡移动到另一个选项卡,并在此站点上进行旋转搜索和 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();
【讨论】:
【参考方案2】:我通过添加 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)
【讨论】:
以上是关于ViewPager内的MapFragment在改变方向时崩溃的主要内容,如果未能解决你的问题,请参考以下文章
ViewPager 中片段内的 ScrollView 内的 HorizontalListView
Android getParentFragment() 在 Fragment 内的 ViewPager 中返回 null