Question

In my main activity I'm trying to access a LinearLayout I have within my main ScrollView. My ScrollView is implemented as follows:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:id="@+id/graph" 
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="250dip"
         />



</ScrollView>

In my Main activity I inflate the scrollView and then try to access the linear layout as follows:

LayoutInflater inflater;
    inflater = (LayoutInflater)  this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);              

    ScrollView mainLayout = (ScrollView) inflater.inflate(R.layout.scrollview,
                    null);
    LinearLayout layout = (LinearLayout) findViewById(R.id.graph);

Why would my findViewById be returning null here? I thought at first it was trying to find it before it had finished inflating, but I've tried having a while loop to wait for it to finish inflating, and this doesn't help either.

Was it helpful?

Solution

you have to give it the inflated layout

LinearLayout layout = (LinearLayout) mainLayout.findViewById(R.id.graph);

OTHER TIPS

You can double check if specify the wrong layout in inflater.inflate() first.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top