从Otto事件中的片段访问资源

时间:2015-02-20 16:53:15

标签: android android-fragments otto

我正在使用Otto在Fragment中调度事件方法。 但是,由于某种原因,此片段方法无法访问资源(R.style。*)。

我认为当它在Otto触发的事件中运行时,该片段与其主实例“断开连接”。

我读到我应该在onCreateView()中将元素设置为实例方法 - 这是我为其他元素所做的。

如何从此订阅者方法中访问R.style。*(或任何资源)?

我目前收到以下错误消息:

  

无法调度事件:类java.util.ArrayList到处理程序   [EventHandler public void   com.myapp.fragments.SubjectFragment.subjectsAvailable(java.util.ArrayList中)〕:   尝试调用虚拟方法'android.content.res.Resources   关于空对象引用的android.content.Context.getResources()'

public class Subjects extends AbstractDao {

    public Subjects(Context context)
    {
        super(context);
        mContext = context;
    }

    @Produce
    public ArrayList<Subject> getAll()
    {
        final ArrayList<Subject> list = new ArrayList<>();
        AsyncHttpClient client = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        RequestHandle post = client.get("http://api.local", params, new     AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] response) {
                try {
                    JSONArray subjects = new JSONArray(response);
                    if (subjects.length() > 0) {
                        for (int i=0; i <subjects.length(); i++) {
                            // populate object here
                            list.add(subject);
                        }
                    }
                    Bus bus = AppBus.getInstance();
                    bus.post(list);
                } catch (JSONException e) {
                    Log.d("JSONException", e.getMessage());
                } catch (Exception ex) {
                    Log.d("General Exception", ex.getMessage());
                }
            }
        });
        return list;
    }


public class SubjectFragment extends BaseFragment {

    Subjects subjectsDao;
    AssetManager currentAssets;
    LinearLayout menuLayout;

    public FreePracticeFragment() {
    }

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

        AppBus.getInstance().register(this);
        subjectsDao = new Subjects(getActivity());
        ArrayList<Subject> subjects = subjectsDao.getAll();
        View view = inflater.inflate(R.layout.fragment_free_practice, container, false);
        // populate here some elemnts in the view
        return view;

    @Subscribe
    public void subjectsAvailable(ArrayList<Subject> subjects) {
        int hexVal;
        SpannableStringBuilder ss;
        Typeface tf_r = Typeface.createFromAsset(currentAssets, "fonts/lato_bold.ttf");
        Typeface tf_icon = Typeface.createFromAsset(currentAssets, "fonts/fontawesome_webfont.ttf");

        for (int i = 0; i < subjects.size(); i++) {
            final ToggleButton button = new ToggleButton(getActivity(),     null, R.style.MultiSubjectButton);
            button.setId(subjects.get(i).subjectId);
            hexVal = Integer.parseInt(subjects.get(i).iconText, 16);
            ss = new SpannableStringBuilder((char)hexVal + "  " + subjects.get(i).name);
            ss.setSpan(new CustomTypeface("", tf_icon), 0, 1,    Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
            ss.setSpan(new CustomTypeface("", tf_r), 1, subjects.get(i).name.length() + 2, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
            button.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
            button.setBackgroundResource(R.drawable.btn_subject_normal);
            button.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (button.isChecked()) {
                       button.setBackgroundResource(R.color.btn_subject_normal);
                    button.setChecked(false);
                } else {
                    button.setBackgroundResource(R.color.btn_subject_pressed);
                    button.setChecked(true);
                }
                return false;
            }
        });

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.FILL_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
        );
        menuLayout.addView(button, layoutParams);
    }
}

}

我对这些类和代码的设计感到抱歉,我是android编程的新手,只是想用一个简单的例子来测试Otto。

我的想法是在onCreateView()和DAO内部调用DAO,以便在Web服务返回结果后调度带有结果的事件。这将直接提供片段方法,以便我可以填充列表视图。

如果有人可以在这里告诉我,我将不胜感激。

谢谢大家。

更新1: 添加源代码以避免混淆。

0 个答案:

没有答案