4 votes

Erreur de gonflement des fragments lors des changements d'orientation

Ok, voici le problème, j'ai une activité fragmentée qui contient deux fragments. L'un est un ListFragment et l'autre un Fragment. Je veux donc afficher le fragment en fonction de l'élément sélectionné. Voici le problème. Si je lance cette activité de fragment soit en mode paysage, soit en mode portrait, elle fonctionne bien. Cependant, lorsque je passe à l'autre orientation, elle se bloque toujours. L'erreur concerne l'inflateur, quelque chose comme le onCreateView mais j'ai regardé dans d'autres articles et aucune des autres résolutions telles que onConfigurationChanges, onSaveInstances, le changement de nom en classe. Toutes ont la même erreur. J'espère donc que quelqu'un pourra m'aider. J'ai bien le layout-land et le layout et ça ne marche toujours pas.

Voici mes codes.

activité_pontix (en mode terrestre et portrait)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#CCFF99"
    android:orientation="vertical" >

    <TextView android:id="@+id/Pontix"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center_horizontal"
        android:text="@string/pontix"
        android:background="#ffffff"
        android:textColor="#000000"
        android:textSize="40dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <fragment
            android:id="@+id/sideMenu"
            android:name="com.example.pontix.SideMenu"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3" />

        <fragment
            android:id="@+id/content"
            android:name="com.example.pontix.ContentPreview"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </LinearLayout>

</LinearLayout>

SideMenu.java

public class SideMenu extends ListFragment
{
    OnMenuSelectedListener setSelected;

    public interface OnMenuSelectedListener 
    {
        public void onItemSelected(int position);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        int layout = R.layout.side_menu_item;

        setListAdapter(new ArrayAdapter<String>(getActivity(), layout, MenuItem.Items));
    }

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

         if (getFragmentManager().findFragmentById(R.id.sideMenu) != null) {
                getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            }
    }

    @Override
    public void onAttach(Activity activity) 
    {
        super.onAttach(activity);

        try 
        {
            setSelected = (OnMenuSelectedListener) activity;
        } 
        catch (ClassCastException e) 
        {
            throw new ClassCastException(activity.toString() + " must implement OnMenuSelectedListener");
        }
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) 
    {
        setSelected.onItemSelected(position);

         // Set the item as checked to be highlighted when in two-pane layout
        getListView().setItemChecked(position, true);
    }
}

ContentPreview.java

public class ContentPreview extends Fragment
{
    final static String ARG_POSITION = "position";
    int mCurrentPosition = -1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {
        if (savedInstanceState != null) 
        {
            mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
        }

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

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

        Bundle args = getArguments();
        if (args != null) 
        {
            updateContentPreview(args.getInt(ARG_POSITION));
        } 
        else if (mCurrentPosition != -1) 
        {
            updateContentPreview(mCurrentPosition);
        }
    }

    public void updateContentPreview(int position) 
    {
        switch(position)
        {
            case 1:
            {
                TextView article = (TextView) getActivity().findViewById(R.id.contentView);
                article.setText("Voce estava na aba Gincana");
                mCurrentPosition = position;
                break;
            }
            case 2:
            {
                TextView article = (TextView) getActivity().findViewById(R.id.contentView);
                article.setText("Voce estava na aba Tarefas");
                mCurrentPosition = position;
                break;
            }
            case 3:
            {
                TextView article = (TextView) getActivity().findViewById(R.id.contentView);
                article.setText("Voce estava na aba Ranking");
                mCurrentPosition = position;
                break;
            }
            case 4:
            {
                TextView article = (TextView) getActivity().findViewById(R.id.contentView);
                article.setText("Voce estava na aba Amigos");
                mCurrentPosition = position;
                break;
            }
            case 5:
            {
                TextView article = (TextView) getActivity().findViewById(R.id.contentView);
                article.setText("Voce estava na aba Perfil");
                mCurrentPosition = position;
                break;
            }
            case 6:
            {
                TextView article = (TextView) getActivity().findViewById(R.id.contentView);
                article.setText("Voce estava na aba Sobre");
                mCurrentPosition = position;
                break;
            }
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) 
    {
        super.onSaveInstanceState(outState);

        outState.putInt(ARG_POSITION, mCurrentPosition);
    }
}

PontixActivity.java

public class PontixActivity extends FragmentActivity implements SideMenu.OnMenuSelectedListener 
{   
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pontix);

