Android应用程序在FragmentTransaction.replace崩溃

时间:2013-08-13 14:01:27

标签: android-fragments android-support-library fragmenttransaction

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

这是我的activity_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> 

这是MainActivity.java:

package com.example.Test;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends FragmentActivity
{
    // Declare button variables
protected Button btnReport;
protected Button btnPay;
protected Button btnContact;

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

        // Assign buttons
        btnReport = (Button)findViewById(R.id.button_report);
        btnPay = (Button)findViewById(R.id.button_pay);
        btnContact = (Button)findViewById(R.id.button_contact);
        // Import fragments
        final Fragment fragmentPhone = new FragmentPhone();
        final Fragment fragmentReport = new FragmentReport();
        final Fragment fragmentPay = new FragmentPay();

        final android.support.v4.app.FragmentManager fm = getSupportFragmentManager();  
        final FragmentTransaction ft = fm.beginTransaction();

        // Check for existing fragment, if none then add FragmentPhone.class
        if (savedInstanceState != null) return;
        else {  
        ft.add(R.id.container, fragmentPhone);
        ft.commit();
        }

// "Report" button        
        btnReport.setOnClickListener(new OnClickListener()  {           
    public void onClick(View v) {
    ft.replace(R.id.container, fragmentReport); //Replace the details_container
    ft.addToBackStack(null);    // Add transaction to back stack
            ft.commit();    // Commit the transaction
    }
    });
// "Pay" button
    btnPay.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
        ft.replace(R.id.container, fragmentPay);
        ft.addToBackStack(null);
            ft.commit();
        }
    });
// "Contact" button
    btnContact.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {               
        ft.replace(R.id.container, fragmentPhone);
        ft.addToBackStack(null);
            ft.commit();
        }
    });

    }
}

这是我的FragmentPhone课程:

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填充,但按下任何按钮会创建一个“应用程序意外停止”对话框并提示我强行关闭。

任何想法或建议将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:1)

我最终在每个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();
        }
    });

答案 1 :(得分:0)

(未测试的) 使ft成为一个全局字段,以便在点击处理程序中可用。