Error while using Adrequest

Asked

Viewed 270 times

1

Could help me find the error in this Oncreate method specifically in the Adrequest class?


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);


    viewPager = (ViewPager) findViewById(R.id.view_pager);
    viewPager.setAdapter(new CustomPageAdapter(getBaseContext()));

    mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);
}

Some of the components are apparently coming into conflict with adRequest because I tested the :

mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);

in another application to test if it was this code , and it worked, so the problem can only be in Oncreate

Below is the logcat error

FATAL EXCEPTION: main Process: br.com.paivasdev.fazendocafe, PID: 5792 java.lang.Runtimeexception: Unable to start Activity Componentinfo{br.com.paivasdev.fazendocafe/br.com.paivasdev.fazendocafe.Mainactivity}: 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 at android.app.Activitythread.performLaunchActivity(Activitythread.java:3319) at android.app.Activitythread.handleLaunchActivity(Activitythread.java:3415) at android.app.Activitythread.access$1100(Activitythread.java:229) at android.app.Activitythread$H.handleMessage(Activitythread.java:1821) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.Activitythread.main(Activitythread.java:7325) at java.lang.reflect.Method.invoke(Native Method) at com.android.Internal.os.Zygoteinit$Methodandargscaller.run(Zygoteinit.java:1230) at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:1120) 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 at br.com.paivasdev.fazendocafe.Mainactivity.onCreate(Mainactivity.java:50) at android.app.Activity.performCreate(Activity.java:6904) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136) at android.app.Activitythread.performLaunchActivity(Activitythread.java:3266) at android.app.Activitythread.handleLaunchActivity(Activitythread.java:3415)  at android.app.Activitythread.access$1100(Activitythread.java:229)  at android.app.Activitythread$H.handleMessage(Activitythread.java:1821)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.Activitythread.main(Activitythread.java:7325)  at java.lang.reflect.Method.invoke(Native Method)


  • Did you receive an error message? If yes, add your question

  • If you leave only the 1 and 2 line works?

  • R.id.adView exists in the layout activity_main?

  • Yes, there’s a strange thing about making that mistake

  • The error indicates that mAdView is null, I can find no other reason for this to happen than that it does not exist in the layout activity_main

  • Tried to rename "adView" to any other name in XML? Because it may be conflicting with other Ids.

  • And you initiated ADMOB?

Show 2 more comments

1 answer

2


After the creation of an account at Admob, see below a brief and functional form to create banners news in your application and make a comparison with your code.

build.Gradle:

Check first the latest available version of Firebase to import the libs into your project.

dependencies {
     compile 'com.google.firebase:firebase-ads:9.6.1'
}

main.xml

It is important not to forget to use the xmlns:ads="http://schemas.android.com/apk/res-auto" in XML. See:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent" 
    tools:context=".MainActivity">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <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>

Mainactivity.java

package ...

import ...
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

public class MainActivity extends ActionBarActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AdView mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
    }
}

Androidmanifest.xml

Finally give permission for your application to access the internet. If you do not have permission, the banners will not appear.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  • Acklay , I put as response to the posting the logcat error

  • Paiva check your Miles if they’re the same as mine. import com.google.android.gms.ads.AdRequest;&#xA;import com.google.android.gms.ads.AdView;

  • If I put in the comment , will exceed the number of characters ...

  • @Paiva is not in the commentary, it is EDIT the question and put the error in it.

  • The Imports are equal yes , I edited the post and put the error

  • Thanks man! I was wrong to import the libs.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.