Android google ads error (null reference ?) - java

I am actually stuck in this error for the last 24h and still can't find solution
the error description:
11-06 18:53:37.745: E/AndroidRuntime(1698): Caused by:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
com.google.android.gms.ads.AdView.loadAd(com.google.android.gms.ads.AdRequest)'
on a null object reference
this is some of the xml layout :
<?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="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background_pattern"
android:orientation="vertical" >
.
.
.
.
.
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-xxxxxxx/xxxxxxxx" />
<TextView
android:id="#+id/tv_05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
oncreate method in mainActivity.java :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdView adview = (AdView) this.findViewById(R.id.adView);
// Request for Ads
AdRequest adRequest = new AdRequest.Builder()
// Add a test device to show Test Ads
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR).addTestDevice("")
.build();
adview.loadAd(adRequest);
// Prepare the Interstitial Ad
interstitial = new InterstitialAd(mainActivity.this);
// Insert the Ad Unit ID
interstitial.setAdUnitId(AppConstants.Interstitial_Id);
// Load ads into Interstitial Ads
interstitial.loadAd(adRequest);
.....
i can't see any error in this i checked all imports libraries everything is ok
Please help me I'm running out of time :/

It is failing because it is not finding an element with id adView in your layout.
Make sure the XML file you displayed above is actually activity_main.xml.
And do a clean build.

in XML replace this
xmlns:ads="http://schemas.android.com/apk/res-auto"
instead of
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"

I faced Similar issue. When i checked closely in resourse file There were two resourse file for my activity:
\res\layout\activity_main_lauch.xml
\res\layout-v21\activity_main_lauch.xml
I was modifing single file, hence it was throwing error.
when i apply the change in both files it started working.
Hope it might help.

You need to put this into your com.google.android.gms.ads.AdView and in your LinearLayout :
xmlns:ads="http://schemas.android.com/apk/res-auto"
EDIT:
You need to pull ID number of your test device from LogCat. Just use the AdManager method and set it to TEST_EMULATOR like the logcat says. If you run on an actual device with usb debugging and watch the logcat, the ID will appear in there.

Related

Anchored adaptive banners in LinearLayout structure

Good afternoon!
I am updating the normal(smart) admob banners for admob Anchored adaptive banners, I am following the example of google but I have several doubts.
I use LinearLayout as a general structure and not RelativeLayout like the google example.
When using the code to display the banner, it underlines two lines.
Example new code banner admob:
<FrameLayout
android:id="#+id/ad_view_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_alignParentBottom="true" />
Yellow underline in android studios (in my code):
android:layout_centerInParent="true"
Message or warning:
Invalid layout param in a LinearLayout: layout_centerInParent
android:layout_alignParentBottom="true"
Message or warning:
Invalid layout param in a LinearLayout: layout_alignParentBottom
Example complet code banner google-admob:
https://github.com/googleads/googleads-mobile-android-examples/blob/master/java/admob/AdaptiveBannerExample/app/src/main/res/layout/activity_my.xml
In the application the test ads are displayed well and work. But I don't like the messages of the underlined code (to avoid problems)
Before I used this code for my SMART banners:
<LinearLayout
android:id="#+id/ad_view_container"
android:orientation="vertical"
android:gravity="bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
Could I use the same code for Anchored adaptive banners? or does it have to be FrameLayout..?
thank you for answers
Try This
android:layout_gravity="center_horizontal|bottom"

AdMob ad doesn't show until I reopen the app

