-1

This is my Fragment's Code:

EFragment
public class PreviewFragment extends Fragment
{
    @Inject Bus bus;
    @Inject HorstDb db;

    //@ViewById(R.id.preview_text_view)
    TextView previewTextView;

    public PreviewFragment() {}

    @Override public void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        ((HorstApp)getActivity().getApplication()).getComponent().inject(this);
    }

    @Override public View onCreateView(LayoutInflater inf, ViewGroup parent, Bundle args)
    {
        super.onCreateView(inf, parent, args);
        View result = inf.inflate(R.layout.fragment_preview, parent, false);
        previewTextView = (TextView) result.findViewById(R.id.preview_text_view);
        return result;
    }

    @Override
    public void onResume() { super.onResume(); bus.register(this); }
    @Override
    public void onPause() { super.onPause(); bus.unregister(this); }

    @Subscribe
    public void onTemplateSelected(TemplateSelected se)
    {

        Cursor c = db.getReadableDatabase().query("templates", null, "_id = ?",
                                                                                            new String[] { Long.toString(se.id) },
                                                                                            null, null, null);

        if(c.moveToFirst())
        {
            previewTextView.setText(c.getString(c.getColumnIndex("content")));
        }
        c.close();
    }
}

onTemplateSelected will get called in onResume via Otto. This works and previewTextView will be non-null. The following version does not work, because previewTextView is null also I'am in the innermost lifecyle of an fragment and I am using @ViewsById. The difference is that I do not manually overload onCreateView, instead I'm using @EFragment and @ViewById to get a reference to the TextView.

@EFragment(R.layout.fragment_preview)
public class PreviewFragment extends Fragment
{
    @Inject Bus bus;
    @Inject HorstDb db;

    @ViewById(R.id.preview_text_view)
    TextView previewTextView;

    public PreviewFragment() {}

    @Override public void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        ((HorstApp)getActivity().getApplication()).getComponent().inject(this);
    }

    @Override
    public void onResume() { super.onResume(); bus.register(this); }
    @Override
    public void onPause() { super.onPause(); bus.unregister(this); }

    @Subscribe
    public void onTemplateSelected(TemplateSelected se)
    {

        Cursor c = db.getReadableDatabase().query("templates", null, "_id = ?",
                                                                                            new String[] { Long.toString(se.id) },
                                                                                            null, null, null);

        if(c.moveToFirst())
        {
            previewTextView.setText(c.getString(c.getColumnIndex("content")));
        }
        c.close();
    }
}

This is using the Fragment class from the Support library.

Any hints why the annotations don't work and previewTextView is null in the second example are welcome.

Panke
  • 156
  • 9
  • previewTextView is null in the second example and the NPE is raised because I call `setText`on it. The question is, why is it null. – Panke Dec 28 '15 at 12:56
  • 1
    In the first example, you set `previewTextView` in `onCreateView()`. In the second example, it is set in `onViewCreated()`. Maybe your `onTemplateSelected()` is called before `onViewCreated()`? Also are you sure you are using the generated class `PreviewFragment_`? – WonderCsabo Dec 28 '15 at 16:11
  • Wasn't using PreviewFragment_, thanks! – Panke Dec 28 '15 at 20:11

1 Answers1

0

I was using PreviewFragment instead of PreviewFragment_.

Panke
  • 156
  • 9