Note that there are some explanatory texts on larger screens.

plurals
  1. POInjected views remain null for inflated layouts
    primarykey
    data
    text
    <p>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.</p> <p>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:</p> <pre><code>@Nullable @InjectView(R.id.title) TextView title; @InjectView(R.id.title_info) LinearLayout titleContainer; </code></pre> <p>And then in onCreate() method I do this:</p> <pre><code>setContentView(R.layout.main); inflater.inflate(R.layout.title_layout, titleContainer); </code></pre> <p>And I have the <b>main.xml</b>:</p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/title_info"&gt; &lt;/LinearLayout&gt; &lt;/LinearLayot&gt; </code></pre> <p>and <b>title_layout.xml</b>:</p> <pre><code> &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/title" android:text="my title" /&gt; &lt;/LinearLayot&gt; </code></pre> <p>The title (TextView) from the inflated view is always <b>null</b>, even after the title_layout is inflated. </p> <p>Doing this:</p> <pre><code>setContentView(R.layout.main); inflater.inflate(R.layout.title_layout, titleContainer); title = (TextView)findViewById(R.id.title);\ </code></pre> <p>solves the problem.</p> <p>Is there any way to use @InjectView?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload