Android fragments Android应用程序在FragmentTransaction.replace上崩溃

Android fragments Android应用程序在FragmentTransaction.replace上崩溃,android-fragments,android-support-library,fragmenttransaction,Android Fragments,Android Support Library,Fragmenttransaction,我正在尝试使用support.v4库创建一个动态UI,以将片段插入到我为主要活动定义的框架布局中 以下是我的活动_main.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill

我正在尝试使用support.v4库创建一个动态UI,以将片段插入到我为主要活动定义的框架布局中

以下是我的活动_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:baselineAligned="false" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="vertical" >
        <Button
            android:id="@+id/button_report"
            android:text="@string/button_report"
        android:textSize="22sp"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
        <Button
            android:id="@+id/button_pay"
            android:text="@string/button_pay"
            android:textSize="22sp"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
        <Button
            android:id="@+id/button_contact"
            android:text="@string/button_contact"
            android:textSize="22sp"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </LinearLayout>    

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="2" />

</LinearLayout> 
这是我的手机碎片课程:

package com.example.Test;

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

public class FragmentPhone extends Fragment {

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_phone, container, false);

}
}
当我启动应用程序时,xml正确加载,framelayout被fragmentPhone填满,但按下任何按钮都会创建一个“应用程序意外停止”对话框,并提示我强制关闭

如有任何意见或建议,将不胜感激。谢谢

(未经测试)
将ft设置为全局字段,以便在单击处理程序中可用。

我最终在每个onClickListener中重新获取并声明FragmentManager和FragmentTransaction。我还删除了“addToBackStack”,因为我认为它是不必要的。以下是我当前的工作代码:

// "Report" button        
    btnReport.setOnClickListener(new OnClickListener()  {           
        public void onClick(View v) {
            android.support.v4.app.FragmentManager fragmentManager1 = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction1 = fragmentManager1.beginTransaction();
            fragmentTransaction1.replace(R.id.container, fragmentReport);
            fragmentTransaction1.commit();
        }
    });

一行
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentId,mf片断).commit()
// "Report" button        
    btnReport.setOnClickListener(new OnClickListener()  {           
        public void onClick(View v) {
            android.support.v4.app.FragmentManager fragmentManager1 = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction1 = fragmentManager1.beginTransaction();
            fragmentTransaction1.replace(R.id.container, fragmentReport);
            fragmentTransaction1.commit();
        }
    });