gpt4 book ai didi

android - android fragment 之间的通信

转载 作者:行者123 更新时间:2023-11-30 03:53:21 24 4
gpt4 key购买 nike

我有两个 fragment 。我正在使用界面将一个 fragment 与另一个 fragment 进行通信:

public class FragmentLanguages extends ListFragment {
LanguageArrayAdapter adapter;
OnLanguageChangedListener mCallback;

public interface OnLanguageChangedListener {
public void onLanguageSelected(int position);
}

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

View view = inflater.inflate(R.layout.languages, null);

return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Language languages[] = new Language[] {
new Language(R.drawable.ic_flag_german, "German"),
new Language(R.drawable.ic_flag_spanish, "Spanish"),
new Language(R.drawable.ic_flag_russian, "Russian"),
new Language(R.drawable.ic_flag_ukrainian, "Ukrainian") };

adapter = new LanguageArrayAdapter(getActivity(), R.layout.list_row,
languages);
getListView().setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
adapter.setSelectedItem(position);
mCallback.onLanguageSelected(position + 1);
}
});
setListAdapter(adapter);
}

public static class Language {
public int image;
public String text;

public Language() {
super();
}

public Language(int image, String text) {
super();
this.image = image;
this.text = text;
}
}

public void setOnLanguageChangedListener(OnLanguageChangedListener mCallback) {
this.mCallback = mCallback;
}

还有一个

public class FragmentCircles extends Fragment implements OnClickListener,
OnLanguageChangedListener { ...
.....
@Override
public void onLanguageSelected(int position) {
currentLang = position;
translationCircle.removeAllViews();
String strSelect = "SELECT Translation FROM Translation WHERE Lang_Id="
+ currentLang + " ORDER BY Word_Id ASC";
translationList = DataBaseHelper.getInstance(getActivity())
.SelectTranslationList(strSelect);
Log.i("Lang ", "changed " + currentLang);


calculateCentreTranslation(lLabelRadius, width, height);
}

但是在屏幕旋转之后,当我在 ListFragment 中选择一些项目时,它给我一个错误

12-07 19:06:15.163: E/AndroidRuntime(436): java.lang.NullPointerException

在线

                mCallback.onLanguageSelected(position + 1);

mCallback 似乎为空。但是我该如何解决呢?

最佳答案

您的FragmentLanguages 必须通知您的FragmentActivity,后者又通知其他 fragment 。这确保没有 fragment 链接在一起,如果一个 fragment 在另一个通知它时未实例化,则可能触发 NullPointerExceptions

因此您的界面 OnLanguageChangedListener 保留在您的 LanguagesFragment

您的FragmentActivity必须实现setOnLanguageChangedListener

FragmentLanguages 中:

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
onLanguageChangedListener = (onLanguageChangedListener) activity;
}

由于 onAttach() 甚至在 onCreate() 之前被调用,您可以随时在 fragment 中使用:

onLanguageChangedListener.onLanguageChanged(...);

在实现 OnLanguageChangedListenerFragmentActivity 中,您需要重写它:

@Override
public void onLanguageChanged(...) {
// here you implement the logic to notify other fragments
}

关于android - android fragment 之间的通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13769486/

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