Reputation: 1368
Can I use Roboguice @InjectView on elements that are included in a layout that will be inflated? It seems right to me that the elements are declaired @Nullable at first, as they are not known until the inflate is done. But after inflate, the elements still remain null until a "findViewById" is done.
As an example: I use the following code to inject a nullable textview (the title) that is included in title_layout that will be inflated in the main layout:
@Nullable @InjectView(R.id.title) TextView title;
@InjectView(R.id.title_info) LinearLayout titleContainer;
And then in onCreate() method I do this:
setContentView(R.layout.main);
inflater.inflate(R.layout.title_layout, titleContainer);
And I have the main.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/title_info">
</LinearLayout>
</LinearLayot>
and title_layout.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/title"
android:text="my title" />
</LinearLayot>
The title (TextView) from the inflated view is always null, even after the title_layout is inflated.
Doing this:
setContentView(R.layout.main);
inflater.inflate(R.layout.title_layout, titleContainer);
title = (TextView)findViewById(R.id.title);\
solves the problem.
Is there any way to use @InjectView?
Upvotes: 0
Views: 1196
Reputation: 41
I had the same problem - turns out I was extending Activity
instead of RoboActivity
, so it was never handling the injection.
Upvotes: 2