Search code examples
androidads

App does not start after implementing ads


My Android app does not start after I had implemented ads. How can I repair that? My Android Studio version is 3.5.

Main Activity Layout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <WebView
        android:id="@+id/webView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal"
        android:layout_weight="1"
        app:adSize="BANNER"
        app:adUnitId="ca-app-pub-3940256099942544/6300978111"
        app:layout_constraintStart_toStartOf="@+id/webView"
        app:layout_constraintTop_toTopOf="@+id/webView"></com.google.android.gms.ads.AdView>


</androidx.constraintlayout.widget.ConstraintLayout>

Main Activity Code

package com.pip.pudzibreakgame;

import android.content.DialogInterface;
import android.os.Bundle;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;


public class MainActivity extends AppCompatActivity {

    private AdView mAdView;

    @Override
    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.ad_view);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);


        WebView WebView= (WebView) findViewById(R.id.webView);
        WebView.getSettings().setJavaScriptEnabled(true);

        WebView.loadUrl("file:///android_asset/myHtml.html");


        WebView.setWebChromeClient(new WebChromeClient(){
            @Override
            public boolean onJsAlert(WebView view, String url, String message, final JsResult result){
                AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
                builder.setTitle("PudziBreak");
                builder.setMessage(message);
                final AlertDialog.Builder ok = builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        result.confirm();
                    }
                });
                AlertDialog dialog = builder
                        .create();
                dialog.setCanceledOnTouchOutside(false);
                dialog.setCancelable(false);
                        dialog.show();
                return true;
            }

            @Override
            public boolean onJsConfirm(WebView view, String url, String message, final JsResult result){
                AlertDialog dialog = new AlertDialog.Builder(view.getContext())
                        .setTitle("PudziBreak")
                        .setMessage(message)
                        .setPositiveButton("OK", new DialogInterface.OnClickListener(){
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i){
                                result.confirm();
                            }
                        })
                        .setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i){
                                result.cancel();
                            }
                        })
                        .create();
                dialog.setCanceledOnTouchOutside(false);
                dialog.setCancelable(false);
                dialog.show();
                return true;

            }
        });


    }
}

Android Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pip.pudzibreakgame">


<uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="PudziBreak"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">


        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-ca-app-pub-9411864465298475~5838053417"/>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.pudzibreak.pudzibreakgame"
        minSdkVersion 19
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'


    implementation 'com.google.android.gms:play-services-ads:18.2.0'

}

UpdatE

