Android FragmentTransaction不会将片段添加或替换为容器

时间:2015-01-18 08:05:16

标签: android fragment

之前已经问过这个问题,但是,我没有找到适合我的问题的答案。我正在构建一个简单的片段活动,在一个容器中显示屏幕顶部的三个按钮,按下时会在屏幕的其余部分更新片段。就像标签主机一样。我在所有代码中使用android.support.v4.app.Fragment,但仍无法让事务管理器在容器中显示片段。我已经在键盘上敲了几个小时并且找不到问题。

以下是扩展FragmentActivity的主要活动:

package com.example.testudpfragment;

import com.example.testudp.R;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

public class TestUDPFragmentActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_udpfragment);

        if(savedInstanceState == null)  {
            getSupportFragmentManager().beginTransaction()
                .add(R.id.menu_container, new MenuFragment()).commit();

        }
    }
}

以下是我的标签的MenuFragment类扩展片段:

package com.example.testudpfragment;

import com.example.testudp.R;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.support.v4.app.*;

public class MenuFragment extends Fragment{

    Fragment frag;
    FragmentTransaction fragTransaction;

    public MenuFragment()   {

    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)   {

        View view = inflater.inflate(R.layout.frament_menu, container, false);

        frag = new StringsFragment();
        fragTransaction = getFragmentManager().beginTransaction().add(R.id.container, frag);
        System.out.println(fragTransaction.toString());
        fragTransaction.commit();

        Button btnString = (Button) view.findViewById(R.id.button_string);
        Button btnSlider = (Button) view.findViewById(R.id.button_slider);
        Button btnTouch = (Button) view.findViewById(R.id.button_touch);



        btnString.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                frag = new StringsFragment();
                fragTransaction = getFragmentManager().beginTransaction().replace(R.id.container, frag);

                fragTransaction.commit();

            }
        });



        btnSlider.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                frag = new SlidersFragment();
                fragTransaction = getFragmentManager().beginTransaction().replace(R.id.container, frag);
                fragTransaction.commit();

            }
        });



        btnTouch.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                frag = new TouchFragment();
                fragTransaction = getFragmentManager().beginTransaction().replace(R.id.container, frag);
                fragTransaction.commit();

            }
        });
        return view;
    }
}

以下是我无法展示的三个片段之一:

package com.example.testudpfragment;

import com.example.testudp.R;

import android.support.v4.app.*;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class StringsFragment extends Fragment   {

    public StringsFragment()    {

    }

    public View onCreatView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)  {

        View rootView = inflater.inflate(R.layout.fragment_strings, null);

        return rootView;
    }
}

activity_test_udpfragment:

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

<FrameLayout
    android:id="@+id/menu_container"
    android:background="#eee"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</FrameLayout>

<FrameLayout
    android:id="@+id/container"
    android:background="#aaa"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</FrameLayout>

</LinearLayout>

这是frament_menu:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
style="android.att/buttonBarStyle" >``

<Button
    android:id="@+id/button_string"
    style="android.att/buttonBarButtonStyle"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="String" />

<Button
    android:id="@+id/button_slider"
    style="android.att/buttonBarButtonStyle"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Slider" />

<Button
    android:id="@+id/button_touch"
    style="android.att/buttonBarButtonStyle"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Touch" />

</LinearLayout>

这里是fragment_strings:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">


<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:scaleType = "centerCrop"
    android:src="@drawable/me_hooded" />

</RelativeLayout>

非常感谢任何帮助!

编辑: 这是新的TestUDPActivityFragment,带有更改片段的按钮:

package com.example.testudpfragment;

import com.example.testudp.R;

import android.support.v4.app.*;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.os.Bundle;

public class TestUDPFragmentActivity extends FragmentActivity {
Fragment frag;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test_udpfragment);

    Button btnString = (Button)findViewById(R.id.btn_string);
    Button btnSlider = (Button)findViewById(R.id.btn_slider);
    Button btnTouch = (Button)findViewById(R.id.btn_touch);


    if(savedInstanceState == null)  {
        getSupportFragmentManager().beginTransaction().add(R.id.container, new StringsFragment()).commit();

    btnString.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            frag = new StringsFragment();
            getSupportFragmentManager().beginTransaction().replace(R.id.container, frag).commit();
            }
    }); 

    btnSlider.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            frag = new SlidersFragment();
            getSupportFragmentManager().beginTransaction().replace(R.id.container, frag).commit();
            }
    }); 

    btnTouch.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            frag = new TouchFragment();
            getSupportFragmentManager().beginTransaction().replace(R.id.container, frag).commit();
            }
    }); 



    }
}

}

1 个答案:

答案 0 :(得分:1)

我可以看到几个问题...

首先,您的FrameLayout广告文件中定义了activity_test_udpfragment.xml容器,该文件由您TestUDPFragmentActivity夸大。尝试使用以下代码在StringsFragment代码中创建/添加MenuFragment时出现问题...

fragTransaction = getFragmentManager().beginTransaction().add(R.id.container, frag);

... MenuFragmentframent_menu.xml的一个布局,它不包含ID为FrameLayout的{​​{1}}。

这是您的第一个问题,但第二个问题是首先尝试使用R.id.container创建/添加Fragment。您的Fragment无法使用MenuFragment中的Framelayout

简而言之,我怀疑您希望TestUDPFragmentActivity两个Fragments显示在Activity布局中(并且StringsFragment不是MenuFragment的{​​{1}}在这种情况下,您需要将所有代码都用于创建/添加Fragments Activity