-1

I have started learning how to code in Android Studio have run into a problem. I've searched for about 9 hours and tried everything I could think of with my limited understanding of android studio.

Details: After learning from this tutorial, I tried integrating banner ads just to learn how to do it. I used this tutorial to create the ad. I think the problem is because I have the setContentView function use my custom GamePanel class because if I use R.layout.activity_game (main layout) it doesn't give me an error, but I couldn't find a way to fix this. I tried to start another activity when the player dies, but that didn't seem to work. After, I tried to change the contentView from inside GamePanel update method, but all that did was stop the game and make it not respond to input.

Error:

06-09 12:48:22.584 6187-6187/coldwarcore.aliens E/AndroidRuntime: FATAL EXCEPTION: main
                                                              Process: coldwarcore.aliens, PID: 6187
                                                              java.lang.RuntimeException: Unable to start activity ComponentInfo{coldwarcore.aliens/coldwarcore.aliens.Game}: java.lang.NullPointerException
                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429)
                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
                                                                  at android.app.ActivityThread.access$800(ActivityThread.java:166)
                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                  at android.os.Looper.loop(Looper.java:136)
                                                                  at android.app.ActivityThread.main(ActivityThread.java:5584)
                                                                  at java.lang.reflect.Method.invokeNative(Native Method)
                                                                  at java.lang.reflect.Method.invoke(Method.java:515)
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
                                                                  at dalvik.system.NativeStart.main(Native Method)
                                                               Caused by: java.lang.NullPointerException
                                                                  at coldwarcore.aliens.Game.onCreate(Game.java:25)
                                                                  at android.app.Activity.performCreate(Activity.java:5447)
                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) 
                                                                  at android.app.ActivityThread.access$800(ActivityThread.java:166) 
                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
                                                                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                  at android.os.Looper.loop(Looper.java:136) 
                                                                  at android.app.ActivityThread.main(ActivityThread.java:5584) 
                                                                  at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                  at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 
                                                                  at dalvik.system.NativeStart.main(Native Method) 

Source code: Main Activity:

public class Game extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(new GamePanel(this));
    //if I put the ad code here, I get the error mentioned above
}

activity_game.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
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="coldwarcore.aliens.Game">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!" />
<com.google.android.gms.ads.AdView
    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="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
</RelativeLayout>

GamePanel:

public class GamePanel extends SurfaceView implements SurfaceHolder.Callback
{


public GamePanel(Context context)
{
    super(context);


    //add the callback to the surfaceholder to intercept events
    getHolder().addCallback(this);

    thread = new MainThread(getHolder(), this);

    //make gamePanel focusable so it can handle events
    setFocusable(true);
}

EDIT: I used this code to create an ad inside GamePanel.

mInterstitialAd = new InterstitialAd(getContext()); 
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712"); 
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(((Game)getContext()).getHASHID()) //gets device HASH ID
.build(); 
mInterstitialAd.loadAd(adRequest);

Then used: if (mInterstitialAd.isLoaded()) { System.out.println("ad is showing"); mInterstitialAd.show(); } else System.out.println("ad not loaded"); to check if it is loaded, but the if statement didn't write anything, so there must be a problem with the isLoaded() function. When this didn't work, I tried to force the show() method outside the if, because I know the ad must be already loaded by the time it has to show it, but that didn't work either. Is there any other way to make this work? Or is there any way to switch to another activity from GamePanel and then back again?

  • Post the code in #onCreate() causing the error. The error is caused by line 25 so post it – Nana Ghartey Jun 09 '16 at 21:29
  • Here it is: AdView mAdView = (AdView) findViewById(R.id.adView); //line 25 AdRequest adRequest = new AdRequest.Builder().build(); //line 26 mAdView.loadAd(adRequest); //line 27 – Cristiannic Jun 11 '16 at 22:37

1 Answers1

1

A null pointer exception is thrown because you don't use the activity_game.xml in your Game class.

Instead of setContentView(new GamePanel(this));

You should use setContentView(R.layout.activity_game);

However, since you need GamePanel to display your game's view. You should either add the adview programmatically or via xml.

Nana Ghartey
  • 7,901
  • 1
  • 24
  • 26
  • I tried to initialize the ad in the GamePanel function using: mInterstitialAd = new InterstitialAd(getContext()); mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712"); AdRequest adRequest = new AdRequest.Builder() .addTestDevice(((Game)getContext()).getHASHID()) .build(); mInterstitialAd.loadAd(adRequest); And then used the isLoaded() function to check if it is loaded, then used show() to show it, but isLoaded() doesn't seem to do anything and actually messes with my code. Even forcing show() doesn't work. – Cristiannic Jun 14 '16 at 14:11
  • I also tried to switch activities using Intent, but I can't find a way to do it without using an XML file. – Cristiannic Jun 14 '16 at 14:14
  • please update your original question – Nana Ghartey Jun 15 '16 at 05:17