2

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

这是我的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 正确加载并且框架布局被 fragmentPhone 填充,但按下任何按钮都会创建一个“应用程序已意外停止”对话框并提示我强制关闭。

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

4

2 回答 2

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();
        }
    });
于 2013-08-14T21:04:35.727 回答
0

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

于 2013-08-13T14:25:07.320 回答