Reputation: 79
I have developed an Android app with libGDX and have added Admob to it, but when I open the APK on an Android it crashes, stating that the process has stopped.
Here is my code in the Android project.
public class MainActivity extends AndroidApplication{
protected AdView adview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = true;
final TelephonyManager tm =(TelephonyManager)getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
String deviceid = tm.getDeviceId();
RelativeLayout layout = new RelativeLayout(this);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
setContentView(graphics.getView(), createLayoutParams());
View gameview = initializeForView(new BalloonBreakout(), false);
adview = new AdView(this);
adview.setAdSize(AdSize.BANNER);
adview.setAdUnitId("ca-app-pub-6258330641042393/6188790266");
adview.loadAd(new AdRequest.Builder().addTestDevice(deviceid).build());
RelativeLayout.LayoutParams adparams = new RelativeLayout.LayoutParams(Gdx.graphics.getWidth(), Gdx.graphics.getHeight() / 14);
adparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
layout.addView(gameview);
layout.addView(adview, adparams);
setContentView(layout);
}
}
I'm not sure why it's crashing, any help would be greatly appreciated.
UPDATE:
I've got the error log here, but I don't know why I'm getting a null pointer.
E( 3875) Caused by: java.lang.NullPointerException (AndroidRuntime)
E( 3875) at com.sevenbit.Balloon_Breakout.MainActivity.onCreate(MainActivity.java:37)(AndroidRuntime)
Line 37 is:
setContentView(graphics.getView(), createLayoutParams());
However when I deleted this line, I still got a null pointer at the same place, line 37.
Any ideas?
Upvotes: 0
Views: 282
Reputation: 20196
Your problem (like all the other libgdx posts I have seen lately) is that libgdx AndroidApplication#initializeForView call Activity#setContextView to set its own layout (which it shouldn't). You then call setContextView with your own layout.
It crashes presumably because some libgdx code is assuming that it's layout object has been loaded but it's not there because you have replaced it with your own.
Upvotes: 1
Reputation: 3723
You should post or look at your stack trace. That will tell you what line your code is failing at. Otherwise people will have to guess from looking at your code. The problem may be anywhere.
Upvotes: 0