        Intent intent = getIntent();
        int code = intent.getIntExtra(GincanaActivity.EXTRA_CONTENT,0);

        SideMenu sideMenu = new SideMenu();

        sideMenu.setArguments(getIntent().getExtras());

        getSupportFragmentManager().beginTransaction().add(R.id.sideMenu, sideMenu).commit();

        ContentPreview contentFragment = (ContentPreview) getSupportFragmentManager().findFragmentById(R.id.content);

        if (contentFragment != null) 
        {
            contentFragment.updateContentPreview(code);

        } 
    }

    public void onItemSelected(int position) 
    {
        switch(position)
        {
            case 0:
            {
                Intent contentView = new Intent(this, GincanaActivity.class);
                startActivity(contentView);
                break;
            }
            case 1:
            {
                Intent contentView = new Intent(this, TarefaActivity.class);
                startActivity(contentView);
                break;
            }
            case 2:
            {
                Intent contentView = new Intent(this, RankingActivity.class);
                startActivity(contentView);
                break;
            }
            case 3:
            {
                Intent contentView = new Intent(this, MainActivity.class);
                startActivity(contentView);
                break;
            }
            case 4:
            {
                Intent contentView = new Intent(this, MainActivity.class);
                startActivity(contentView);
                break;
            }
            case 5:
            {
                Intent contentView = new Intent(this, MainActivity.class);
                startActivity(contentView);
                break;
            }
        }
    }
}

Voici le journal des erreurs.

08-03 13:13:58.745: W/IInputConnectionWrapper(13847): showStatusIcon on inactive InputConnection
08-03 13:14:09.065: D/AbsListView(13847): Get MotionRecognitionManager
08-03 13:14:09.065: D/AbsListView(13847): Get MotionRecognitionManager
08-03 13:14:12.310: D/AndroidRuntime(13847): Shutting down VM
08-03 13:14:12.310: W/dalvikvm(13847): threadid=1: thread exiting with uncaught exception (group=0x40c661f8)
08-03 13:14:12.340: E/AndroidRuntime(13847): FATAL EXCEPTION: main
08-03 13:14:12.340: E/AndroidRuntime(13847): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pontix/com.example.pontix.PontixActivity}: android.view.InflateException: Binary XML file line #21: Error inflating class fragment
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3365)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.app.ActivityThread.access$700(ActivityThread.java:128)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1165)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.os.Looper.loop(Looper.java:137)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.app.ActivityThread.main(ActivityThread.java:4514)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at java.lang.reflect.Method.invokeNative(Native Method)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at java.lang.reflect.Method.invoke(Method.java:511)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at dalvik.system.NativeStart.main(Native Method)
08-03 13:14:12.340: E/AndroidRuntime(13847): Caused by: android.view.InflateException: Binary XML file line #21: Error inflating class fragment
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:280)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.app.Activity.setContentView(Activity.java:1892)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at com.example.pontix.PontixActivity.onCreate(PontixActivity.java:13)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.app.Activity.performCreate(Activity.java:4562)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
08-03 13:14:12.340: E/AndroidRuntime(13847):    ... 12 more
08-03 13:14:12.340: E/AndroidRuntime(13847): Caused by: java.lang.IllegalStateException: Fragment com.example.pontix.SideMenu did not create a view.
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:295)
08-03 13:14:12.340: E/AndroidRuntime(13847):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
08-03 13:14:12.340: E/AndroidRuntime(13847):    ... 23 more
08-03 13:14:12.355: D/dalvikvm(13847): GC_CONCURRENT freed 152K, 3% free 13194K/13575K, paused 1ms+3ms
08-03 13:14:12.355: D/AbsListView(13847): [unregisterDoubleTapMotionListener]
08-03 13:14:12.355: I/MotionRecognitionManager(13847):   .unregisterListener : / listener count = 0->0, listener=android.widget.AbsListView$4@419278a8
08-03 13:14:12.355: D/AbsListView(13847): [unregisterDoubleTapMotionListener]
08-03 13:14:12.355: I/MotionRecognitionManager(13847):   .unregisterListener : / listener count = 0->0, listener=android.widget.AbsListView$4@4192d5e0
08-03 13:14:12.360: D/AbsListView(13847): [unregisterDoubleTapMotionListener]
08-03 13:14:12.360: I/MotionRecognitionManager(13847):   .unregisterListener : / listener count = 0->0, listener=android.widget.AbsListView$4@41963500

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X