Android-Onfailedtoreceivead(ad request successful) occurs even in test mode? - android

I have been trying to get ads running on my android app however they just wont appear. I searched numerous websites but none of them had the answer. Eventually I started using this tutorial http://www.smartlab.at/articles/admob_advertising_android_tutorial/. I downloaded the example and it worked perfectly so I copied the code. However ads don't show up for my app.
I know receiving a "onfailedtorecieve(ad request successful) due to lack of ad inventory"
error means that my code is fine, however the thing is that I am in test mode, and during my numerous hours of trying to figure how to add ads, I discovered that ads should always appear in testmode even if there is a lack of inventory. I entered my device id that I acquired from logcat and everything but still no ads appear. Here is my code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:tools="http://schemas.android.com/tools"
xmlns:myapp="http://schemas.android.com/apk/res/com.example.test"
android:id="#+id/main_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:debuggable="true"
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=".MainActivity" >
<com.google.ads.AdView
android:id="#+id/adview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="AD UNIT ID"
ads:loadAdOnCreate="true"
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
/>
I made sure to replace "TEST_DEVICE_ID" with my actual device id and the "AD UNIT ID" with the ad unit id I got from admob.
Here is my code in my activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE); //hide title bar
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout layout=(RelativeLayout)findViewById(R.id.main_view);
adView = (AdView)this.findViewById(R.id.adview);
adView.loadAd(new AdRequest());
}
Like I said nothing shows up. However if I put my ad_unit_id in the example i downloaded from the tutorial website, it works perfectly.
Here is their code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<com.google.ads.AdView
android:id="#+id/adview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="a1501676c633fac" <!-- This is the adunitid in the tutorial i downloaded-->
ads:loadAdOnCreate="true"
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
/>
</LinearLayout>
Here is their activity code:
package at.smartlab.androidbook.AdMobTest;
import com.google.ads.AdRequest;
import com.google.ads.AdView;
import android.app.Activity;
import android.os.Bundle;
public class AdMobTestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Look up the AdView as a resource and load a request.
AdView adView = (AdView)this.findViewById(R.id.adview);
adView.loadAd(new AdRequest());
}
}
Interestingly, I don't have to put my actual device id in the xml code of the example I downloaded, it works fine whether I do put my id or I don't. Also, I can put either my adunitid or use theirs, it works fine either way.
I made sure to add the proper permissions in my androidmanifest.xml file and the proper tags. Honestly, I don't see why ads don't show up on my app but do in the example I downloaded. Any help would be appreciated. Thank you.

First off you're using an old tutorial.
The tags should now be
Com.Google.android.gms.adView.. Don't mind the random capital letters I'm on a mobile phone. You also need to make sure you have an SDK for google play services
XML
//at the top
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_alignParentTop="true"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="your new pub Id goes here ca-app-something">
</com.google.android.gms.ads.AdView>
java
//calling ads
adView=(AdView)findViewById(R.id.adView);
AdRequest adRequest=new. AdRequest.Builder().build();
adView.loadAd(adRequest);
manifest
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize">
</activity>
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version"/>
code to test the adview
adView=(AdView)findViewById(R.id.MyAdView);
 AdRequest adRequest=new.AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).addTestDevice("7XXXXXXXXXXXXXXXXXXXXX").build();
adView.loadAd(adRequest);

Related

Android AdMob panel says banner ad is active but it's not showing in my app

