0

I am trying to add webview as well as reqarded ads in the app i have the webview and rewarded ad app works perfectly if used in seperate projects but when i put them together, the app stops working.

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.JavascriptInterface;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;


import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.OnUserEarnedRewardListener;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import com.google.android.gms.ads.rewarded.RewardItem;
import com.google.android.gms.ads.rewarded.RewardedAd;
import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback;


public class MainActivity extends AppCompatActivity {


    private RewardedAd mRewardedAd;
    private final String TAG = "MainActivity";
    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            if ("www.example.com".equals(request.getUrl().getHost())) {
                // This is my website, so do not override; let my WebView load the page
                return false;
            }
            // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
            Intent intent = new Intent(Intent.ACTION_VIEW, request.getUrl());
            startActivity(intent);
            return true;
        }
    }

    public class WebAppInterface {
        Context mContext;

        /** Instantiate the interface and set the context */
        WebAppInterface(Context c) {
            mContext = c;
        }

        /** Show a toast from the web page */
        @JavascriptInterface
        public void showToast(String toast) {
            Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView myWebView = (WebView) findViewById(R.id.webview);
        myWebView.loadUrl("MYURL");
        setContentView(myWebView);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
        myWebView.setWebViewClient(new MyWebViewClient());


        AdRequest adRequest = new AdRequest.Builder().build();

        RewardedAd.load(this, "ca-app-pub-MYID",
                adRequest, new RewardedAdLoadCallback() {
                    @Override
                    public void onAdFailedToLoad( LoadAdError loadAdError) {
                        // Handle the error.

                        mRewardedAd = null;
                    }

                    @Override
                    public void onAdLoaded(RewardedAd rewardedAd) {
                        mRewardedAd = rewardedAd;
                    }
                });

        if (mRewardedAd != null) {
            AppCompatActivity activityContext = MainActivity.this;
            mRewardedAd.show(activityContext, new OnUserEarnedRewardListener() {
                @Override
                public void onUserEarnedReward( RewardItem rewardItem) {

                    int rewardAmount = rewardItem.getAmount();
                    String rewardType = rewardItem.getType();
                }
            });
        } else {
        }

    }
}

I am trying to add webview as well as reqarded ads in the app i have the webview and rewarded ad app works perfectly if used in seperate projects but when i put them together, the app stops working.

i checked the logcat it gives the following error

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

help please

Dr Cooper
  • 1
  • 2
  • Post the full stack trace from logcat. You left out all the useful information. In particular, with "Unable to start activity" there's always multiple stack traces and the bottom most one is the real problem. – Gabe Sechan Apr 30 '22 at 07:37
  • Although your problem is likely related to calling setContentView twice, once on a view that's already part of the content view. Either you want to make a new WebView and not inflate anything from XML, or you want to inflate from xml and then not set the content view again. – Gabe Sechan Apr 30 '22 at 07:39
  • You're calling `setContentView` twice. Remove this one: `setContentView(myWebView);` – Ryan M Apr 30 '22 at 08:50
  • yes it worked. im trying to implement webview and rewarded video ads in the app but the ad wont show. i have checked the ad id its all right but still doesnt work – Dr Cooper May 01 '22 at 08:05

0 Answers0