Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: How to access xml resources from within an inflated custom component
    text
    copied!<p>I'm fairly new to Android, and am trying to build a custom component in my android application. To do this, I'm extending FrameLayout, so that my custom component can consist of a spinner on top of a view (I'll eventually be drawing in the view, but it's just blank at the moment). </p> <p>Within my class that extends FrameLayout, I'd like to set up the spinner button, which I've laid out in xml. However, <strong>calling findViewById from within that class returns null.</strong> From reading answers to similar problems on here, I gather that I need to call findViewById from an activity (and indeed, if I set up the spinner from a class that extends activity, everything works fine). One solution then would be to pass an activity in to my FrameLayout class's constructor. However, I never call my FrameLayout class's constructor myself, because I use a layoutinflater to inflate it. I assume layout inflater is calling my FrameLayout class's constructor, and I'm not sure how to add an argument to its constructor call. </p> <p>Can anyone tell me what I'm doing wrong, or how I should be approaching this?</p> <p>Here's a simplified version of my code:</p> <p>My custom FrameLayout class:</p> <pre><code>package com.example.myoscilloscope.gui; import android.app.Activity; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.widget.FrameLayout; import android.widget.Spinner; import com.example.myoscilloscope.R; public class Oscilloscope extends FrameLayout { //variables private Spinner chanSelector; //the channel selector // overloading constructors public Oscilloscope(Context context) { this(context, null, 0); } public Oscilloscope(Context context, AttributeSet attrs) { this(context, attrs, 0); } public Oscilloscope(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); //set up the spinner. chanSelector = (Spinner) findViewById(R.id.channel_spinner); if(chanSelector == null){ Log.d("FROMTOM", "CHANNELSELECTOR IS NULL!"); } } } </code></pre> <p>My activity class is huge, but the oscilliscope is inflated on a button press, so the relevant section (i think) is:</p> <pre><code>import com.example.myoscilloscope.gui.Oscilloscope; // ... public void addchannel(View v){ //this initialization is actually done elsewhere, but I've copied it here // for simplicity's sake private LayoutInflater oscilloscopeInflater; LinearLayout myOscilloscopeHolder; oscilloscopeInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); myOscilloscopeHolder = (LinearLayout)findViewById(R.id.oscilloscopeHolder); Oscilloscope myOscilliscope = (Oscilloscope)oscilloscopeInflater.inflate(R.layout.oscilloscope, myOscilloscopeHolder, false); myOscilloscopeHolder.addView(trOscilloscopes[trChannelsDisplayed]); } </code></pre> <p>Here's my Oscilloscope.xml file, which is inflated by my main program:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;com.example.myoscilloscope.gui.Oscilloscope xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/oscilloscope" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" &gt; &lt;com.example.myoscilloscope.gui.Oscillator android:id="@+id/oscillator" android:layout_width="fill_parent" android:layout_height="fill_parent" /&gt; &lt;Spinner android:id="@+id/channel_spinner" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" /&gt; &lt;/com.example.myoscilloscope.gui.Oscilloscope&gt; </code></pre> <p>And here's the relevant bit of my main.xml:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/background_color" &gt; &lt;LinearLayout android:id="@id/oscilloscopeHolder" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_above="@id/rg_channels" android:layout_toLeftOf="@id/HistogramArea" android:orientation="vertical" android:background="#FFFFFF" &gt; &lt;/LinearLayout&gt; &lt;/RelativeLayout&gt; </code></pre> <p>Hopefully that makes some sense. Any insights would be much appreciated.</p> <p>Tom</p>
 

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