我正在借助this教程制作一个简单的应用,但无法添加广告。
没有它们,游戏运行正常,但出现了Chromium WebView不存在错误和广告出现UnsupportedOperationException错误(我不确定一个错误是否引起另一个错误)。
这是我的SampleGame类(在应用程序启动时调用的类)
package com.harryhochwarter.frugalfarminggame;
import android.os.Bundle;
import com.google.android.gms.ads.*;
import com.kilobolt.framework.Screen;
import com.kilobolt.framework.implementation.AndroidGame;
public class SampleGame extends AndroidGame
{
public static String map;
boolean firstTimeCreate = true;
//
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AdView adView = (AdView)findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("AC98C820A50B4AD8A2106EDE96FB87D4")
.build();
adView.loadAd(adRequest);
}
@Override
public Screen getInitScreen()
{
if(firstTimeCreate)
{
Assets.load(this);
firstTimeCreate = false;
}
return new SplashLoadingScreen(this);
}
@Override
public void onBackPressed()
{
getCurrentScreen().backButton();
}
@Override
public void onResume()
{
super.onResume();
//Assets.theme.play();
}
@Override
public void onPause()
{
super.onPause();
//Assets.theme.pause();
}
}
这是我修改的SampleGame的父类
package com.kilobolt.framework.implementation;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.view.Window;
import android.view.WindowManager;
import com.harryhochwarter.frugalfarminggame.R;
import com.kilobolt.framework.Audio;
import com.kilobolt.framework.FileIO;
import com.kilobolt.framework.Game;
import com.kilobolt.framework.Graphics;
import com.kilobolt.framework.Input;
import com.kilobolt.framework.Screen;
public abstract class AndroidGame extends Activity implements Game {
AndroidFastRenderView renderView;
Graphics graphics;
Audio audio;
Input input;
FileIO fileIO;
Screen screen;
WakeLock wakeLock;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
int frameBufferWidth = isPortrait ? 480: 800;
int frameBufferHeight = isPortrait ? 800: 480;
Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth,
frameBufferHeight, Config.RGB_565);
float scaleX = (float) frameBufferWidth
/ getWindowManager().getDefaultDisplay().getWidth();
float scaleY = (float) frameBufferHeight
/ getWindowManager().getDefaultDisplay().getHeight();
renderView = new AndroidFastRenderView(this, frameBuffer);
graphics = new AndroidGraphics(getAssets(), frameBuffer);
fileIO = new AndroidFileIO(this);
audio = new AndroidAudio(this);
input = new AndroidInput(this, renderView, scaleX, scaleY);
screen = getInitScreen();
setContentView(renderView);
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "MyGame");
}
@Override
public void onResume() {
super.onResume();
wakeLock.acquire();
screen.resume();
renderView.resume();
}
@Override
public void onPause() {
super.onPause();
wakeLock.release();
renderView.pause();
screen.pause();
if (isFinishing())
screen.dispose();
}
@Override
public Input getInput() {
return input;
}
@Override
public FileIO getFileIO() {
return fileIO;
}
@Override
public Graphics getGraphics() {
return graphics;
}
@Override
public Audio getAudio() {
return audio;
}
@Override
public void setScreen(Screen screen) {
if (screen == null)
throw new IllegalArgumentException("Screen must not be null");
this.screen.pause();
this.screen.dispose();
screen.resume();
screen.update(0);
this.screen = screen;
}
public Screen getCurrentScreen() {
return screen;
}
}
这是我的main.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.ads.AdView android:id="@+id/adView"
android:layout_width="480dp"
android:layout_height="75dp"
android:gravity="bottom"
ads:adUnitId="ca-app-pub-9472185097153282/1248179452"
ads:adSize="BANNER"/>
</LinearLayout>
最后,这是我的logcat输出
08-24 05:38:37.130: D/dalvikvm(897): GC_FOR_ALLOC freed 42K, 8% free 2162K/2340K, paused 149ms, total 150ms
08-24 05:38:37.130: I/dalvikvm-heap(897): Grow heap (frag case) to 2.972MB for 768016-byte allocation
08-24 05:38:37.260: D/dalvikvm(897): GC_FOR_ALLOC freed <1K, 6% free 2912K/3092K, paused 124ms, total 124ms
08-24 05:38:38.080: W/GooglePlayServicesUtil(897): Google Play services out of date. Requires 5089000 but found 5077534
08-24 05:38:38.220: I/Ads(897): Starting ad request.
08-24 05:38:38.280: I/Ads(897): Please set theme of AdActivity to @android:style/Theme.Translucent to enable transparent background interstitial ad.
08-24 05:38:38.360: E/WebViewFactory(897): Chromium WebView does not exist
08-24 05:38:38.370: E/WebViewFactory(897): Chromium WebView does not exist
08-24 05:38:38.370: D/AndroidRuntime(897): Shutting down VM
08-24 05:38:38.370: W/dalvikvm(897): threadid=1: thread exiting with uncaught exception (group=0xb1a60d70)
08-24 05:38:38.660: E/AndroidRuntime(897): FATAL EXCEPTION: main
08-24 05:38:38.660: E/AndroidRuntime(897): Process: com.harryhochwarter.frugalfarminggame, PID: 897
08-24 05:38:38.660: E/AndroidRuntime(897): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.harryhochwarter.frugalfarminggame/com.harryhochwarter.frugalfarminggame.SampleGame}: java.lang.UnsupportedOperationException
08-24 05:38:38.660: E/AndroidRuntime(897): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2197)
08-24 05:38:38.660: E/AndroidRuntime(897): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2258)
08-24 05:38:38.660: E/AndroidRuntime(897): at android.app.ActivityThread.access$800(ActivityThread.java:138)
08-24 05:38:38.660: E/AndroidRuntime(897): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
08-24 05:38:38.660: E/AndroidRuntime(897): at android.os.Handler.dispatchMessage(Handler.java:102)
08-24 05:38:38.660: E/AndroidRuntime(897): at android.os.Looper.loop(Looper.java:136)
08-24 05:38:38.660: E/AndroidRuntime(897): at android.app.ActivityThread.main(ActivityThread.java:5026)
08-24 05:38:38.660: E/AndroidRuntime(897): at java.lang.reflect.Method.invokeNative(Native Method)
08-24 05:38:38.660: E/AndroidRuntime(897): at java.lang.reflect.Method.invoke(Method.java:515)
08-24 05:38:38.660: E/AndroidRuntime(897): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
08-24 05:38:38.660: E/AndroidRuntime(897): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
08-24 05:38:38.660: E/AndroidRuntime(897): at dalvik.system.NativeStart.main(Native Method)
08-24 05:38:38.660: E/AndroidRuntime(897): Caused by: java.lang.UnsupportedOperationException
08-24 05:38:38.660: E/AndroidRuntime(897): at com.android.webview.nullwebview.NullWebViewFactoryProvider.createWebView(NullWebViewFactoryProvider.java:41)
08-24 05:38:38.660: E/AndroidRuntime(897): at android.webkit.WebView.ensureProviderCreated(WebView.java:2058)
08-24 05:38:38.660: E/AndroidRuntime(897): at android.webkit.WebView.setOverScrollMode(WebView.java:2116)
08-24 05:38:38.660: E/AndroidRuntime(897): at android.view.View.<init>(View.java:3461)
08-24 05:38:38.660: E/AndroidRuntime(897): at android.view.View.<init>(View.java:3517)
08-24 05:38:38.660: E/AndroidRuntime(897): at android.view.ViewGroup.<init>(ViewGroup.java:470)
08-24 05:38:38.660: E/AndroidRuntime(897): at android.widget.AbsoluteLayout.<init>(AbsoluteLayout.java:52)
08-24 05:38:38.660: E/AndroidRuntime(897): at android.webkit.WebView.<init>(WebView.java:498)
08-24 05:38:38.660: E/AndroidRuntime(897): at android.webkit.WebView.<init>(WebView.java:475)
08-24 05:38:38.660: E/AndroidRuntime(897): at android.webkit.WebView.<init>(WebView.java:455)
08-24 05:38:38.660: E/AndroidRuntime(897): at android.webkit.WebView.<init>(WebView.java:444)
08-24 05:38:38.660: E/AndroidRuntime(897): at android.webkit.WebView.<init>(WebView.java:434)
08-24 05:38:38.660: E/AndroidRuntime(897): at com.google.android.gms.internal.ex.<init>(Unknown Source)
08-24 05:38:38.660: E/AndroidRuntime(897): at com.google.android.gms.internal.ex.a(Unknown Source)
08-24 05:38:38.660: E/AndroidRuntime(897): at com.google.android.gms.internal.u.a(Unknown Source)
08-24 05:38:38.660: E/AndroidRuntime(897): at com.google.android.gms.internal.au.a(Unknown Source)
08-24 05:38:38.660: E/AndroidRuntime(897): at com.google.android.gms.ads.AdView.loadAd(Unknown Source)
08-24 05:38:38.660: E/AndroidRuntime(897): at com.harryhochwarter.frugalfarminggame.SampleGame.onCreate(SampleGame.java:27)
08-24 05:38:38.660: E/AndroidRuntime(897): at android.app.Activity.performCreate(Activity.java:5242)
08-24 05:38:38.660: E/AndroidRuntime(897): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-24 05:38:38.660: E/AndroidRuntime(897): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
08-24 05:38:38.660: E/AndroidRuntime(897): ... 11 more
08-24 05:38:40.360: W/dalvikvm(897): threadid=5: spin on suspend #1 threadid=13 (pcf=0)
08-24 05:38:40.360: D/dalvikvm(897): Temporarily moving tid 914 to fg (was 0)
08-24 05:38:40.360: D/dalvikvm(897): Temporarily raised priority on tid 914 (10 -> 0)
08-24 05:38:40.450: W/dalvikvm(897): threadid=5: spin on suspend resolved in 1148 msec
08-24 05:38:40.450: D/dalvikvm(897): Restored policy of 914 to 0
08-24 05:38:40.450: D/dalvikvm(897): Restored priority on 914 to 10
08-24 05:38:45.900: D/dalvikvm(897): GC_FOR_ALLOC freed 272K, 12% free 3154K/3560K, paused 113ms, total 1199ms
08-24 05:38:45.950: D/dalvikvm(897): DexOpt: --- BEGIN 'ads2143539119.jar' (bootstrap=0) ---
08-24 05:38:49.050: D/dalvikvm(897): DexOpt: --- END 'ads2143539119.jar' (success) ---
08-24 05:38:49.070: D/dalvikvm(897): DEX prep '/data/data/com.harryhochwarter.frugalfarminggame/cache/ads2143539119.jar': unzip in 1ms, rewrite 3112ms
如果需要添加其他代码或信息,请告诉我!
最佳答案
您在这里遇到几个问题:
日志显示您的Google Play服务版本已过时
如您的日志所述,您需要“将AdActivity的主题设置为@android:style / Theme.Translucent以启用透明的背景非页内广告。”
您正在两次调用Activity的#setContentView。一次在活动中,一次在父活动中。这会引起你的悲伤。
关于java - Chromium WebView不存在(UnsupportedOperationException),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25468903/