我创建了一个仅使用活动和扩展View的游戏,问题是我不知道如何在我的活动中设置我的Admob,因为如果我不在我的sentContentView中放置一个布局,则说它为null。我已经尝试在活动中创建一个布局,它没有工作,我也尝试在xml中添加我的视图,也没有工作。我不知道如何解决这个问题。 我的菜单活动:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Menu menu = new Menu(this);
menu.setKeepScreenOn(true);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
setContentView(menu);
MobileAds.initialize(this, "ad_id_banner");
mAdView = (AdView) this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
我的菜单视图(我只会放置构造函数):
public class Menu extends View {
private Bitmap titleGraphic;
private Bitmap playButtonUp;
private Bitmap playButtonDown;
private Bitmap InstruButtonUp;
private Bitmap InstruButtonDown;
private Bitmap background;
private Bitmap back;
private boolean playButtonPressed;
private boolean instruButtonPressed;
private int screenW;
private int screenH;
private Context myContext;
public Menu (Context context){
super(context);
myContext = context;
titleGraphic = BitmapFactory.decodeResource(getResources(),R.drawable.title_graphic);
playButtonUp = BitmapFactory.decodeResource(getResources(),R.drawable.play_button_up);
playButtonDown = BitmapFactory.decodeResource(getResources(),R.drawable.play_button_down);
InstruButtonUp = BitmapFactory.decodeResource(getResources(),R.drawable.instru_button_up);
InstruButtonDown = BitmapFactory.decodeResource(getResources(),R.drawable.instru_button_down);
back = BitmapFactory.decodeResource(myContext.getResources(),R.drawable.frontbackground);
}
@Override
public void onSizeChanged(int w, int h, int oldw, int oldh)
{
super.onSizeChanged(w,h,oldw,oldh);
screenW = w;
screenH = h;
background = Bitmap.createScaledBitmap(back,screenW,screenH,true);
}
在这里,我为AdMob创建了一个XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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="com.acimutstudios.puzzle.MainActivity">
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/5224354917">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
logCat错误消息:
FATAL EXCEPTION: main
Process: com.acimutstudios.puzzle, PID: 6885
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.acimutstudios.puzzle/com.acimutstudios.puzzle.MenuActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.ads.AdView.loadAd(com.google.android.gms.ads.AdRequest)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.ads.AdView.loadAd(com.google.android.gms.ads.AdRequest)' on a null object reference
at com.acimutstudios.puzzle.MenuActivity.onCreate(MenuActivity.java:37)
at android.app.Activity.performCreate(Activity.java:6679)