I tested a banner ad with a test ID and it was showing fine, after that I procceeded to change the adUnitId to the actual Id AdMob gave me and I initialized MobileAds in application clas. I published my app on the play store, linked it to AdMob, and configured a payment profile. AdMob says the banner ad is active, but I downloaded my app and reinstalled it and the ads are not showing. Could i be missing some step?
Relevant Code:
this is the application class:
public class MyApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
MobileAds.initialize(getApplicationContext(), "ca-app-pub-8983537837429131~3925299209");
}
The activity where the banner ad is supposed to show:
public class EntryActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_entry);
Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
final AdView adView = (AdView)this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
And it's xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/entryActivityLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/libs/com.google.ads‌​"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
tools:context="com.bastardo.francisco.fitjournal.activities.EntryActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
<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="My-banner-ad-Id"/>
</RelativeLayout>
As i said, test ads were showing just fine but the real ads are not showing. My app is linked with the play store in AdMob and i already configured a payment profile. Is there any other step? How long do I have to wait for apps to display?
Sometime the ads take few hours to show up, this is because there is not enough data to show relevant ads in your app.
If you app is current in the playstore try to download it in other device with diferent Google account, if you can se the ads so there is no problem.

App keeps crashing at start up after I used AdMob

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.

Android: video returns to the beginning when device is rotated.

I have this activity that I use to play videos.
public class OtherActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
VideoView view = (VideoView) findViewById(R.id.videoView1);
view.setMediaController(new MediaController(this));
view.setVideoURI(Uri.parse("http:somevideo.mp4"));
}
}
And here is my layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<VideoView
android:id="#+id/videoView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"/>
</RelativeLayout>
Now, the problem here is that whenever I rotate the device the video goes back to the beginning. How can I stop this?
Your activity is simply restarting when it rotates. This is default behavior of Android.
Document yourself about android rotations and configChanges.
To make a device rotation a configuration change add the following code to the <activity> tag in the AndroidManifest.xml.
android:configChanges="orientation|screenSize"
Note: The device configuration is a set of characteristics that describe the current state of the device. The characteristics that make up the configuration include screen orientation, screen size, language and others.
I think this will be happened on activity restart.
Please add below line in activity tag of AndroidMainfest.
<activity
android:name="com.test.MainActivity"
android:configChanges="orientation|screenSize">
..........
..........
</activity>

android: Cannot switch between activities using intent

