& ldquo; linearLayout can not be resolved or it is not a field & rdquo; with AdMob instructions by Google for Android

advertisements

I am trying to find a solution to my problem by an hour and I found some topics with my similar problem, but I don't understand how to resolve.

I was developing an app with Processing for Android and I wanted to try AdMob (just for learning, for now), so I followed instructions all over the net and now I am stuck.

To add ads in the app, I was following Google instructions to add the SDK of Play Service and to add banners, but I am stuck here:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.activity_main);
        initPdService();

        // Create an ad.
        adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId("ca-app-pu....83399");

        // Add the AdView to the view hierarchy. The view will have no size
        // until the ad is loaded.
        LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
        layout.addView(adView);

        AdRequest adRequest = new AdRequest.Builder()
        // .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        // .addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
                .build();

        // Start loading the ad in the background.
        adView.loadAd(adRequest);

    }

The problem is that it give me "linearLayout cannot be resolved or it is not a field", and I don't know how to resolve. I think that the problem is in findViewById(R.id.linearLayout), since the error is at "linearLayout" and not "LinearLayout".

I am sure that a lot of people find this problem, so I think that is a common problem. I already added import android.widget.LinearLayout;at the top of the sketch, so I don't know what is the problem.

Other infos: it is targeting Android SDK 19, as I know that Play Services want at least API level 13.

EDIT: Here is my [old] activity_main.xml found is /res/layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <android.support.v7.widget.Space
        android:id="@+id/space1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/adView"
        android:layout_marginTop="208dp"
        android:layout_toRightOf="@+id/adView" />

    <View
        android:id="@+id/view1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>

EDIT 2: This is my new activity_main.xml, with linearLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:orientation="horizontal" >

        <android.support.v7.widget.Space
        android:id="@+id/space1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/adView"
        android:layout_marginTop="208dp"
        android:layout_toRightOf="@+id/adView" />

    <View
        android:id="@+id/view1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />
 </LinearLayout>

</RelativeLayout>


You need to set your content view:

setContentView(R.layout.activity_main);

which you have commented out. Do you have an xml file "activity_main.xml" that contains the LinearLayout with an id, linearLayout?

If yout don't have a LinearLayout with the id linearLayout, you need to add one. Otherwise, it's trying to find a view (by id!) that doesn't exist. For example:

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:orientation="horizontal" >
...
 </LinearLayout>