1. Download our Official Android App: Forums for Android!

Apps component null before layout is drawn

Discussion in 'Android Development' started by 9tontruck, Jan 7, 2014.

  1. 9tontruck

    9tontruck Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    Jan 7, 2014

    Jan 7, 2014
    1
    0
    5
    I have two layout xmls.
    greeting.xml
    main.xml


    It's really nub question. I draw greeting layout at first and draw main layout when user clicks a button in the greeting layout. Problem is that components in the main layout are null because they are not drawn yet so I can't initiate components or give event listeners onto them.

    [HIGH]@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_facereader_greeting); //draw greeting layout first
    btnStart = (Button) findViewById(R.id.btnStart);
    btnStart.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    setContentView(R.layout.activity_facereader_main); //if button is click, main layout is drawn
    }
    });
    ArrayList<Entry> searchResults = GetSearchResults();
    ListView lv1 = (ListView) findViewById(R.id.entryList); //ListView in main xml
    lv1.setAdapter(new EntryListAdaptor(this, searchResults));
    //error around here because ListView is not drawn yet
    //I cannot put this code in onClick function because "this" is not onCreate
    }[/HIGH]
    How should I change this?
     

    Advertisement

Share This Page

Loading...