我使用此链接中的Bundle方法:https://stackoverflow.com/a/21102881/2641960
但我遇到了这个错误:
-15 11:14:47.323 8558-8558/co.kssg.expertnote E/InputEventReceiver﹕ Exception dispatching input event.
04-15 11:14:47.323 8558-8558/co.kssg.expertnote E/MessageQueue-JNI﹕ Exception in MessageQueue callback: handleReceiveCallback
04-15 11:14:47.333 8558-8558/co.kssg.expertnote E/MessageQueue-JNI﹕ java.lang.IllegalStateException: Activity has been destroyed
以下是将数据包发送到片段的方法:
public void sendDataToFragment (String data)
{
Bundle bundle = new Bundle();
MainActivity ac = new MainActivity();
FragmentManager fManager = ac.getFragmentManager();
FragmentTransaction transaction = fManager.beginTransaction();
bundle.putString("message", data );
DataFragment dataFragment = new DataFragment();
dataFragment.setArguments(bundle);
transaction.replace(R.id.sFLayout, dataFragment);
transaction.commit();
}
此方法放在自定义ImageView类中。
感谢您的帮助。
答案 0 :(得分:2)
我认为这MainActivity ac = new MainActivity();
无效。
您应该在ImageView类的构造函数中添加一个引用,如下所示:
public class ImageView {
private Activity mainActivity;
public void ImageView(Activity mainActivity) {
this.mainActivity = mainActivity;
}
public void sendDataToFragment (String data){
Bundle bundle = new Bundle();
FragmentManager fManager = mainActivity.getFragmentManager();
FragmentTransaction transaction = fManager.beginTransaction();
bundle.putString("message", data );
DataFragment dataFragment = new DataFragment();
dataFragment.setArguments(bundle);
transaction.replace(R.id.sFLayout, dataFragment);
transaction.commit();
}
}