Insert a code in MainActivity

0

I recently started messing with AndroidStudio and the deal is as follows. 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 above stop working, in this case here:

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();
        }
    
asked by anonymous 12.03.2018 / 15:01

1 answer

0

You should insert the first 3 lines within onCreate and the rest outside ofCreate 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);  };  }

    
12.03.2018 / 15:49