首页 > 解决方案 > 未收到碎片捆绑的活动

问题描述

更新:这是我当前的代码,但是在我重新单击同一个按钮后它会中断......原因:java.lang.IllegalStateException:片段已经处于活动状态

main_activity.xml分为框架布局。上部框架布局包含一个编辑文本和 3 个按钮(小、中、大)。下部框架布局包含一个文本视图,用于以小、中或大字体填充编辑文本的文本。

我将文本放入一个包中,并在单击按钮后尝试在相应的片段中重新打开该包......但是,根据我的调试器,该包不包含来自文本的任何文本信息。

这是我删除了中、大按钮的代码

请问有什么帮助吗?

主要活动:

public class MainActivity extends AppCompatActivity {
 TextView textView;
 Button bSmall;
 Small fSmall = new Small();
  Bundle bundle = new Bundle();



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

        if(findViewById(R.id.fragment)!=null){
            if(savedInstanceState!=null){
                return;
            }

        }

        final EditText editText = (EditText) findViewById(R.id.type_here);

        final Button bSmall = (Button)findViewById(R.id.small);
        Button bMedium = (Button)findViewById(R.id.medium);

        bSmall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String message_small = editText.getText().toString();
                small = message_small;

                bundle.putString("message_small", message_small);
                fSmall.setArguments(bundle);
                FragmentManager fm =getFragmentManager();

                FragmentTransaction ft = fm.beginTransaction();
                ft.replace(R.id.fragment, fSmall).commit();


            }

        });

        bMedium.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String message_medium = editText.getText().toString();
                bundle.putString("message_medium", message_medium);
                fMedium.setArguments(bundle);
                FragmentManager fm = getFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                ft.replace(R.id.fragment, fMedium).commit();
            }
        });



    }

    public String getMyData(){
        return small;
    }
}



Small:

public class Small extends Fragment {

    EditText editText;
    View myView;




    public Small() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        myView = inflater.inflate(R.layout.fragment_small, container, false);

        Bundle bundle = new Bundle();
        bundle = getArguments();
        MainActivity activity = (MainActivity)getActivity();
        //String dataFromMainActivity = activity.getMyData();


        String myString = bundle.getString("message_small");
        TextView set = myView.findViewById(R.id.small_text);
        set.setText(myString);

        // Inflate the layout for this fragment

        return myView;
    }

}

标签: androidfragmentbundle

解决方案


您忘记将捆绑包添加到片段中。你需要做这样的事情:

 Small fSmall = new Small();
 ...
 bundle.putString("message_small", message_small);
 fSmall.setArguments(bundle);

 FragmentManager fm =getFragmentManager();

 FragmentTransaction ft = fm.beginTransaction();
 ft.replace(R.id.fragment, fSmall);

推荐阅读