android - 未收到碎片捆绑的活动
问题描述
更新:这是我当前的代码,但是在我重新单击同一个按钮后它会中断......原因: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;
}
}
解决方案
您忘记将捆绑包添加到片段中。你需要做这样的事情:
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);
推荐阅读
- spring - 此注释不适用于目标“局部变量”
- php - 为什么 PHP 减法给我这个?
- spring - 从 Spring 4 升级到 Spring 5 - NoClassDefFoundError: org/springframework/core/JdkVersion
- linux - 恢复构建
- excel-formula - 是否有将 json 日期转换为日期格式的函数(VB6 或 Excel)
- php - 从 WooCommerce 中的特定国家/地区位置获取运费 ID
- javascript - 是否可以选择忽略 SmoothScroll 导航栏中的一个导航链接?
- android-studio - 如何在评级栏上进行仪器测试
- javascript - 为什么使用 JavaScript 无法复制到剪贴板?
- javascript - Visual Studio Code 中的异步智能感知 节点JS