没有收到Fragment Bundle的活动

问题描述 投票:0回答:2

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

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

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

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

有什么帮助吗?

我的名字是c:

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

}
android fragment bundle
2个回答
1
投票

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

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

0
投票

我想你在调用片段管理器之前忘了将你的bundle添加到Fragment中。

你应该尝试这样的事情:

Small fSmall = new Small();
Bundle bundle = new Bundle();
bundle.putString("message_small", message_small);   //parameters are (key, value).
fSmall.setArguments(bundle);

getSupportFragmentManager().beginTransaction().replace(R.id.fragment, fSmall).commit();

在第二个片段中,您应该检查myString是否为空或空。

String myString = getArguments().getString("message_small");
if (myString == null) {
    Log.e("TAG", "Error: null argument");
}

编辑我在这里看到另一个问题。您正在访问尚未实例化的变量。你应该在调用qazxsw poi之前给你的布局充气,否则它会返回一个qazxsw poi。

像这样更新你的findViewById()类:

NullPointerException

最好

© www.soinside.com 2019 - 2024. All rights reserved.