在活动和片段之间进行通信

时间:2016-02-20 16:35:11

标签: android

我正在编写Story app,我希望用户可以更改内容的大小,字体。值的更改在活动中完成,并通过接口发送到片段。但是我遇到了界面问题(NullPointer),onSettingChangedListener在我调用setOnSettingChangedListener时不为空,但是当我签入setSize()时,则 null 。请告诉我为什么?

这是代码

ChapterActivity.class

public class ChapterActivity extends AppCompatActivity {

    private OnSettingChangedListener onSettingChangedListener;

    private static ChapterActivity mInstances;

    public static ChapterActivity getInstance() {
        if (mInstances == null)
            mInstances = new ChapterActivity();
        return mInstances;
    }

    private void setSize(int mode) {
        text_size = mode;
        SharedManager.putInt(mContext, PREF_TEXT_SIZE, text_size);
        if (onSettingChangedListener != null)
            onSettingChangedListener.onTextSizeChanged(mode);
        Log.d(TAG, "setSize: " + (onSettingChangedListener == null ? " Null" : " not null"));
    }

    public interface OnSettingChangedListener {
        public void onTextSizeChanged(int mode);

        public void onTextFontChanged(String fontName);
    }

    public void setOnSettingChangedListener(OnSettingChangedListener onSettingChangedListener) {
        this.onSettingChangedListener = onSettingChangedListener;
        Log.d(TAG, "setOnSetting: " + (onSettingChangedListener == null ? " Null" : "Not null"));
    }
}

ChapterFragment

public class ChapterFragment extends Fragment 
    implements ChapterActivity.OnSettingChangedListener {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.content_chapter, container, false);
        mContext = getActivity();

        txtContent = (TextView) view.findViewById(R.id.txtContent);
        txtTitle = (TextView) view.findViewById(R.id.txtTitle);
        // Add Listener
        ChapterActivity.getInstance().setOnSettingChangedListener(this);

        return v;
    }


    @Override
    public void onTextSizeChanged(int mode) {
        Log.d(TAG, String.valueOf(mode));
        if (mode == ChapterActivity.TEXT_SIZE.SMALL.id) {
            setTextAppearance(android.R.style.TextAppearance_Material_Small);
        } else if (mode == ChapterActivity.TEXT_SIZE.MEDIUM.id) {
            setTextAppearance(android.R.style.TextAppearance_Material_Medium);
        } else {
            setTextAppearance(android.R.style.TextAppearance_Material_Large);
        }
    }

    @Override
    public void onTextFontChanged(String fontName) {
        Log.d(TAG, fontName);
    }
}

logcat的:

setOnSetting: not null setSize: null

1 个答案:

答案 0 :(得分:3)

不要将您的活动视为单身人士。拥有getInstance()方法不起作用,因为Android框架处理为您创建活动,您自己创建的活动将不会按照您的想法使用。

您需要获取当前附加ChapterFragment的活动并设置回调。

更改:ChapterActivity.getInstance().setOnSettingChangedListener(this);
收件人:((ChapterActivity) getActivity()).setOnSettingChangedListener(this)

或删除该行,并将以下方法添加到ChapterFragment

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    ((ChapterActivity) activity).setOnSettingChangedListener(this);
}

如果您使用片段支持库或定位API 23+,新的onAttach方法会将上下文作为参数:

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    ((ChapterActivity) context).setOnSettingChangedListener(this);
}