当方向发生变化时,android中的片段无法正常工作

时间:2014-01-29 06:43:57

标签: java android layout android-fragments landscape-portrait

我在这个网站上有一个教程http://examples.javacodegeeks.com/android/core/app/fragment/android-fragments-example/我能够在我的项目中实现它完美的工作但我遇到了一个简单的故障我无法解决。当我在肖像上运行项目然后我改变横向该项目停止工作,我不知道出了什么问题。我试图为肖像和风景制作一个单独的xml,但它不起作用。这是我的代码

主要活动

<?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();

        }

    }

片段 细节:          

       <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);
       }
    }

更新:

logcat

1 个答案:

答案 0 :(得分:0)

我在我的清单中添加了这个,我现在在活动中工作

android:configChanges="orientation|screenSize"