AdMob simple tutorial doesn't work - android

I'm in an early process of developping a game for Android and I'm trying to incorporate an ad banner using AdMob. I have downloaded the sample straight from the tutorial on the official site, so I guess that whatever I'm doing wrong here has to be basic since it crushes after a few seconds when debugging on my Galaxy S2 device. Pleae help.
package com.google.example.ads.fundamentals;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.LinearLayout;
/**
* A simple {#link Activity} that embeds an AdView.
*/
public class BannerSample extends Activity {
private AdView adView;
private final TelephonyManager tm =
(TelephonyManager)getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
private final String AD_MOB_ID = "my AdMob ID goes here";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create an ad.
adView = new AdView(this, AdSize.BANNER, AD_MOB_ID);
// Add the AdView to the view hierarchy. The view will have no size
// until the ad is loaded.
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
layout.addView(adView);
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(tm.getDeviceId());
// Start loading the ad in the background.
adView.loadAd(adRequest);
}
/** Called before the activity is destroyed. */
#Override
public void onDestroy() {
// Destroy the AdView.
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
}
The Logcat data in a screen shot is here
Edit: Also adding the Manifest.XML, which I suspect is what causeing the problems - amazingly the one that came with the example from the official site had an error in it (according to Eclipse), so I had to modify it a bit:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="3"
android:targetSdkVersion="13" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name">
<activity
android:name=".HelloAdMobActivity"
android:label="#string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:screenOrientation="landscape" >
</activity>
</application>
</manifest>

Logcat information would be super useful, but I'm betting on one of these two reasons:
You referenced the SDK as an external Jar, and didn't add it into your libs/ folder. You have two options here to fix this: add it to your libs/ folder, or go to Properties -> Java Build Path -> Order and Export and check the AdMob jar.
You don't have a LinearLayout in your XML with android:id="linearLayout". This is less likely as the sample project should include this.

Alright, so it turns out the problem is with tm.getDeviceId(), which for whatever reason caused the program to crush.

You need to add the following permission.
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

Related

First html page opens but clicking any link within that page causes crash the app

My WebView open a local html file when the app starts. It opens perfectly but any link within that page causes a crash of the app when clicked.
On the bug report, it says,
signal 6 (SIGABRT), code-6 (SI_TKILL), fault addr ----- Abort message: 'art/runtime/java_vm_ext:cc:475] JNI DETECTED ERROR IN APPLICATION: JNI NewLocalRef called with pending exception adroid.os.FileUriExposedException: file///android_asset/page2.html exposed beyond app through Intget.getData()
Similar problems have been addressed so many times before but even after digging different solutions for some time, I haven't been able to solve the problem. Any help would be appreciated.
My MainActivity.java page:
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
public class MainActivity extends Activity {
private WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Load WebView with HTML from Assets folder
myWebView = (WebView) findViewById(R.id.webView1);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("file:///android_asset/page1.html");
}
#Override
public void onBackPressed() {
if (myWebView.canGoBack())
myWebView.goBack();
else
super.onBackPressed();
}
}
My AndroidManifest.xml page:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.html">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name="com.rimikri.PRx.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
As the documentation says: "This exposure is discouraged since the receiving app may not have access to the shared path. For example, the receiving app may not have requested the READ_EXTERNAL_STORAGE runtime permission, or the platform may be sharing the Uri across user profile boundaries." (https://developer.android.com/reference/android/os/FileUriExposedException.html)
Try to add READ_EXTERNAL_STORAGE permission: https://developer.android.com/training/permissions/requesting.html

The Crosswalk framework for Android

I have a few questions about Crosswalk, I tried searching on Google, but since I don't know the exact names of some functions, I can't find anything.
Is it possible to use Crosswalk to render a online website?
If the answer to question 1 is no, Is it possible to write a HTML5+JS app, that renders an iframe, with in there a online site?
If question 1 is yes, can you post a link to a documentation or a tutorial?
Yes it is possible to do both action.
here is some help on the official web site. and here is a code that might work for you too
import android.app.Activity;
import android.os.Bundle;
import java.util.logging.Logger;
import org.xwalk.core.XWalkView;
public class MainActivity extends Activity {
/**
* Called when the activity is first created.
*/
Logger logger = Logger.getLogger("xwl");
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
XWalkView view = new XWalkView(this, this);
logger.severe("loadeding ...");
view.load("https://www.google.com", null);
logger.severe("loaded");
setContentView(view);
}
}
Here is the manifest file
?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wxl"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application android:label="#string/app_name"
android:icon="#drawable/ic_launcher" android:hardwareAccelerated="true"
android:largeHeap="true">
<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>
</application>
</manifest>
The two permissions are needed.
The only problem I found is that it renders blurry :( on android.
Good luck.

Android addPreferencesFromResource

I decided to learn android and I bought a book Hello, Android Introducing Google's Mobile Development Platform, Third Edition. I started to do my first project Sudoku from the book and I have managed to do it until when I need it create class Prefs extends PreferenceActivity. The method addPreferencesFromResource(R.XML.Settings) do not work any more with PreferenceActivity. I am trying to fix this problem for two day without success for.
What I have so far! I read that I could manage it via PerformanceFragment and I try to follow this link: http://developer.android.com/reference/android/preference/PreferenceFragment.html and I did following:
public class Prefs extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
It appears an error and Eclipse recommend to add #TargetApi(Build.VERSION_CODES.HONEYCOMB)
when I did it, I could compile it and run my emulator but when I press menu and settings I have received message Unfortunately, Sudoku has stopped.
Other codes in mainActivity I have:
package org.example.sudoku;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.MenuInflater;
public class MainActivitySudoku extends ActionBarActivity implements OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity_sudoku);
//setting up click listener for all buttons
View continueButton = findViewById(R.id.continu_button);
continueButton.setOnClickListener(this);
View newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(this);
View aboutButton = findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
View exitButton = findViewById(R.id.exit_button);
exitButton.setOnClickListener(this);
}
// other codes...
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.settings:
startActivity(new Intent(this, Prefs.class));
return true;
}
return false;
}
inside Sudoku/res/xml/settings.xml I have xml codes:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference
android:key="music"
android:title="#string/music_title"
android:summary="#string/music_summary"
android:defaultValue="true" />
<CheckBoxPreference
android:key="hints"
android:title="#string/hints_title"
android:summary="#string/hints_sammary"
android:defaultValue="true" />
</PreferenceScreen>
Last thing to mention is AndroidManifest Sudoku/bin/AndroidManifest.xml I have following codes:
$ <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.example.sudoku"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivitySudoku"
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=".About"
android:label="#string/about_title"
android:theme="#android:style/Theme.Dialog" >
</activity>
<activity android:name=".Prefs"
android:label="#string/settings_title"
</activity>
</application>
</manifest>
Well, could anyone please help me how to fix this problem. I have looked at on some examples but I could not figure it out. Also if anybody knows a book that would be better to follow exercises it would be great! Thank you David
The method addPreferencesFromResource(R.XML.Settings) do not work any more with PreferenceActivity
It should. It is deprecated, but it should still work.
I have received message Unfortunately, Sudoku has stopped
I'm pretty sure that Ed covers LogCat in his book, which is how you learn more about problems like this.
In this case, you are probably getting a ClassCastException. Your Intent for startActivity() is:
new Intent(this, Prefs.class)
However, Prefs is a PreferenceFragment, not an Activity. You need to have your <activity> element point to an Activity, presumably a PreferenceActivity, one that uses your PreferenceFragment.
Also if anybody knows a book that would be better to follow exercises it would be great!
Asking for off-site resources is considered off-topic for Stack Overflow. That being said, I recommend you pick a book that was published this year. The book that you are using was published four years ago, and that is a long time in Android development.

