Q:

How to get LinearLayout in java code as container? 

I'm working on small project with google maps API.  And now I want to add button in container above marker whenever user clicks that marker.  I found that I can add new layer above my map and make my create my above marker manually.  I created main_map.xml as my layout file.  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/mapLayout">

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:id="@+id/map" tools:context=". MainActivity"
    android:name="com.google.android.gms.maps. SupportMapFragment"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="false"
    android:layout_alignParentBottom="false"
    android:layout_alignParentRight="false"
    class="com.example.project. MapFragment" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />
<AbsoluteLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <LinearLayout
        android:id="@+id/container_popup"
        android:layout_x="0dp"
        android:layout_y="0dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

    </LinearLayout>

</AbsoluteLayout>

So I wanted to get my LinearLayout in MainActivity.java I tried following code
public class MainActivity extends FragmentActivity {
private static final String TAG = "myLogs";
private GoogleMap mMap; // Might be null if Google Play services APK is not available. 
private static final int ANIMATION_DURATION = 500;

private LatLng trackedPosition;

//offset window that allows to correct position according to marker 
private int popupXOffset;
private int popupYOffset;
private int markerHeight;
//listener that will update the offset when the window is resized
private ViewTreeObserver. OnGlobalLayoutListener infoWindowLayoutListener;

private View infoWindowContainer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_map);
    infoWindowContainer = findViewById(R.id.container_popup);
    infoWindowLayoutListener = new InfoWindowLayoutListener();
    infoWindowContainer.getViewTreeObserver().addOnGlobalLayoutListener(infoWindowLayoutListener );
    setUpMapIfNeeded();
}

But problem is that infoWindowContainer is always null.  I cannot understand how to get it right. 
LogCat:
E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang. RuntimeException: Unable to start activity ComponentInfo{com.example.geoalarm/com.example.geoalarm. MainActivity}: java.lang. NullPointerException
        at android.app. ActivityThread.performLaunchActivity(ActivityThread.java:2211)
        at android.app. ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
        at android.app. ActivityThread.access$600(ActivityThread.java:141)
        at android.app. ActivityThread$H.handleMessage(ActivityThread.java:1256)
        at android.os. Handler.dispatchMessage(Handler.java:99)
        at android.os. Looper.loop(Looper.java:137)
        at android.app. ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect. Method.invokeNative(Native Method)
        at java.lang.reflect. Method.invoke(Method.java:525)
        at com.android.internal.os. ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os. ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system. NativeStart.main(Native Method)
 Caused by: java.lang. NullPointerException
        at com.example.geoalarm. MainActivity.onCreate(MainActivity.java:51)
        at android.app. Activity.performCreate(Activity.java:5133)
        at android.app. Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app. ActivityThread.performLaunchActivity(ActivityThread.java:2175)

            
at android.app. ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
          
at android.app. ActivityThread.access$600(ActivityThread.java:141)
            at android.app. ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os. Handler.dispatchMessage(Handler.java:99)
            at android.os. Looper.loop(Looper.java:137)
            at android.app. ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect. Method.invokeNative(Native Method)
            at java.lang.reflect. Method.invoke(Method.java:525)
            at com.android.internal.os. ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os. ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system. NativeStart.main(Native Method)

A:

Use LinearLayout instead of using View then cast it like this
LinearLayout infoWindowPopup = (LinearLayout) findViewById(R.id.container_popup);

