Android App Crashes on FragmentTransaction.replace

杀马特。学长 韩版系。学妹 提交于 2019-12-12 16:51:46

问题


I'm attempting to create a dynamic UI using the support.v4 library to insert fragments into a framelayout I have defined for my main activity.

Here is my 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> 

And here is the 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();
        }
    });

    }
}

And here is my FragmentPhone class:

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);

}
}

When I launch the app, the xml loads correctly and the framelayout gets filled with fragmentPhone, but pressing any of the buttons creates an "app has stopped unexpectedly" dialogue and prompts me to force close.

Any ideas or suggestions will be greatly appreciated. Thanks!


回答1:


I ended up re-getting and re-declaring FragmentManager and FragmentTransaction within each onClickListener. I also removed "addToBackStack" as I decided it was unneeded. Here is my current, working code:

// "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();
        }
    });



回答2:


(untested) make ft a global field so it's available in the click handler.



来源:https://stackoverflow.com/questions/18211222/android-app-crashes-on-fragmenttransaction-replace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!