Home Activity unable to start due to implementation of Admob ads
Reply: 0

Activity unable to start due to implementation of Admob ads

user5064
1#
user5064 Published in April 22, 2018, 6:50 am

I've been trying to make a messaging app with ads when the app crashes upon implementing AdMob. Not sure why the app crashes but I suspect that there may be clashes with the other existing codes. Is there anything wrong with the code below?

Java:

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

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
    // Sample AdMob app ID: ca-app-pub-5289059028582551~3029249239
       MobileAds.initialize(this, "ca-app-pub-5289059028582551~3029249239");
       AdView adView = (AdView)findViewById(R.id.adView);
       AdRequest adRequest = new AdRequest.Builder().build();
       adView.loadAd(adRequest);
       editMessage = findViewById(R.id.editMessageE);
       mDatabase = FirebaseDatabase.getInstance().getReference().child("Messages");
       mMessageList = findViewById(R.id.messageRec);
       mMessageList.setHasFixedSize(true);
       LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
       linearLayoutManager.setStackFromEnd(true);
       mMessageList.setLayoutManager(linearLayoutManager);
       mAuth = FirebaseAuth.getInstance();
       mAuthListener = new FirebaseAuth.AuthStateListener() {
           @Override
           public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
               if (firebaseAuth.getCurrentUser() == null){
                startActivity(new Intent(MainActivity.this,RegisterActivity.class));
          }
        }
    };
}

Log cat:

FATAL EXCEPTION: main Process: comdanishansogning.wixsite.httpshobojuniors.danishansgning, PID: 3879 java.lang.RuntimeException: Unable to start activity ComponentInfo{comdanishansogning.wixsite.httpshobojuniors.danishansgning/comdanishansogning.wixsite.httpshobojuniors.danishansgning.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

Manifest:

<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"
            android:theme="@android:style/Theme.Translucent" />

Xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingStart="16dp"
android:paddingRight="16dp"
android:paddingEnd="16dp"
android:paddingTop="16dp"
android:id="@+id/activity_register"
tools:context="comdanishansogning.wixsite.httpshobojuniors.danishansgning.RegisterActivity"
>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Sign In:"
    />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:id="@+id/editEmail"
    android:hint="Enter email"
    android:inputType="textEmailAddress"
    />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:id="@+id/editUsername"
    android:hint="Enter username"
    />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editPassword"
    android:hint="Enter password"
    android:inputType="textPassword"
    />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/signupButtonClicked"
    android:text="Sign up"
    android:onClick="signupButtonClicked"
    />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Already have an Account? Login"
    android:onClick="loginButtonClicked"
    />

<Space
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    />

<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"
    ads:adSize="SMART_BANNER"
    ads:adUnitId="ca-app-pub-5289059028582551/8546061599"
    >
</com.google.android.gms.ads.AdView>

</LinearLayout>
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.305042 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO