App keeps crashing at start up after I used AdMob - android

My app just keeps crashing after I tryed to use banner ads with AdMob (without it everything works just fine). The SplashScreen loads but the app crashes when the game should start. Here are the steps that I followed to implemet this:
1) I made sure that I have installed Google Play Services and Google Repository
2) Then I connected my app to Firebase using Firebase Tool Assistant ( it just added google services.json under the app folder in my project )
3)Then I added the AdMob to my app, as shown here:
4)I added this codes in my layout:
xmlns:ads="http://schemas.android.com/apk/res-auto"
<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>
5)In my Main Activity class called "Game" I added this code to onCreate() method as you can see here:
public class Game extends Activity {
//ADMOB
private AdView mAdView;
MediaPlayer sound;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//turn title off
requestWindowFeature(Window.FEATURE_NO_TITLE);
//set to full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(new GamePanel(this));
//ADMOB
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
int[] sounds={R.raw.chiptune, R.raw.chiptune1, R.raw.chiptune2, R.raw.chiptune3};
Random r = new Random();
int Low = 0;
int High = 4;
int rand = r.nextInt(High-Low) + Low;
sound = MediaPlayer.create(getApplicationContext(),sounds[rand]);
sound.start();
sound.setLooping(true);
}
6) Add this to my strings: <string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string>
7) In my AndroidManifest.xml I added this pieces of codes:
<!-- Include required permissions for Google Mobile Ads to run-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<!--This meta-data tag is required to use Google Play services.-->
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<!--Include the AdActivity configChanges and theme. -->
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="#android:style/Theme.Translucent" />
I wounder what's wrong, if anybody can help!
Here is the Crash Log:

findViewById(R.id.adView); returns a View if it exists in the layout you provided in setContentView(), otherwise it returns null and that's what happening to you.
mAdView is null so you're getting null pointer exception.
So create a layout.xml, embed GamePanel view and AdView both then pass that layout to setContentView(); method.

Related

Cannot show video ads from Smaato Ads for my Android App

I am trying to implement Smaato Video Ads on my Android App.
I have followed the instructions from the Smaato page and the Smaato example app and yet, I cannot manage to show the Smaato Video Ads.
The code that apparently should show a video ad in fullscreen is:
private static Video smaatoVideoAd; // A Smaato Video global variable
smaatoVideoAd = new Video(this.getApplicationContext());
smaatoVideoAd.getAdSettings().setPublisherId(0); //int of 0 is supposedly a test mode
//smaatoVideoAd.getAdSettings().setPublisherId(PublisherID);
smaatoVideoAd.getAdSettings().setAdspaceId(0); //int of 0 is supposedly a test mode
//smaatoVideoAd.getAdSettings().setAdspaceId(VideoAd_ID);
smaatoVideoAd.setVastAdListener(this);
smaatoVideoAd.setAutoCloseDuration(5);
smaatoVideoAd.disableAutoClose(true);
smaatoVideoAd.setVideoSkipInterval(3);
} // End of onCreate
// Called when the video has been loaded.
#Override
public void onReadyToShow() {
// Call this when you want to show the video ad.
smaatoVideoAd.show();
}
#Override
public void onWillShow() {
// Called when the ad will show.
}
#Override
public void onWillOpenLandingPage() {
// Called when the banner has been clicked.
}
#Override
public void onWillClose() {
// Called when Interstitial ad will be closed.
}
#Override
public void onFailedToLoadAd() {
// called when video failed to load.
}
The Smaato libs for build.gradle has been setup, and that is pretty straightforward and short:
repositories {
...
flatDir {
dirs 'libs'
}
...
}
dependencies {
...
implementation name:'SOMAAndroid-9.1.3-release', ext:'aar'
...
}
And in the AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />
<uses-feature android:name="android.hardware.location.network" />
<meta-data android:name=”com.google.android.gms.version” android:value=”#integer/google_play_services_version”/>
<activity android:name="com.smaato.soma.interstitial.InterstitialActivity" android:resizeableActivity="false" android:screenOrientation="landscape" />
<activity android:name="com.smaato.soma.video.VASTAdActivity" android:resizeableActivity="false" android:screenOrientation="landscape" />
<activity android:name="com.smaato.soma.ExpandedBannerActivity" android:resizeableActivity="false" android:screenOrientation="landscape" />
Besides, Smaato declares that: "Since v8.0.0, the Smaato Android SDK contains an automatic initialization routine. Therefore there is no need for publishers to manually call an additional SDK initialization method".
But running this code, gives me the following alert in the Logcat:
SOMA_VIDEO: Video must be loaded before showing it
So, the video add is not loading at all. What am I missing here?
Can you help me? Thanks.
Solved! Apparently, I hadn't the correct sequence order. With this order, the warning in Logcat vanishes:
smaatoVideoAd = new Video(this.getApplicationContext());
smaatoVideoAd.setVastAdListener(this);
smaatoVideoAd.setAutoCloseDuration(3);
smaatoVideoAd.disableAutoClose(false);
smaatoVideoAd.setVideoSkipInterval(1);
smaatoVideoAd.getAdSettings().setPublisherId(0); // Trial Publisher Id: 0
smaatoVideoAd.getAdSettings().setAdspaceId(3090); // Trial Adspace Id for video: 3090
smaatoVideoAd.asyncLoadNewBanner();
Notice that the code for Smaato video ad in trial mode is 3090, not 0. Also, I have put the Smaato Ad in a new activity, so closing the ad will close the child activity and not my main one.

