0

活动布局:activity_text_entry.xml

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

片段布局:fragment_text_entry.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"
     >

<EditText
     android:id="@+id/Text_Entry_Date"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:hint="@string/choose_date_text"
     android:layout_marginLeft="16dp"
     android:layout_marginRight="16dp"

     />
 <requestFocus />

<EditText
     android:id="@+id/Text_Entry_Title"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:hint="@string/choose_title_text"
    android:layout_marginBottom="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginTop="16dp" >


     </EditText>

<EditText
    android:id="@+id/Text_Entry_Content"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginTop="16dp"
    android:ems="100"
    android:gravity="left|top"
    android:hint="@string/enter_text_here"
    android:inputType="textMultiLine" >


</EditText>

<LinearLayout 
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:gravity="center|bottom"
    >

    <Button
        android:id="@+id/Text_Entry_Button_Save"
        android:text="@string/action_Save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp" />

    <Button
        android:id="@+id/Text_Entry_Button_Cancel"
        android:text="@string/action_Cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp" />

</LinearLayout>

</LinearLayout>

活动:TextEntryActivity

package com.app.journal_v001;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.widget.Toast;

public class TextEntryActivity extends FragmentActivity {

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



        FragmentManager m_fm_text_entry_activity = getSupportFragmentManager();

        Fragment m_f_text_entry_activity = m_fm_text_entry_activity.findFragmentById(R.id.fragmentContainer_TextEntry);

        if(m_f_text_entry_activity == null)
        {

            m_f_text_entry_activity = new Fragment();

            m_fm_text_entry_activity.beginTransaction()
                                    .add(R.id.fragmentContainer_TextEntry, m_f_text_entry_activity)
                                    .commit();


        }
    }

}

片段:TextEntryFragment

package com.app.journal_v001;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class TextEntryFragment extends Fragment {

//private Button m_btn_Text_Entry_Save;
//private Button m_btn_Text_Entry_Cancel;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflate,ViewGroup parent,Bundle savedInstanceState)
{
    View v = inflate.inflate(R.layout.fragment_text_entry, parent, false);
        return v;
}

}

我正在尝试从此 TextEntryActivity(activity) 呈现 TextEntryFragment(fragment)。我在不同的地方保存了一系列 toast 来跟踪,我可以看到我的代码一直执行到 FragmentManager 的 FragmentTransaction 之后,它似乎没有调用 Fragment 的 onCreate/onCreateView。

谁能指出我有什么问题?当我执行时,我看不到正在呈现的片段 UI。

4

1 回答 1

0

消除

if(m_f_text_entry_activity == null)
        {

            m_f_text_entry_activity = new Fragment();

            m_fm_text_entry_activity.beginTransaction()
                                    .add(R.id.fragmentContainer_TextEntry, m_f_text_entry_activity)
                                    .commit();


        }

并替换为

m_fm_text_entry_activity.beginTransaction()
                                    .add(R.id.fragmentContainer_TextEntry, m_f_text_entry_activity)
                                    .commit();

==================编辑=============================== ===

TextEntryFragment m_f_text_entry_activity=new TextEntryFragment();

代替

Fragment m_f_text_entry_activity = m_fm_text_entry_activity.findFragmentById(R.id.fragmentContainer_TextEntry);
于 2013-07-23T10:39:25.797 回答