Layout does not appear in android emulator

I'm new to Android programming and i'm doing tutorial from layouts from http://developer.android.com/resources/tutorials/views/index.html . Relative layout tutorial exactly. I did everything they say but when I try to start app in android emulator there are no buttons, textfields etc. Only name of app shows at the top. What is wrong? Is this a problem with emulator? I'm using Eclipse Version: 3.7.1.
EDIT:
package pchot.tutorial;
import android.app.Activity;
import android.os.Bundle;
public class Tutorial1Activity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pchot.tutorial"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".Tutorial1Activity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Layout is copy/paste from http://developer.android.com/resources/tutorials/views/hello-relativelayout.html
EDIT:
Ok, problem solved. I needed to delete "Hello World, Tutorial1Activity!" from string.xml file which was autocreated during creation of project in Eclipse.
Have you modified you manifest.xml file? Maybe you haven't modify it to make it aware that the launched main activity is the activity you've just created.
You didn't declare anything in your onCreate() method.
For Button declare Button btn = (Button)findViewById(R.id.btnSubmit)
Implement onClickListener if you want the button to listen to clicks.
For TextView, declare TextView tv = (TextView)findViewById(R.id.textview)

admob banner doesn't not get shown

I know this have been discussed many times but I couldn't menage to make this work :(. I really tried but I didn't make it.
I have add the GoogleAdMobAdsSdk-4.0.4.jar to my libs folder and I add it to build path.
What else should I do ? I do not see my banner, it never get shown
I guess I do something very stupid but I can not realize what
this is my activity
package com.google.ads.example;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
public class BannerEssentials extends Activity{
private static final String MY_BANNER_UNIT_ID = "123";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Lookup R.layout.main
LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout);
// Create the adView
// Please replace MY_BANNER_UNIT_ID with your AdMob Publisher ID
AdView adView = new AdView(this, AdSize.BANNER, MY_BANNER_UNIT_ID);
// Add the adView to it
layout.addView(adView);
// Initiate a generic request to load it with an ad
AdRequest request = new AdRequest();
request.setTesting(true);
adView.loadAd(request);
}
}
this is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.ads.example"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".BannerEssentials"
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.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation"/>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-sdk android:minSdkVersion="3" />
</manifest>
this is my layout main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
</LinearLayout>
Are you compiling your project against android API 13? Check out admob requirements:
Requirements
The Google AdMob Ads SDK for Android requires Android 1.5 or later. Make sure you have the latest copy of the Android SDK and that you're compiling against at least Android v3.2 (set target in default.properties to android-13).
If this does not help, try download and use the latest admob sdk from their official website.
Try to add admob banner in xml file( http://code.google.com/mobile/ads/docs/android/banner_xml.html )
And remember that you won't always see ad in applicatio, even if everything works fine(read what is a fill rate)
Are you setting MY_BANNER_UNIT_ID to your actual publisher ID? If not, you need to go to the AdMob website, and sign up and create a site/app to get your publisher ID.
FYI, 4.0.4 is a pretty old version of the SDK. I would recommend upgrading to 4.3.1 instead. 4.3.1 requires a couple new configChanges in your manifest and requires you to compile against android-13 (4.0.4 does NOT), but it is a more stable and is a more well supported version. You can find documentation on integrating 4.3.1 here.
You need to manually add your app on the admob site. It will ask you for the name and what type of ad banner or interstitial. Mine was not showing even though I had publisher ID, until I manually added it and hit the finish button at the bottom of the page.

Categories

Resources