gpt4 book ai didi

java - 使用 Otto 时订户不会被解雇

转载 作者:太空宇宙 更新时间:2023-11-03 11:33:22 25 4
gpt4 key购买 nike

我正在 Android 上试用 Otto,我正在尝试将一条消息从我的 Fragment 发送回 Activity。这是我的代码的基础知识:

我的巴士供应商:

public final class BusProvider {

private static final Bus mInstance = new Bus();

private BusProvider() {}

public static Bus getBusProviderInstance() {
return mInstance;
}
}

我的 Activity 有以下代码:

public class MyActivity
extends BaseActivity {

// ....

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
BusProvider.getBusProviderInstance().register(this);
// ....
}

@OnClick(R.id.profile_image)
public void onImageClicked() {

// ...

MyFragment fragment = MyFragment.newInstance(choices);
fragment.show(getFragmentManager(), "myChoices");
}

@Subscribe
public void onButtonChoicePicked(MyFragment.ChoicePickedEvent event) {
Toast.makeText(this, "reaching here", Toast.LENGTH_SHORT).show();
}

@Override
protected void onStop() {
super.onStop();
BusProvider.getBusProviderInstance().unregister(this);
}

// ...
}

这些是我 fragment 中的重要代码:

public class MyFragment
extends BaseDialogFragment {

// ...

@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {

LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_choices,
container,
false);
setupDialogButtons(inflater, layout);
return layout;
}



private void setupDialogButtons(LayoutInflater inflater, LinearLayout parentView) {
ChoiceButtonViewHolder holder;

holder = new ChoiceButtonViewHolder(inflater, parentView);
holder.populateContent("First Choice", 1);

parentView.addView(holder.mChoiceTextView);
}


class ChoiceButtonViewHolder {

@InjectView(R.id.item_dialog_choice_desc) TextView mChoiceTextView;
private int mPosition;

ChoiceButtonViewHolder(LayoutInflater inflater, ViewGroup container) {
TextView mChoiceTextView = (TextView) inflater.inflate(R.layout.item_dialog_choice, container, false);
ButterKnife.inject(this, mChoiceTextView);
}

public void populateContent(String choiceDesc, int position) {
mChoiceTextView.setText(choiceDesc);
mPosition = position;
}

@OnClick(R.id.item_dialog_choice_desc)
public void onChoiceClicked() {
MyFragment.this.mDialog.dismiss();
BusProvider.getBusProviderInstance().post(new ChoicePickedEvent(1));
}
}



public static class ChoicePickedEvent {
public int mPositionClicked;
ChoicePickedEvent(int position) {
mPositionClicked = position;
}
}
}

我没有收到任何错误。但是当我从 fragment 中单击我的按钮时,不会调用 onButtonChoicePicked 事件。

我做错了什么吗?我误解了奥托的工作方式吗?是 ButterKnife 和 Otto 的奇怪组合导致它不起作用吗?

最佳答案

确保您导入的是“com.squareup.otto.Subscribe”而不是“com.google.common.eventbus.Subscribe”

关于java - 使用 Otto 时订户不会被解雇,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22278135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com