0

i got a tutorial in this site http://examples.javacodegeeks.com/android/core/app/fragment/android-fragments-example/ i was able to implement it on my project it works perfectly but i have encountered a simple glitch which i cant solve.when i run the project on portrait and then i change to landscape the project stops working and i have no idea what went wrong.i tried making a separate xml for portrait and landscape but it didn't work as well. here is my code

main activity

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

    <Button
        android:id="@+id/prevscore_details"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight=".5"
        android:background="@drawable/selector"
        android:onClick="selectFrag"
        android:text="Dtls" />

     <Button
         android:id="@+id/prevscore_p1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight=".5"
         android:background="@drawable/selector"
         android:onClick="selectFrag"
         android:text="P 1" />

     <Button
        android:id="@+id/prevscore_p2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight=".5"
        android:background="@drawable/selector"
        android:onClick="selectFrag"
        android:text="P 2" />

     <Button
         android:id="@+id/prevscore_p3"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight=".5"
         android:background="@drawable/selector"
         android:onClick="selectFrag"
         android:text="P3" /> 

     <Button
         android:id="@+id/prevscore_p4"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight=".5"
         android:background="@drawable/selector"
         android:onClick="selectFrag"
         android:text="P 4" />       

</LinearLayout>
   <fragment
        android:name="com.afield.golfscore.PreviousScoreDetails"
        android:id="@+id/fragment_place"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

java

package com.afield.golfscore;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class PreviousScore extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_previousscore);
        Toast toast = Toast.makeText(PreviousScore.this, "Previous Score screen", Toast.LENGTH_LONG);
        toast.show();
    }        
        public void selectFrag(View view) {
             Fragment fr;

             if(view == findViewById(R.id.prevscore_p1)) {
                 fr = new PreviousScorePlayerOne();

             }else if(view == findViewById(R.id.prevscore_p2)) {
                 fr = new PreviousScorePlayerTwo();

             }else if(view == findViewById(R.id.prevscore_p3)) {
                 fr = new PreviousScorePlayerThree();

             }else if(view == findViewById(R.id.prevscore_p4)) {
                 fr = new PreviousScorePlayerFour();

             }else {
                 fr = new PreviousScoreDetails();
             }


             FragmentManager fm = getFragmentManager();
             FragmentTransaction fragmentTransaction = fm.beginTransaction();
             fragmentTransaction.replace(R.id.fragment_place, fr);
             fragmentTransaction.commit();

        }

    }

fragments details:

       <TextView
           android:id="@+id/textView1"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_weight="1"
           android:text="details"
           android:textStyle="bold" />

</LinearLayout>

package com.afield.golfscore;

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


    public class PreviousScoreDetails extends Fragment{
       @Override
       public View onCreateView(LayoutInflater inflater,
          ViewGroup container, Bundle savedInstanceState) {
          /**
           * Inflate the layout for this fragment
           */
          return inflater.inflate(
          R.layout.activity_previousscoredetails, container, false);
       }
    }

p1:

           <TextView
               android:id="@+id/textView1"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:layout_weight="1"
               android:text="player one"
               android:textStyle="bold" />

    </LinearLayout>

package com.afield.golfscore;

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


    public class PreviousScorePlayerOne extends Fragment{
       @Override
       public View onCreateView(LayoutInflater inflater,
          ViewGroup container, Bundle savedInstanceState) {
          /**
           * Inflate the layout for this fragment
           */
          return inflater.inflate(
          R.layout.activity_previousscoreplayerone, container, false);
       }
    }

UPDATE:

logcat

Giant
  • 1,619
  • 7
  • 33
  • 67
  • 2
    Please post the logcat of error. – Mandar Kakade Jan 29 '14 at 06:49
  • @MandarKakade can you see the logcat clearly? – Giant Jan 29 '14 at 06:51
  • yup...looks clear enough – Mandar Kakade Jan 29 '14 at 06:52
  • what do you think is happening?why does it stops when i change orientation?by the way i didnt post the other fragments coz it will be too long.. – Giant Jan 29 '14 at 06:53
  • Well from what it looks like..the error occurs while inflating the class fragment. The thing is that activity gets restarted whenever you change the orientation. So you have to save data that you want to preserve in savedInstanceState. And retrieve them again in onCreate. Otherwise it gives exception. – Mandar Kakade Jan 29 '14 at 07:06
  • @MandarKakade how do i do it in this situation? – Giant Jan 29 '14 at 07:08
  • I think its already answered here http://stackoverflow.com/questions/13986630/saving-fragment-state-on-orientation-change and here http://stackoverflow.com/questions/3915952/how-to-save-state-during-orientation-change-in-android-if-the-state-is-made-of-m – Mandar Kakade Jan 29 '14 at 07:14
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/46302/discussion-between-hak-hak-and-mandar-kakade) – Giant Jan 29 '14 at 07:20

1 Answers1

0

i added this in my manifest and i worked now in the activity

android:configChanges="orientation|screenSize"
Giant
  • 1,619
  • 7
  • 33
  • 67