Issue
Being my first project ever and wanting to implement banner ads in my app, I don't know how to get rid of this problem: 'onCreate(Bundle)' is already defined in activity
in MainActity.java .
Here's the problematic part:
public class MainActivity extends AppCompatActivity {
private AdView mAdView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
private WebView webView;
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomWebViewClient client = new CustomWebViewClient(this);
webView = findViewById(R.id.webView);
webView.setWebViewClient(client);
webView.getSettings().setJavaScriptEnabled(true);
webView.clearCache(true);
webView.loadUrl("https://example.com/");
}
Solution
You are declaring the same method twice. Just add the content of the second method in the first method and delete the second. Like so:
public class MainActivity extends AppCompatActivity {
private AdView mAdView;
private WebView webView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
CustomWebViewClient client = new CustomWebViewClient(this);
webView = findViewById(R.id.webView);
webView.setWebViewClient(client);
webView.getSettings().setJavaScriptEnabled(true);
webView.clearCache(true);
webView.loadUrl("https://example.com/");
}
}
Answered By - Mike087
Answer Checked By - Marie Seifert (JavaFixing Admin)