I'm trying to put an AdMob banner into my app but half the time it doesn't show any content.
The AdListener even receives an call to onAdLoaded and I never get any error.
I noticed that if the ad isn't showing and I put the app in the background somehow the ad starts to show. For example if I:
close and reopen the app
start an fullscreen interstitial ad
start the in-app purchase overlay
All these actions trigger an update that somehow redraws/remeasures the views.
I tried many different things to simulate this in onAdLoaded.
Is it possible that AdMob can't find a matching ad even if I don't get any errors? According to the website the match rate is 98%. The number of impressions is about a third of the requests.
I found more posts with similar issues (all from ~7 years ago) but none of their answers worked in my case:
Admob not showing up until phone is locked
AdMob won't show the banner until refresh or sign in to google plus
Android AdMob: addView doesn't show ad until return to activity
Admob ads appear only after first refresh
I tried to get this to work for over two days. Please take a look at my code, I annotated it with explainations:
MainActivity:
private AdView adView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
if(privacyPolicyAcceptedAndHasNoPremium()){ //at first i check the shared preferences
binding.adPlaceholder.setVisibility(View.VISIBLE); //the adPlaceholder starts as GONE when i set it to VISIBLE the rest of the content slides up
List<String> testDevices = Arrays.asList(AdRequest.DEVICE_ID_EMULATOR, "...");
RequestConfiguration requestConfiguration = new RequestConfiguration.Builder().setTestDeviceIds(testDevices).build();
MobileAds.setRequestConfiguration(requestConfiguration);
MobileAds.initialize(MainActivity.this, initializationStatus -> {});
adView = new AdView(this); //i create the adView programmatically but using the xml view element causes the same problem
adView.setAdSize(AdSize.LARGE_BANNER);
adView.setAdUnitId("...");
adView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() { // this gets called so i think the ad is successfully downloaded from the google server. i put all kinds of code in here to force the adView to show
//setContentView(binding.getRoot());
//binding.adPlaceholder.addView(new View(MainActivity.this));
//binding.adPlaceholder.forceLayout();
//binding.constraintLayout.requestLayout();
//adView.invalidate();
//adView.bringToFront();
//adView.setVisibility(AdView.GONE);
//adView.setVisibility(AdView.VISIBLE);
//adView.setBackgroundColor(Color.TRANSPARENT);
}
}
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
binding.adPlaceholder.addView(adView); //i add the adView into the empty LinearLayout. i tried to add it directly to binding.constraintLayout too.
adView.loadAd(adRequestBuilder.build());
}
activity_main.xml:
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
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">
<!-- this is the empty container into which i load the ads. it sits at the bottom of the screen and is not visible before an ad is showing -->
<LinearLayout
android:id="#+id/adPlaceholder"
android:layout_width="match_parent"
android:layout_height="100dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:orientation="vertical"
android:visibility="gone" />
<!-- this MotionLayout contains the rest of the app (except the toolbar). it slides up when an ad is showing -->
<androidx.constraintlayout.motion.widget.MotionLayout
android:id="#+id/motionLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layoutDescription="#xml/activity_main_scene"
app:layout_constraintBottom_toTopOf="#+id/adPlaceholder"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:context=".MainActivity">
...
</androidx.constraintlayout.motion.widget.MotionLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application ...>
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="..." />
...
</application>
Edit:
I finally found a way that works for me.
I'm just setting the window background color to the color that it already has. This is really fast and doesn't recreate the activity.
However I don't know why this works and why only this worked for me. I guess it updates the views in a similar way to the many other solutions from the other questions.
adView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
super.onAdLoaded();
getWindow().setBackgroundDrawableResource(R.color.background);
}
}

Images taking more time (4-5 min) to update in Android webview

I have converted my wordpress blog into app using Webview. But now i am facing serious issue of the time my android webview takes to update blog images on my app.
Whenever i change image in an existing blog, i have checked it on browser it takes nearly 2-3 seconds for updating the image. But my android webview shows me the old image for 4-5 minutes even after wordpress website is updated with new image.
I have tried clearing webview cache and also tried allowing dom storage, but it didn't helped me either. I have also used a refresh button to reload the page in webview, but still it shows me old image for 3-4 minutes.
So can anyone please help me to reduce this time issue that I am facing currently. I have added my code of the webview below
.Java file
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fantasy__football);
this.setTitle("Football");
F_foo = findViewById(R.id.fantasyf_Adview);
AdRequest f_adRequest15 = new AdRequest.Builder().build();
F_foo.loadAd(f_adRequest15);
F_web = (WebView)findViewById(R.id.fantasy_f_web);
WebSettings webSettings = F_web.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAppCacheEnabled(false);
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
webSettings.setDomStorageEnabled(true);
F_web.clearCache(true);
F_web.loadUrl("muyrl.com");
F_web.setWebViewClient(new WebViewClient());
F_web.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url){
super.onPageFinished(view,url);
view.clearCache(true);
}
});
Button ref = (Button)findViewById(R.id.ref_fb);
ref.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
F_web.clearCache(true);
F_web.reload();
}
});
.xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fantasy_Football">
<WebView
android:id="#+id/fantasy_f_web"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/fantasyf_Adview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-xxxx"
tools:layout_editor_absoluteX="369dp"
tools:layout_editor_absoluteY="446dp" />
<Button
android:id="#+id/ref_fb"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:text="Refresh"
/>
</RelativeLayout>
So can anyone please help me with this issue or suggest me what i can do to update my images faster ?