I have a hard time switching from one activity to another using intent. My app is almost done, I have there several activities and I am able to switch between them with intent. I have added one more activity to my project (in Eclipse: File ... New ... AndroidActivity). As I was used to, it created .java and .xml file. In my main activity, I have defined my button (xml) and in java class method to perform on buttonClick. Everything seems fine, but when I click my button nothing happens. What could be wrong?
Here is defined my main activity class called "Ponuka":
package com.example.s_home;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
public class Ponuka extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
getWindow().setBackgroundDrawableResource(R.drawable.pozadie);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ponuka);
ActionBar actionBar = getActionBar();
actionBar.hide();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_ponuka, menu);
return true;
}
// switch to activity: Osvetlenie --- works fine
public void obrOsv(View view) {
Intent intent = new Intent (this, Osvetlenie.class);
startActivity(intent);
}
// switch to activity: Kurenie --- works fine
public void obrKur(View view) {
Intent intent = new Intent (this, Kurenie.class);
startActivity(intent);
}
// switch to activity: Ochrana --- this isnt working
public void obrBez(View view) {
Intent intent = new Intent (this, Ochrana.class);
startActivity(intent);
System.out.println("ok"); // It will not even print out "ok"
}
}
Here is activity_ponuka.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:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="150dp"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/lin1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
>
<Button
android:id="#+id/bezpecnost" <!-- THIS IS THE BUTTON -->
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/zabezp"
android:onClick="obrBez"
/>
<Button
android:id="#+id/kuren"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:background="#drawable/kurenie"
android:onClick="obrKur" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/lin2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/osvetl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/osvetlenie"
android:onClick="obrOsv"
/>
<Button
android:id="#+id/pohod"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/pohodlie"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
</RelativeLayout>
One more note: When my new activity called "Ochrana" was created, I had to manually update the R.java file. What did I do wrong here?
You shouldn't use System.out.println on Android, rather log the message using the Log class, then you can find it in LogCat. System.out.println might get redirected to LogCat too, but it's not guaranteed. Why doesn't "System.out.println" work in Android?
Is your problem that it starts a different activity than you want or doesn't it start one at all? Because you're creating an intent with Kurenie.class where you obviously want to start the Ochrana activity (according to the comment above the obrBez() method).
PS: Zdravim z Brna. :)
You should never manually update the R.java because it automatically updates itself if your code is correct.
You also need to get an instance of your button in your activity and add a OnClickListener to call the methods that switch the activity.
And the last thing you need to do is to add those activities you want to use in your manifest file.
Probably unrelated to your issue but you're calling the wrong Activity in your new button event:
public void obrBez(View view) {
Intent intent = new Intent (this, Kurenie.class);
I would recommend using setOnClickListener() for the buttons instead of the android:onClick in the XML - XML ment for layout and design and java activity files ment for logic, mixing the two can cause confusion. You can also try that for solving your problem (Although I don't see any problem with your code):
Button btn = (Button) findViewById(R.id.bezpecnost);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Assuming you want Ochrana activity from your comment
Intent intent = new Intent (this, Ochrana.class);
startActivity(intent);
}
});
Two more things that won't solve your problem but might help you:
You might want to define Intent intent; for the entire activity class and than create new intent on the same variable to save memory allocating.
And last thing - give your XML file [Ctrl]+A and than [Ctrl]+i to automatically order the spacing - it will do only good (:
P.S.
If you have any problems with your R file - just delete it! Eclipse auto re-create it immediately - solving all kinds of R bad/not updated...
The answer is in your XML layout.... look closely...
<Button
android:id="#+id/bezpecnost" <!-- THIS IS THE BUTTON -->
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/zabezp"
android:onClick="obrBez"
/>
The comment should rather be in this format <!-- .... //-->
You left out the two forward slashes - that's why nothing is happening!
Edit:
Eclipse would have shown the entire block commented out highlighted in colour green instead of XML syntax highlight colour!
In regards to R.java - that is generated at compile time, so whenever you make changes to your XML layouts, the Android SDK will re-generate the R.java file each time!
The AndroidManifest.xml should have the lines specifying the activity within the tags application, as in:
<activity android:name="com.example.s_home.Ochrana"
android:label="Ochrana Activity"/>
Your xml is all wrong. Your button with "#+id/pohod" is mixed up with button "#+id/bezpecnost" because you missed android:layout_below="#id/lin1" in your second linear layout. I tested the layout below and it is OK. I change to android:text because I have no drawable.
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="150dp"
>
<LinearLayout
android:id="#+id/lin1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
>
<Button
android:id="#+id/bezpecnost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ochrana"
android:onClick="obrBez"
/>
<Button
android:id="#+id/kuren"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Kurenie"
android:onClick="obrKur" />
</LinearLayout>
<LinearLayout
android:id="#+id/lin2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:gravity="center"
android:layout_below="#id/lin1"
android:orientation="horizontal" >
<Button
android:id="#+id/osvetl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="osvetlenie"
android:onClick="obrOsv"
/>
<Button
android:id="#+id/pohod"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="pohodlie"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
</RelativeLayout>

Android open new window problem

very easy question, i'm embarrassed to ask but cannot find it out on my own.
In MainActivity.java there is a menu. When user clicks on the menu item, a new window should appear but the app crashes ("the app stopped unexpectedly") instead.
MainActivity.java part:
case R.id.Menu6:
Intent intentabout = new Intent(this, About.class);
startActivity(intentabout);
break;
The case should be right, as the other menu items are working.
About.java:
public class About extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aboutxml);
TextView tv1 = (TextView)findViewById(R.id.TextView01);
tv1.setText("Something");
setContentView(tv1);
}
}
aboutxml.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:text="Something"
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
I have included the class in the AndroidManifest.xml:
<activity
android:name=".About"
android:label="#string/app_name">
</activity>
I can't believe i don't know this, i have other class in my app and they are working...
You shouldn't be calling setContentView twice. Remove the second call. That may or may not be your problem, but it needs to go. If that doesn't fix it, you need to post your error log. If you view it yourself, you'll probably figure it out pretty easily, but if not, post it here.

Categories

Resources