Autore Topic: MapFragment dentro ViewPager crash quando cambio orientazione  (Letto 356 volte)

Offline secretx

  • Nuovo arrivato
  • *
  • Post: 1
  • Respect: 0
    • Google+
    • secret931
    • _xXsecretXx_
    • Mostra profilo
MapFragment dentro ViewPager crash quando cambio orientazione
« il: 11 Agosto 2014, 20:59:39 CEST »
0
Salve.

Ho un problema con i fragment, quando cambio l'orientazione dello schermo, mentre mi trovo nel tab Map o nel tab successivo (Tab2) la mia applicazione crasha, l'errore è nel file map.java (riga 23), infatti se elimino questa riga l'app funziona anche cambiando orientazione, ma in tal caso se vado nel Tab3 e successivamente ritorno nel tab Map (o Tab2) l'applicazione crasha nuovamente, di seguito il sorgente:

MainActivity.java
Codice (Java): [Seleziona]
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
Codice (Java): [Seleziona]
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
Codice (XML): [Seleziona]
<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
Codice (XML): [Seleziona]
<?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 trace
Codice: [Seleziona]
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


Qualche soluzione?

Grazie in anticipo.
« Ultima modifica: 11 Agosto 2014, 21:02:13 CEST da secretx »