前提
Android StudioでGoogle AdMob Ads Activityプロジェクトを新規作成してビルドしてapkをインストールしました。
実現したいこと
とりあえずスケルトンアプリを起動したい。
発生している問題・エラーメッセージ
2022-09-19 08:38:38.239 10706-11020/com.example.xxxxxxx E/chromium: [ERROR:simple_file_enumerator.cc(21)] opendir /data/user/0/com.example.xxxxxxx/cache/WebView/Default/HTTP Cache/Code Cache/js: No such file or directory (2) 2022-09-19 08:38:38.240 10706-11020/com.example.xxxxxxx E/chromium: [ERROR:simple_index_file.cc(614)] Could not reconstruct index from disk
該当のソースコード
Kotlin
1package com.example.xxxxxxx 2 3import com.google.android.gms.ads.AdError 4import com.google.android.gms.ads.AdRequest 5import com.google.android.gms.ads.FullScreenContentCallback 6import com.google.android.gms.ads.LoadAdError 7import com.google.android.gms.ads.MobileAds 8import com.google.android.gms.ads.interstitial.InterstitialAd 9import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback 10import android.widget.Button 11import android.widget.TextView 12 13import android.os.Bundle 14import android.util.Log 15import androidx.appcompat.app.AppCompatActivity 16import android.view.Menu 17import android.view.MenuItem 18import android.widget.Toast 19import com.example.xxxxxxx.databinding.ActivityMainBinding 20 21import java.util.Locale 22 23// Remove the line below after defining your own ad unit ID. 24private const val TOAST_TEXT = "Test ads are being shown. " + 25 "To show live ads, replace the ad unit ID in res/values/strings.xml " + 26 "with your own ad unit ID." 27private const val START_LEVEL = 1 28 29class MainActivity : AppCompatActivity() { 30 31 private var currentLevel: Int = 0 32 private var interstitialAd: InterstitialAd? = null 33 private lateinit var nextLevelButton: Button 34 private lateinit var levelTextView: TextView 35 private val TAG = "MainActivity" 36 private lateinit var binding: ActivityMainBinding 37 38 override fun onCreate(savedInstanceState: Bundle?) { 39 super.onCreate(savedInstanceState) 40 41 binding = ActivityMainBinding.inflate(layoutInflater) 42 setContentView(binding.root) 43 44 MobileAds.initialize( 45 this 46 ) { } 47 // Load the InterstitialAd and set the adUnitId (defined in values/strings.xml). 48 loadInterstitialAd() 49 50 // Create the next level button, which tries to show an interstitial when clicked. 51 nextLevelButton = binding.nextLevelButton 52 nextLevelButton.isEnabled = false 53 nextLevelButton.setOnClickListener { showInterstitial() } 54 55 levelTextView = binding.level 56 // Create the text view to show the level number. 57 currentLevel = START_LEVEL 58 59 // Toasts the test ad message on the screen. Remove this after defining your own ad unit ID. 60 Toast.makeText(this, TOAST_TEXT, Toast.LENGTH_LONG).show() 61 } 62 63 override fun onCreateOptionsMenu(menu: Menu): Boolean { 64 // Inflate the menu; this adds items to the action bar if it is present. 65 menuInflater.inflate(R.menu.menu_main, menu) 66 return true 67 } 68 69 override fun onOptionsItemSelected(item: MenuItem) = 70 when (item.itemId) { 71 R.id.action_settings -> true 72 else -> super.onOptionsItemSelected(item) 73 } 74 75 private fun loadInterstitialAd() { 76 val adRequest = AdRequest.Builder().build() 77 InterstitialAd.load(this, getString(R.string.interstitial_ad_unit_id), adRequest, 78 object : InterstitialAdLoadCallback() { 79 override fun onAdLoaded(ad: InterstitialAd) { 80 // The interstitialAd reference will be null until 81 // an ad is loaded. 82 interstitialAd = ad 83 nextLevelButton.setEnabled(true) 84 Toast.makeText(this@MainActivity, "onAdLoaded()", Toast.LENGTH_SHORT) 85 .show() 86 ad.setFullScreenContentCallback( 87 object : FullScreenContentCallback() { 88 override fun onAdDismissedFullScreenContent() { 89 // Called when fullscreen content is dismissed. 90 // Make sure to set your reference to null so you don't 91 // show it a second time. 92 interstitialAd = null 93 Log.d(TAG, "The ad was dismissed.") 94 } 95 96 override fun onAdFailedToShowFullScreenContent(adError: AdError) { 97 // Called when fullscreen content failed to show. 98 // Make sure to set your reference to null so you don't 99 // show it a second time. 100 interstitialAd = null 101 Log.d(TAG, "The ad failed to show.") 102 } 103 104 override fun onAdShowedFullScreenContent() { 105 // Called when fullscreen content is shown. 106 Log.d(TAG, "The ad was shown.") 107 } 108 }) 109 } 110 111 override fun onAdFailedToLoad(loadAdError: LoadAdError) { 112 // Handle the error 113 Log.i(TAG, loadAdError.message) 114 interstitialAd = null 115 nextLevelButton.setEnabled(true) 116 val error: String = String.format( 117 Locale.ENGLISH, 118 "domain: %s, code: %d, message: %s", 119 loadAdError.domain, 120 loadAdError.code, 121 loadAdError.message 122 ) 123 Toast.makeText( 124 this@MainActivity, 125 "onAdFailedToLoad() with error: $error", Toast.LENGTH_SHORT 126 ) 127 .show() 128 } 129 }) 130 } 131 132 private fun showInterstitial() { 133 // Show the ad if it"s ready. Otherwise toast and reload the ad. 134 if (interstitialAd != null) { 135 interstitialAd!!.show(this) 136 } else { 137 Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show() 138 goToNextLevel() 139 } 140 } 141 142 private fun goToNextLevel() { 143 // Show the next level and reload the ad to prepare for the level after. 144 levelTextView.text = "Level " + (++currentLevel) 145 if (interstitialAd == null) { 146 loadInterstitialAd() 147 } 148 } 149}
試したこと
Android StudioでGoogle AdMob Ads Activityプロジェクトを新規作成してそのままビルドしてapkをインストールして実行してlogcatでエラーメッセージを拾いました。
回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。