Question

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

Was it helpful?

Solution

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

android:configChanges="orientation|screenSize"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top