活动布局: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(片段)。 我已经在各个地方保留了一系列的toast跟踪,我可以看到我的代码正在执行,直到FragmentManager的FragmentTransaction,之后它似乎没有调用Fragment的onCreate / onCreateView。
有人能指出我的错吗? 当我执行时,我无法看到正在呈现的片段UI。
答案 0 :(得分: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();
================== EDIT ============================ ==
另外
TextEntryFragment m_f_text_entry_activity=new TextEntryFragment();
而不是
Fragment m_f_text_entry_activity = m_fm_text_entry_activity.findFragmentById(R.id.fragmentContainer_TextEntry);