2019-09-01 15:39:08.016 985-1019/? E/zygote64: Error encountered killing process cgroup uid 99133 pid 20461: No such file or directory
2019-09-01 15:39:09.692 985-2370/? E/AwareLog: CPUFeatureAMSCommunicator: set app boost but type is unknown
2019-09-01 15:39:09.920 20786-20786/? E/Minikin: Could not get cmap table size!
2019-09-01 15:39:10.096 20786-20786/? E/WebViewFactory: No such method for getDefinedWebViewPackageName: java.lang.NoSuchMethodException: getDefinedWebViewPackageName []
2019-09-01 15:39:10.415 20820-20820/? E//system/bin/webview_zygote32: Failed to make and chown /acct/uid_99134: Permission denied
2019-09-01 15:39:10.416 20820-20820/? E/Zygote: createProcessGroup(99134, 0) failed: Permission denied
2019-09-01 15:39:10.595 20820-20842/? E/libc: Access denied finding property "ro.sf.lcd_density"
2019-09-01 15:39:10.598 20820-20842/? E/libc: Access denied finding property "ro.sf.lcd_density"
2019-09-01 15:39:10.715 20820-20820/? E/EasyInvokeUtils: no target fieldName
2019-09-01 15:39:10.719 20820-20820/? E/EasyInvokeUtils: no target fieldName
2019-09-01 15:39:11.046 20786-20786/com.pudzibreak.pudzibreakgame E/Ads: Google Mobile Ads SDK initialization functionality unavailable for this session. Ad requests can be made at any time.
2019-09-01 15:39:11.084 20820-20820/? E/Minikin: Could not get cmap table size!
2019-09-01 15:39:11.251 20786-20853/com.pudzibreak.pudzibreakgame E/IPCThreadState: More than IPC_START_MONITOR_LEVEL, start Monitor
2019-09-01 15:39:11.252 20786-20864/com.pudzibreak.pudzibreakgame E/IPCThreadState: More than IPC_START_MONITOR_LEVEL, start Monitor
2019-09-01 15:39:11.254 20786-20864/com.pudzibreak.pudzibreakgame E/IPCThreadState: More than IPC_START_MONITOR_LEVEL, start Monitor
2019-09-01 15:39:11.256 20786-20864/com.pudzibreak.pudzibreakgame E/IPCThreadState: More than IPC_START_MONITOR_LEVEL, start Monitor
2019-09-01 15:39:11.261 20786-20864/com.pudzibreak.pudzibreakgame E/IPCThreadState: More than IPC_START_MONITOR_LEVEL, start Monitor
2019-09-01 15:39:11.264 20786-20864/com.pudzibreak.pudzibreakgame E/IPCThreadState: More than IPC_START_MONITOR_LEVEL, start Monitor
2019-09-01 15:39:11.265 20786-20864/com.pudzibreak.pudzibreakgame E/IPCThreadState: More than IPC_START_MONITOR_LEVEL, start Monitor
2019-09-01 15:39:11.269 20786-20864/com.pudzibreak.pudzibreakgame E/IPCThreadState: More than IPC_START_MONITOR_LEVEL, start Monitor
2019-09-01 15:39:11.270 20786-20873/com.pudzibreak.pudzibreakgame E/IPCThreadState: More than IPC_START_MONITOR_LEVEL, start Monitor
2019-09-01 15:39:11.272 20786-20864/com.pudzibreak.pudzibreakgame E/IPCThreadState: More than IPC_START_MONITOR_LEVEL, start Monitor
2019-09-01 15:39:11.274 20786-20864/com.pudzibreak.pudzibreakgame E/IPCThreadState: More than IPC_START_MONITOR_LEVEL, start Monitor
2019-09-01 15:39:11.276 20786-20864/com.pudzibreak.pudzibreakgame E/IPCThreadState: More than IPC_START_MONITOR_LEVEL, start Monitor
2019-09-01 15:39:11.301 20786-20864/com.pudzibreak.pudzibreakgame E/IPCThreadState: More than IPC_START_MONITOR_LEVEL, start Monitor
2019-09-01 15:39:12.869 985-1092/? E/AudioAttributes: Illegal value unmarshalling AudioAttributes, can't initialize bundle
2019-09-01 15:39:12.869 985-1092/? E/AudioService.PlaybackActivityMonitor: Error updating audio attributes
2019-09-01 15:39:12.947 486-486/? E/Thermal-daemon: [ap] temp_new :34  temp_old :33
2019-09-01 15:39:12.948 486-486/? E/Thermal-daemon: Report temperature: [ap] temp :34  report_threshold:1
2019-09-01 15:39:31.660 2289-2289/? E/SarControl_Manager: mReceiver.onReceive, enter. action=android.intent.action.BATTERY_CHANGED
2019-09-01 15:39:37.363 985-1023/? E/DeviceStorageMonitorService: Will not process invalid message
2019-09-01 15:39:42.904 985-4813/? E/AudioAttributes: Illegal value unmarshalling AudioAttributes, can't initialize bundle
2019-09-01 15:39:42.905 985-4813/? E/AudioService.PlaybackActivityMonitor: Error updating audio attributes
2019-09-01 15:40:07.955 486-486/? E/Thermal-daemon: [ap] temp_new :33  temp_old :34
2019-09-01 15:40:07.958 486-486/? E/Thermal-daemon: Report temperature: [ap] temp :33  report_threshold:1

Solution

  • In your XML file you are using id for the ad:

    android:id="@+id/adView"
    

    and in MainActivity.java you are using wrong id to get the adView:

    mAdView = findViewById(R.id.ad_view);
    

    so change it into:

    mAdView = findViewById(R.id.adView);