Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7691581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:39:29+00:00 2026-05-31T20:39:29+00:00

I’m fairly new to Android, and am trying to build a custom component in

  • 0

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).

Within my class that extends FrameLayout, I’d like to set up the spinner button, which I’ve laid out in xml. However, calling findViewById from within that class returns null. 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.

Can anyone tell me what I’m doing wrong, or how I should be approaching this?

Here’s a simplified version of my code:

My custom FrameLayout class:

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!");
            }

    }

}

My activity class is huge, but the oscilliscope is inflated on a button press, so the relevant section (i think) is:

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]);
}

Here’s my Oscilloscope.xml file, which is inflated by my main program:

<?xml version="1.0" encoding="utf-8"?>
<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" >

    <com.example.myoscilloscope.gui.Oscillator
        android:id="@+id/oscillator"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

       <Spinner 
        android:id="@+id/channel_spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
    />


</com.example.myoscilloscope.gui.Oscilloscope>

And here’s the relevant bit of my main.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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" >

    <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" >


    </LinearLayout>
</RelativeLayout>

Hopefully that makes some sense. Any insights would be much appreciated.

Tom

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T20:39:31+00:00Added an answer on May 31, 2026 at 8:39 pm

    Whoops sorry didn’t notice those were different views… this should fix your problem.

    You should not be using findViewById in the constructor because the inflation has not finished at this point. Move the findViewById code into an overriden function called onFinishInflate().

        public class Oscilloscope extends FrameLayout {
    
            private Spinner chanSelector; //the channel selector
    
            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);
            }
    
            @Override
            public void onFinishInflate(){
                    super.onFinishInflate();        
    
                    chanSelector = (Spinner) findViewById(R.id.channel_spinner);
    
                    if (chanSelector == null) {
                        //Should never get here
                    }
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.