开发者

android extended LinearLayout, added XML with LayoutInflater, findViewById is null

开发者 https://www.devze.com 2023-02-15 11:31 出处:网络
I am creating a custom / extended LinearLayout which I am adding to a ViewAnimator obviously for cool effects.

I am creating a custom / extended LinearLayout which I am adding to a ViewAnimator obviously for cool effects.

So my main Activity has a ViewAnimator and I create an instance of this NavigationView I have been working on and add it to the ViewAnimator as a subview. The NavigationView itself uses LayoutInflater to grab the xml for the interface I made and attaches it to itself.

I then try to find this spinner which I know has the correct id and it always returns null. If I use this XML as the main content view of an Activity, I have no problem finding the spinner.

Here is my code:

  public class NavigationView extends LinearLayout {

    public NavigationView(Context context) {
        super(context);
        addView(context);
    }

    public NavigationView(Context context, AttributeSet attrs) {
        super(context, attrs);
        addView(context);
    }

    public void addView(Context context) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.nav, this, true);

        Spinner spinner = (Spinner) findViewById(R.id.nav_spinner);
        if(spinner == null) Log.v("spinner", "null");

    } 
   }

So I always 开发者_StackOverflow中文版see "spinner" as "null" in the LogCat.

I see the interface I created added to the screen, but it's driving me nuts why it wont find this object by the id.

Could it be since it's inflated that it doesn't correctly assign ids?


So not sure why, given a few good suggestions and toying around with the code I ended up getting it to work:

public class NavigationView extends LinearLayout {

    public NavigationView(Context context) {

        super(context);
        this.init(context);

    }

    public void init(Context context) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.nav, this, true);

        Spinner spinner = (Spinner) v.findViewById(R.id.navSpinner);
        if(spinner != null) {

            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(context, R.array.nav_sections, android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(adapter);
        }

    }

}

It seems like the same exact code, maybe I just needed to clean my project after I took out the underscore?


You are inflating you view at the wrong time. Remove the addView method, Override the onFinishInflate() method and do the inflation here. e.g

@Override
    protected void onFinishInflate() {
            super.onFinishInflate();
            ((Activity)getContext()).getLayoutInflater().inflate(R.layout.nav, this);

             Spinner spinner = (Spinner) findViewById(R.id.nav_spinner);
             if(spinner == null) Log.v("spinner", "null");
    }


You need to call findViewById on the view you've inflated. So that means you need to try this:

Spinner spinner = (Spinner) v.findViewById(R.id.nav_spinner);
0

精彩评论

暂无评论...
验证码 换一张
取 消