1

我有两个片段。我正在使用接口将一个片段与另一个片段进行通信:

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 为空。但是我该如何解决呢?

4

3 回答 3

1

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

所以你的界面OnLanguageChangedListener留在你的LanguagesFragment

FragmentActivity 必须实施setOnLanguageChangedListener

FragmentLanguages

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

onAttach()甚至在onCreate()您可以随时在片段中使用之前就调用了因为:

onLanguageChangedListener.onLanguageChanged(...);

在您的FragmentActivitywhich 实现OnLanguageChangedListener中,您需要覆盖它:

@Override
public void onLanguageChanged(...) {
    // here you implement the logic to notify other fragments
}
于 2012-12-07T19:35:52.450 回答
0

在 android 中,当您从纵向切换到横向或反之时,将重新创建活动,因此如果您想保留一些数据,您必须使用 Bundle saveInstanceState 来管理这些活动。

于 2012-12-07T19:27:10.217 回答
0

mCallBack 侦听器仅被声明但从未实例化。您应该使用 onAttach() 方法中的容器活动来实例化 mCallBack 侦听器。在此处查看如何操作。 http://developer.android.com/training/basics/fragments/communicating.html#DefineInterface

于 2012-12-07T22:16:40.757 回答