Insert a code into Mainactivity

Asked

Viewed 182 times

0

I just started messing with Androidstudio and here’s the deal. I have this code in my mainactivity:

package com.tero.master;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

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

public class MainActivity extends AppCompatActivity {
    private AdView adView;
    private Button btnInterstitialAd;
    private InterstitialAd interstitialAd;
    Button clk1, clk2;
    MediaPlayer mdx;


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


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

        interstitialAd = new InterstitialAd(this);
        interstitialAd.setAdUnitId(getString(R.string.admob_interstitial_id));

        LoadInterstitial();

        interstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdClosed(){
                LoadInterstitial();
            }
        });




        btnInterstitialAd = (Button) findViewById(R.id.btnInterstitial);
        btnInterstitialAd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                interstitialAd.show();
            }
        });
    };



    @Override
    public void onPause(){
            if(adView !=null){
                adView.pause();
            }
            super.onPause();
    }

    @Override
    public void onResume(){
        super.onResume();
        if(adView !=null){
            adView.resume();
        }
    }

    @Override
    public void onDestroy() {
        if (adView != null) {
            adView.destroy();
        }
        super.onDestroy();
    }

   private void LoadInterstitial(){
        AdRequest interAdRequest = new AdRequest.Builder().build();
        interstitialAd.loadAd(interAdRequest);
   };
}

And I wanted to add this code without the top stop working, in case this one:

clk1 = (Button) findViewById(R.id.playid);
clk2 = (Button) findViewById(R.id.pauseid);
mdx = MediaPlayer.create(MainActivity.this, R.raw.callerf);

public void clkplay(View v) {
        mdx.start();
        }

public void clkpause(View v) {
        mdx.pause();
        }

1 answer

0


You must insert the first 3 lines inside onCreate and the rest outside onCreate as shown in the code:

public class MainActivity extends AppCompatActivity {
    private AdView adView;
    private Button btnInterstitialAd;
    private InterstitialAd interstitialAd;
    Button clk1, clk2;
    MediaPlayer mdx;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        adView = (AdView) findViewById(R.id.adbanner);
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);
        interstitialAd = new InterstitialAd(this);
        interstitialAd.setAdUnitId(getString(R.string.admob_interstitial_id));
        LoadInterstitial();
        interstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdClosed(){
                LoadInterstitial();
            }
        });
        btnInterstitialAd = (Button) findViewById(R.id.btnInterstitial);
        btnInterstitialAd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                interstitialAd.show();
            }
        });
    clk1 = (Button) findViewById(R.id.playid); clk2 = (Button) findViewById (R.id.pauseid); 
    mdx = MediaPlayer.create(MainActivity.this, R.raw.callerf);
    };
public void clkplay(View v) {
        mdx.start();
        }
public void clkpause(View v) {
        mdx.pause();
        }

@Override public void onPause(){ if(adView !=null){ adView.pause(); } super.onPause(); } @Override public void onResume(){ super.onResume(); if(adView !=null){ adView.resume(); } } @Override public void onDestroy() { if (adView != null) { adView.destroy(); } super.onDestroy(); }private void LoadInterstitial(){ AdRequest interAdRequest = new AdRequest.Builder().build(); interstitialAd.loadAd(interAdRequest); }; }

  • It worked! Thank you very much!

Browser other questions tagged

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