-1

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>
Joshua Lee
  • 71
  • 7
  • Can you post `activity_main.xml`? – Michael Dodd Apr 17 '18 at 10:42
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Michael Dodd Apr 17 '18 at 10:42
  • here is the activity_main.xml https://pastebin.com/isfYfrsr im using another activity instead of activity_main would that somehow pose a problem? – Joshua Lee Apr 17 '18 at 10:44
  • Could you please add it [as an edit to your question?](https://stackoverflow.com/posts/49875850/edit) If Pastebin goes down then this question won't be useful to others in the future. – Michael Dodd Apr 17 '18 at 10:45
  • also im not very sure of the NullPointerException but the problem probably lies in the implementation of the AdMob... with the new updates to it, im not sure if i correctly written the code – Joshua Lee Apr 17 '18 at 10:46
  • If you're trying to use using a different layout file instead of `activity_main` then yes that would be a problem. What's the name of the layout file you're trying to use? – Michael Dodd Apr 17 '18 at 10:47
  • the name is activity_register.xml do I need to use this instead in the Java class? – Joshua Lee Apr 17 '18 at 10:55
  • Yes. See my answer below. – Michael Dodd Apr 17 '18 at 10:57

2 Answers2

1

As stated in the comments, you are inflating the wrong XML layout file. In your code:

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

You are inflating the file res/layout/activity_main.xml. To inflate res/layout/activity_register.xml instead, change setContentView to this:

setContentView(R.layout.activity_register);

For your second issue, you do not have a RecyclerView defined within your layout. Add one e.g.

<android.support.v7.widget.RecyclerView
    android:id="@+id/messageRec"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
  • I tried using your suggested edit in inflating the correct XML layout file, however the app still crashes with an addition of Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference – Joshua Lee Apr 17 '18 at 11:07
  • @JoshuaLee That's because you've not defined a `RecyclerView` with ID `@+id/messageRec` in the XML layout file you've posted above. Add it in there. – Michael Dodd Apr 17 '18 at 11:09
  • sorry I did not quite understand what you meant, could you edit it in your answer? and how does defining the recycler view have to do with it? – Joshua Lee Apr 17 '18 at 11:16
  • I've updated my answer with an example `RecyclerView`. If you do have any further issues though I'd recommend asking a new question, so that this question does not deviate too far from its original purpose. – Michael Dodd Apr 17 '18 at 11:20
  • after adding the recycler view, the app no longer crashes but the ad is not displayed. Is this due to another problem? – Joshua Lee Apr 17 '18 at 11:51
  • @JoshuaLee Possibly, though I don't know enough about AdMob to advise on that though. – Michael Dodd Apr 17 '18 at 12:30
0
Please Check weather you have define activity in your manifest file or not.
<application>
<activity android:name=".MainActivity" android:screenOrientation="portrait">
<intent-filter> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application>

else check this:-> https://developers.google.com/admob/android/quick-start

  • The activity is likely already defined in the manifest as the activity process makes it past `super.onCreate()`. It's a `NullPointerException` when referencing `adView`, so `R.id.adView` is not present in `R.layout.activity_main`. Need to wait for OP to clarify the details. – Michael Dodd Apr 17 '18 at 10:53
  • Yes I did define the activity, which I did not include. And I did check the quick start guide, although the result is still the same... – Joshua Lee Apr 17 '18 at 10:54
  • major issue is your project could not get your activity context. – Hitesh Manwani Apr 17 '18 at 11:06
  • @HiteshManwani The issue was that OP was inflating the wrong layout. See my answer above. – Michael Dodd Apr 17 '18 at 11:21