InMobiBanner is not initialized

I am getting this error while integrating InMobi Banner ad.
InMobiBanner is not initialized. Ignoring InMobiBanner.load()
I am using version 7.0.4 of InMobi Ads SDK. I have followed the instructions given in the documentation.
How can I fix this problem? Please help me regarding this.
Finally, I got the solution. Somehow, InMobiBanner is not working if the placement id is given in XML layout. So, we have to initialize the InMobiBanner programmatically with the Java code.
But complete your profile information and add your payment information before integrating the InMobi SDK. Also, don't create any placements before that.
Step 1:
Initialize the InMobiSdk in your Application file with the below code:
public class MyApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
InMobiSdk.init(this, "Your Account ID");
InMobiSdk.setLogLevel(InMobiSdk.LogLevel.DEBUG);
}
}
Step 2:
Add the Application file in your Manifest file and also make the hardwareAccelerated attribute of application tag to true. Check the sample code given below:
<application
android:name=".MyApplication"
android:hardwareAccelerated="true"
..
<activity
android:name="com.inmobi.rendering.InMobiAdActivity"
android:configChanges="keyboardHidden|orientation|keyboard|smallestScreenSize|screenSize|screenLayout"
android:resizeableActivity="false"
android:hardwareAccelerated="true"
android:theme="#android:style/Theme.NoTitleBar"
tools:ignore="UnusedAttribute" />
</application
Step 3:
Add a ViewGroup in your layout so that we can add the InMobiBanner view within that view.
<RelativeLayout
android:id="#+id/banner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center" />
Step 4:
Now add the below Java code in your Activity or Fragment to initialize and load the InMobiBanner ad.
InMobiBanner bannerAd = new InMobiBanner(this, 1234567890L);
RelativeLayout adContainer = findViewById(R.id.banner);
float density = getResources().getDisplayMetrics().density;
RelativeLayout.LayoutParams bannerLp = new RelativeLayout.LayoutParams((int) (320 * density), (int) (50 * density));
adContainer.addView(bannerAd, bannerLp);
bannerAd.load();
Hope this helps.
You need to create the InMobiBanner class instance inside UI Thread as it is not thread-safe.
Official documentation says -
Notes:
The InMobiBanner class is not thread-safe. A banner instance must be created on the UI thread.
Similarly, all methods on this instance must be called on the UI thread. Not doing so will lead to unpredictable behavior and may affect your ability to monetize with InMobi.
Hope this answer your question. Thanks
Also, you need to add these line android manifest file
<activity
android:name="com.inmobi.rendering.InMobiAdActivity"
android:configChanges="keyboardHidden|orientation|keyboard|smallestScreenSize|screenSize|screenLayout"
android:resizeableActivity="false"
android:hardwareAccelerated="true"
android:theme="#android:style/Theme.NoTitleBar"
tools:ignore="UnusedAttribute"/>

Android : How to integrate Flurry Banner Ads from new SDK 5.6.0