Attempting to Implement a Thread for a Game Loop

I am a novice learning Android development and the next step I must take is to be capable to understanding threads to be able to implement a game loop.
I have taken knowledge from google and StackOverflow and snippets from examples to come up with a lump of code that seems to not be working:
MainActivity.java
GameSetup.java
GameView.java
activity_main.xml
game_setup.xml
MainActivity.java is a simply title screen with a play button. This seems to work fine and will link to the next activity. However, I am getting a crash whenever I attempt to load up the next activity. My guestimations based on the error lead me to believe that threads have issues with RelativeActivity, but I'm not so sure on what the issue may be exactly, I am rather confused.
Here is the error I am getting thrown, I am having issues trying to decipher the issue with it.
Thanks.
R.id.layout is a RelativeLayout but you are trying to cast it to GameView, that's causing your crash.
Edit:
To add your GameView to the RelativeLayout you can do
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.your.package.GameView
android:id="#+id/my_game_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ship1" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ship1" />
</RelativeLayout>
and retrive it with
GameView myGameView = (GameView) findViewById(R.id.my_game_view);
Hmm, from the code you have written so far, I can explain the reason for the ClassCastException.
You are declaring a RelativeLayout in both XML files.
Both RelativeLayouts use the same android:id "#+id/layout"
In GameView.java you try to find a view by specifying the id
m_game = (GameView) findViewById(R.id.layout);
IMO you are retrieving one of the RelativeLayouts and then try to cast it to a GameView. As this is not possible, Java throws the exception.

findViewById Error cant find id in XML

I cant find out why my findViewById is not linking to the XML file and it is driving me nuts. The is is in the XML file but it is still not Finding it in the java MainActivity file.
here is my XML
<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=".MainActivity" >
<EditText
android:id="#+id/editCentimeters"
android:layout_width="wrap_content"
android:layout_height="27sp"
android:layout_alignLeft="#+id/editInches"
android:layout_alignTop="#+id/textCentimeters"
android:ems="5" >
<Button
android:id="#+id/buttonConvert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editInches"
android:layout_centerHorizontal="true"
android:layout_marginTop="98dp"
android:text="#string/convert" />
And here is my Java
EditText etCentimeters = (EditText) findViewById(R.id.editCentimeters);
Button buttonConvert = (Button) findViewById(R.id.buttonConvert);
I was facing the same issue. Just comment out the line which is throwing the error & BUILD the project once. Intellisense (ctrl + space) then will show you the IDs.
You need to take care of 2 things here:-
EditText etCentimeters = (EditText) findViewById(R.id.editCentimeters);
Button buttonConvert = (Button) findViewById(R.id.buttonConvert);
Firstly, the above code should be present in the onCreate() method.
And next, you need to place that code snippet below this line in the onCreate() method, not before it.(I'm assuming that the setContentView is already present, if not, add that)
setContentView(R.layout.main);
use setContentView(R.layout.main); at the onCreate of your Activity
You can refresh project.
And also check ur setContentView(R.layout.YOURLAYOUT_NAME) in Your activity.
Make sure that u are using ur code in activity.
Just clean & build in order to find the ID.
Clean & build didn't work for me. I had to restart Android Studio.

Categories