简体   繁体   中英

Null LinearLayout?

I'm trying to use a LinearLayout for implementing ads on mobile devices, because I saw it used in a tutorial put up by Google. However, my LinearLayout keeps returning null, and I haven't been able to find a solution for a long time.

Here is my MainActivity.java:

package com.me.mygdxgame;


import android.os.Bundle;
import android.widget.LinearLayout;
//import android.widget.RelativeLayout;

//import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
//import com.google.ads.AdSize;
//import com.google.ads.AdView;
import com.me.mygdxgame.R;

public class MainActivity extends AndroidApplication {

    AdView adView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
        cfg.useGL20 = true;

        initialize(new MyGame(), cfg);

        adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId("XXXXConfidential Ad Unit IDXXXX");

        LinearLayout layout = (LinearLayout) findViewById(R.id.normal);
//      layout.addView(adView);
        if (layout ==null){
            System.out.println("layout doesn't work");
        }
        if (layout !=null){
            System.out.println("layout works");
        }
        if (adView == null){
            System.out.println("adView doesn't work");
        }
        if (adView != null){
            System.out.println("adView works");
        }

        AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice("XXXXConfidential Test Device CodeXXXXXXX")
        .build();

        if (adRequest == null){
            System.out.println("adRequest doesn't work");
        }
        if (adRequest != null){
            System.out.println("adRequest works");
        }

        adView.loadAd(adRequest);
//      setContentView(layout);
    }

    public void onDestroy(){
        if (adView != null) {
            adView.destroy();
          }
          super.onDestroy();
    }
}

The System.out.println s are telling me that everything works except for the layout

Here's my main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/normal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
</LinearLayout>

Here's a snippet from my R.java file, in my package:

public static final class id {
    public static final int hybrid=0x7f050004;
    public static final int none=0x7f050000;
    public static final int normal=0x7f050001;
    public static final int satellite=0x7f050002;
    public static final int terrain=0x7f050003;
}
public static final class integer {
    public static final int google_play_services_version=0x7f070000;
}
public static final class layout {
    public static final int main=0x7f030000;
}

I'm not familiar at all with XML, so I don't know what the problem is. I've followed all of the tutorials very closely, yet my layout is still returning null. Any help? I've been trying to solve it for more than 24 hours...

You don't show the code for #initialize but I'm willing to bet that somewhere in that method you are calling #setContentView again and overwriting R.layout.main

That is why you cannot find R.is.normal because it doesn't exist in whatever you have replaced R.layout.main with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM