【问题标题】:My fragment does not appear in the main activity我的片段没有出现在主要活动中
【发布时间】:2015-09-08 15:38:27
【问题描述】:

我使用 XML 添加了片段,它工作正常,但是当我尝试动态添加它时,它没有出现在主要活动中

FragmentActivity.java:

package com.example.pc.learn_again;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.os.PersistableBundle;

public class FragmentActivity2 extends Activity{
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
    super.onCreate(savedInstanceState, persistentState);
    setContentView(R.layout.mainfragment2);

    //adding the fragment using Java code---dynamically ya3ni//
    //create an object of the intended frag//
    Fragment3 frag = new Fragment3();
    //get the manager
    FragmentManager manager = getFragmentManager();
    //start the transaction
    FragmentTransaction transaction = manager.beginTransaction();
    //add to the layout
    transaction.add(R.id.mylayout,frag,"fragment3");//--note: the parameters used are 1- the id of the main layout 2- the intended fragment 3- a key for future use//
    //finish using the below
    transaction.commit();
}
}

Fragment3.java:

package com.example.pc.learn_again;


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

public class Fragment3 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment3,container,false);
}
}

fragment3.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/highlighted_text_material_light">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/textView3"
    android:layout_gravity="center_horizontal"
    android:layout_margin="40dp"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button12"
    android:layout_below="@+id/textView3"
    android:layout_margin="40dp"
    android:layout_gravity="center_horizontal" />
</RelativeLayout>

mainfragment2.xml:

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

</LinearLayout>

我检查了上述所有内容以及 xml 文件和类之间的连接,所有内容都是正确的,尽管当我测试它时,片段没有显示在活动中

【问题讨论】:

  • 我看到的一些东西,没有测试你的代码: - 尝试为你的 FragmentManager、FragmentTransaction 和 Fragment 导入 android.support.v4.* 类 - 尝试改变你的活动来扩展 FragmentActivity -使用不同的 onCreate() 方法——没有 PersistentState

标签: android xml android-fragments


【解决方案1】:

试试这个:

mainfragment2.xml:

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

【讨论】:

    【解决方案2】:

    我认为问题可能是您使用了错误的 onCreate(),因此您的活动代码永远不会运行。

    onCreate(Bundle) 是创建活动时始终调用的标准方法。 onCreate(Bundle, PersistableBundle) 仅在您正确设置了 persistableMode 标记时才会调用。

    【讨论】:

      【解决方案3】:

      首先在LinearLayout 中创建FrameLayout mainfragment2.xml

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/mylayout">
      
          <FrameLayout android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:id="@+id/container" />
      
      </LinearLayout>
      

      然后,使用replace() 方法将Fragment3 放入这个FrameLayout

      getFragmentManager().beginTransaction()
              .replace(R.id.container, new Fragment3).commit();
      

      由于Fragment是在API级别11中添加的,您可以使用支持库提供的getSupportFragmentManager()来使pre-Honeycomb设备也支持Fragment

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多