Here is my code I wanted to integrate Flurry Banner Ads at the bottom and Top of My App with their NEW SDK.. Please help
My Manifest.xml file
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Start of Flurry Permission-->
<activity
android:name="com.flurry.android.FlurryFullscreenTakeoverActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize">
</activity>
<!-- End of Flurry Permission -->
My Launcher.java Activity
import com.flurry.android.FlurryAdType;
import com.flurry.android.FlurryAds;
import com.flurry.android.FlurryAdSize;
import com.flurry.android.FlurryAgent;
import com.flurry.android.FlurryAdListener;
public class MainActivity extends Activity {
ProgressDialog progress;
private Context mContext;
FrameLayout mBanner;
public static String apiKey ;
private String adSpace;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getIntent().getExtras();
mBanner = (FrameLayout) findViewById(R.id.banner);
mContext = LauncherActivity.this;
apiKey = getResources().getString(R.string.flurry_api_key);
adSpace = getResources().getString(R.string.adSpaceName);
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
FlurryAds.fetchAd(mContext, adSpace, mBanner,
FlurryAdSize.BANNER_BOTTOM);
}
My MainScreen.xml Layout
<FrameLayout
android:id="#+id/banner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
My String File
<string name="flurry_api_key">3HVSXVC8XTBT8NCN5SCB</string>
<string name="adSpaceName">MyAdSpace Bottom</string>
The ad's are not displaying on my apps from this codes please let me know where is the problem and tell me how to solve it.
I am using FlurryAds-5.6.0.jar SDK. The error i m getting is Unfortunately Application Has Stop. without showing error in error catalog
Just follow instructions from https://developer.yahoo.com/flurry/docs/publisher/code/android/#banner-ads-integration-code.
And I think it would be better to edit your question and delete your API key as that is a data private to your app.
Also, please consider looking at your LogCat, as it could contain reason of not showing any ad. If your app is new, it could take some time to start appearing.
Enable logging by adding FlurryAgent.setLogLevel(2) before the FlurryAgent.init() call. (http://flurrydev.github.io/FlurryAndroidSDK5xAPI/classcom_1_1flurry_1_1android_1_1FlurryAgent.html#a0133e301ea0f01327b5143fc3974f3ac)
Although FlurryAds.fetchAd() method call is deprecated, it could still be used, if you just wanted to test. But, then, replace it with code from the above link.

AdActivity leak on AdMob (SDK 7.0) for Android

I have memory leaks regarding Interstitial ads of AdMob with AdActivity object. Whenever an ad is shown, AdActivity object count in the memory increments by 1. I inspect all changes via MAT after explicit GC calls. I use the latest versions of everything.
At first, I thought that this is related to how I implemented my UI or project, but creating a fresh and empty project shows the same leak.
This leak has also existed in the previous Admob SDK (Google Play Services) and now it exists in version 7.0 too.
I see that people try to solve these kinds of issues by creating a SingleInstance empty activity just to show and set as the context of the interstitial ads. I tried them all and they did not work for my case. Some did help but I even couldn't use it because of the flow of my app. launchMode in Android has limitations and it does not help me on my case.
I already notified the AdMob team but even they fix it, it doesn't seem to happen in a short time as they have just released the version 7.0 SDK.
I do not understand how others do not report leaks like this. It cannot be a special case just for me as it happens on even samples or default templates. If anyone somehow solved this issue (including ugly reflection hacks) please share your experience. I have been working on this for months! Really!
AndroidManifest:
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="22" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:name="MyApplication"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="#android:style/Theme.Translucent" />
</application>
MainActivity:
package com.example.leaktest1;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
private InterstitialAd interstitial=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
interstitial= new InterstitialAd(getApplicationContext());
interstitial.setAdUnitId("YOUR-ADD-ID");
AdRequest adRequest2 = new AdRequest.Builder()
// .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
interstitial.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
super.onAdLoaded();
if(interstitial.isLoaded()){
interstitial.show();
}
}
});
interstitial.loadAd(adRequest2);
}
#Override
protected void onDestroy() {
if(interstitial!=null){
interstitial.setAdListener(null);
interstitial=null;
}
super.onDestroy();
}
}
Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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="com.example.leaktest1.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
Start the app. When the ad is shown, close it with backpress and hit back button again to exit app.
App will still be in memory, but the activity is gone. Now touch the app icon to start the activity again, it will show the ad again, exit like you did before.
Cause GC multiple times and get heap dump. You will see that there are 2 AdActivity objects (and also many other related objects). It will continue to grow according to the number of shown ads.
The following did not work too (it still leaks):
/*
interstitial.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
super.onAdLoaded();
}
});*/
interstitial.loadAd(adRequest2);
Runnable r=new Runnable() {
#Override
public void run() {
if(interstitial.isLoaded()){
interstitial.show();
}
}};
new Handler().postDelayed(r,10000);
And putting code inside a button did not work too (it still leaks):
Button b = new Button(this);
b.setText("Touch me");
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(interstitial.isLoaded()){
interstitial.show();
}
}
});
ViewGroup v = (ViewGroup) this.findViewById(android.R.id.content);
v.addView(b);
Leak platform and exceptions:
This leak happens on various devices from Samsung and Asus with various un-modded (original firmware) Androis systems from 2.3 to 4.4. It also happens on any setup of android simulator.
(This leak does not show up on rooted Cyanogenmod (Galaxy S3) Android 4.4.4)
UPDATE
The leak does not vanish if I use Activity context instead of Application context. It also causes Activity leak too.

Upgrading AdMob SDK and fail on load() method

I've been working to update to the new AdMob that requires google services to function.
I'm using this code in onCreate()
AdView adView = (AdView)this.findViewById(R.id.adView);
//AdRequest
AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
adView.loadAd(adRequest);
Log.d("ADMOB", "Successfully loaded");
and am using this code in XML
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center_horizontal"
ads:adSize="SMART_BANNER"
ads:adUnitId="ADMOB_ID"/>
However, the adMob ad does not display. The try/catch block fails here:
adView.loadAd(adRequest);
The AndroidManifest code looks like this:
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
What am I missing?
Referring to the AdMob Android Guides I quote
Next, create the onActivityCreated method. This is where you'll build and load the AdRequest. Reference the AdView, then build and load the AdRequest.
So, you must implement it like following :
#Override
public void onActivityCreated(Bundle bundle)
{
super.onActivityCreated(bundle);
AdView mAdView = (AdView) getView().findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}

Categories

Resources