当我尝试在片段之间进行通信时,无法将 FragmentTwo 转换为 FragmentOne
FragmentTwo cannot be cast to FragmentOne when I am trying to comunicate between fragments
我有两个片段,我想在我的屏幕上一个一个地显示它们。它们由 MainActivity 管理。第一个片段包含一个 TextView 和一个 Button。第二个片段包含一个 EditText 和一个 Button。我想做的是:第一个片段显示在屏幕上,当用户按下按钮时,我希望我的 activity 显示第二个片段,当用户按下这个片段中的按钮时,我想发送从 EditText 到第一个片段的文本并将其显示在 TextView 上。我为听众使用了界面。
public class MainActivity extends AppCompatActivity implements Listener, ListenerTwo{
FragmentTransaction fragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragmentPlaceholder, new FragmentOne());
fragmentTransaction.commit();
}
@Override
public void onButtonSelected() {
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragmentPlaceholder, new FragmentTwo());
fragmentTransaction.commit();
}
@Override
public void onButtonPressed(String text) {
FragmentOne fragmentOne = (FragmentOne)getSupportFragmentManager().findFragmentById(R.id.fragmentPlaceholder);
if (fragmentOne != null)
fragmentOne.setTextt(text);
}
}
在我的 FragmentOne 中:
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button button = view.findViewById(R.id.button);
textView = view.findViewById(R.id.textView);
Listener listener = (Listener) getActivity();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "button clicked", Toast.LENGTH_SHORT).show();
if (listener != null)
listener.onButtonSelected();
}
});
}
public void setTextt (String text) {
textView.setText(text);
}
在 FragmentTwo 中:
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button button = view.findViewById(R.id.button2);
EditText editText = view.findViewById(R.id.editText);
ListenerTwo listener = (ListenerTwo) getActivity();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "button clicked", Toast.LENGTH_SHORT).show();
if (listener != null)
listener.onButtonPressed(editText.getText().toString());
}
});
}
在 Activity
中,在 onButtonPressed
方法中,您当前将 FragmentTwo
设置为 FragmentOne
。请注意,当您设置 Fragment
时,您正在使用 fragmentTransaction.replace(
<- 替换!您正在将片段加载到 R.id.fragmentPlaceholder
,当设置新片段时,之前的片段将被销毁
里面onButtonPressed
你要重新设置FragmentOne
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragmentPlaceholder, new FragmentOne());
fragmentTransaction.commit();
这些行很常见,也放在 onCreate
中,因此它们可以打包到在这两个地方调用的一个方法中
为了将一些数据传输到片段中,您可以使用在构造函数中传递此对象,但更好的方法是使用 Bundle
,因此:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showFragmentOne(null);
}
@Override
public void onButtonPressed(String text) {
showFragmentOne(text);
}
private void showFragmentOne(String text) {
Fragment fragment = new FragmentOne();
if (text!=null){
Bundle bundle = new Bundle();
bundle.putInt("text", text);
fragment.setArguments(bundle);
}
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragmentPlaceholder, fragment);
fragmentTransaction.commit();
}
在方法检查结束时 onViewCreated
内 Bundle
并设置文本
Bundle bundle = this.getArguments();
if (bundle != null) {
String text = bundle.getString("text", null);
if(text!=null) setTextt(text); // or just textView.setText(text);
}
我有两个片段,我想在我的屏幕上一个一个地显示它们。它们由 MainActivity 管理。第一个片段包含一个 TextView 和一个 Button。第二个片段包含一个 EditText 和一个 Button。我想做的是:第一个片段显示在屏幕上,当用户按下按钮时,我希望我的 activity 显示第二个片段,当用户按下这个片段中的按钮时,我想发送从 EditText 到第一个片段的文本并将其显示在 TextView 上。我为听众使用了界面。
public class MainActivity extends AppCompatActivity implements Listener, ListenerTwo{
FragmentTransaction fragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragmentPlaceholder, new FragmentOne());
fragmentTransaction.commit();
}
@Override
public void onButtonSelected() {
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragmentPlaceholder, new FragmentTwo());
fragmentTransaction.commit();
}
@Override
public void onButtonPressed(String text) {
FragmentOne fragmentOne = (FragmentOne)getSupportFragmentManager().findFragmentById(R.id.fragmentPlaceholder);
if (fragmentOne != null)
fragmentOne.setTextt(text);
}
}
在我的 FragmentOne 中:
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button button = view.findViewById(R.id.button);
textView = view.findViewById(R.id.textView);
Listener listener = (Listener) getActivity();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "button clicked", Toast.LENGTH_SHORT).show();
if (listener != null)
listener.onButtonSelected();
}
});
}
public void setTextt (String text) {
textView.setText(text);
}
在 FragmentTwo 中:
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button button = view.findViewById(R.id.button2);
EditText editText = view.findViewById(R.id.editText);
ListenerTwo listener = (ListenerTwo) getActivity();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "button clicked", Toast.LENGTH_SHORT).show();
if (listener != null)
listener.onButtonPressed(editText.getText().toString());
}
});
}
在 Activity
中,在 onButtonPressed
方法中,您当前将 FragmentTwo
设置为 FragmentOne
。请注意,当您设置 Fragment
时,您正在使用 fragmentTransaction.replace(
<- 替换!您正在将片段加载到 R.id.fragmentPlaceholder
,当设置新片段时,之前的片段将被销毁
里面onButtonPressed
你要重新设置FragmentOne
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragmentPlaceholder, new FragmentOne());
fragmentTransaction.commit();
这些行很常见,也放在 onCreate
中,因此它们可以打包到在这两个地方调用的一个方法中
为了将一些数据传输到片段中,您可以使用在构造函数中传递此对象,但更好的方法是使用 Bundle
,因此:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showFragmentOne(null);
}
@Override
public void onButtonPressed(String text) {
showFragmentOne(text);
}
private void showFragmentOne(String text) {
Fragment fragment = new FragmentOne();
if (text!=null){
Bundle bundle = new Bundle();
bundle.putInt("text", text);
fragment.setArguments(bundle);
}
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragmentPlaceholder, fragment);
fragmentTransaction.commit();
}
在方法检查结束时 onViewCreated
内 Bundle
并设置文本
Bundle bundle = this.getArguments();
if (bundle != null) {
String text = bundle.getString("text", null);
if(text!=null) setTextt(text); // or just textView.setText(text);
}