我不太熟悉android的活动,也不知道它们是如何工作的;任何关于这个主题的指导都会令人惊奇。我在将广告sdk与我的游戏集成时遇到问题。我将介绍几个类,以便充分理解我的代码。从本质上说,我试图确定一个好的方式来集成Startapp的sdk(一个广告sdk)与我的libgdx游戏。我得到了一个似乎是由startappad.show();和startappad.load()调用引起的循环错误。
下面是堆栈跟踪(提到一个ui线程,并将广告加载到另一个线程上):

01-05 15:55:27.646: W/webview_proxy(14876): java.lang.Throwable: Warning: A WebView method was called on thread 'GLThread 8584'. All WebView methods must be called on the UI thread. Future versions of WebView may not support use on other threads.
01-05 15:55:27.646: W/webview_proxy(14876):     at android.webkit.WebView.checkThread(WebView.java:1895)
01-05 15:55:27.646: W/webview_proxy(14876):     at android.webkit.WebView.ensureProviderCreated(WebView.java:1878)
01-05 15:55:27.646: W/webview_proxy(14876):     at android.webkit.WebView.setOverScrollMode(WebView.java:1935)
01-05 15:55:27.646: W/webview_proxy(14876):     at android.view.View.<init>(View.java:3316)
01-05 15:55:27.646: W/webview_proxy(14876):     at android.view.View.<init>(View.java:3383)
01-05 15:55:27.646: W/webview_proxy(14876):     at android.view.ViewGroup.<init>(ViewGroup.java:432)
01-05 15:55:27.646: W/webview_proxy(14876):     at android.widget.AbsoluteLayout.<init>(AbsoluteLayout.java:52)
01-05 15:55:27.646: W/webview_proxy(14876):     at android.webkit.WebView.<init>(WebView.java:505)
01-05 15:55:27.646: W/webview_proxy(14876):     at android.webkit.WebView.<init>(WebView.java:482)
01-05 15:55:27.646: W/webview_proxy(14876):     at android.webkit.WebView.<init>(WebView.java:468)
01-05 15:55:27.646: W/webview_proxy(14876):     at android.webkit.WebView.<init>(WebView.java:457)
01-05 15:55:27.646: W/webview_proxy(14876):     at android.webkit.WebView.<init>(WebView.java:447)
01-05 15:55:27.646: W/webview_proxy(14876):     at com.startapp.android.publish.Ad.load(Unknown Source)
01-05 15:55:27.646: W/webview_proxy(14876):     at com.startapp.android.publish.HtmlAd.load(Unknown Source)
01-05 15:55:27.646: W/webview_proxy(14876):     at com.startapp.android.publish.StartAppAd.loadAds(Unknown Source)
01-05 15:55:27.646: W/webview_proxy(14876):     at com.startapp.android.publish.StartAppAd.load(Unknown Source)
01-05 15:55:27.646: W/webview_proxy(14876):     at com.startapp.android.publish.StartAppAd.loadAd(Unknown Source)
01-05 15:55:27.646: W/webview_proxy(14876):     at com.startapp.android.publish.StartAppAd.loadAd(Unknown Source)
01-05 15:55:27.646: W/webview_proxy(14876):     at com.startapp.android.publish.StartAppAd.onResume(Unknown Source)
01-05 15:55:27.646: W/webview_proxy(14876):     at com.audiack.freetappgame.MainActivity.resume(MainActivity.java:41)
01-05 15:55:27.646: W/webview_proxy(14876):     at Screens.GameScreen.resume(GameScreen.java:699)
01-05 15:55:27.646: W/webview_proxy(14876):     at com.badlogic.gdx.Game.resume(Game.java:41)
01-05 15:55:27.646: W/webview_proxy(14876):     at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:480)
01-05 15:55:27.646: W/webview_proxy(14876):     at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1516)
01-05 15:55:27.646: W/webview_proxy(14876):     at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
01-05 15:55:27.656: W/IInputConnectionWrapper(14876): getSelectedText on inactive InputConnection
01-05 15:55:27.656: D/SensorManager(14876): onAccuracyChanged :: accuracy = 3
01-05 15:55:27.656: W/IInputConnectionWrapper(14876): setComposingText on inactive InputConnection
01-05 15:55:27.676: W/webview_proxy(14876): java.lang.Throwable: Warning: A WebView method was called on thread 'GLThread 8584'. All WebView methods must be called on the UI thread. Future versions of WebView may not support use on other threads.
01-05 15:55:27.676: W/webview_proxy(14876):     at android.webkit.WebView.checkThread(WebView.java:1895)
01-05 15:55:27.676: W/webview_proxy(14876):     at android.webkit.WebView.getFactory(WebView.java:1889)
01-05 15:55:27.676: W/webview_proxy(14876):     at android.webkit.WebView.ensureProviderCreated(WebView.java:1882)
01-05 15:55:27.676: W/webview_proxy(14876):     at android.webkit.WebView.setOverScrollMode(WebView.java:1935)
01-05 15:55:27.676: W/webview_proxy(14876):     at android.view.View.<init>(View.java:3316)
01-05 15:55:27.676: W/webview_proxy(14876):     at android.view.View.<init>(View.java:3383)
01-05 15:55:27.676: W/webview_proxy(14876):     at android.view.ViewGroup.<init>(ViewGroup.java:432)
01-05 15:55:27.676: W/webview_proxy(14876):     at android.widget.AbsoluteLayout.<init>(AbsoluteLayout.java:52)
01-05 15:55:27.676: W/webview_proxy(14876):     at android.webkit.WebView.<init>(WebView.java:505)
01-05 15:55:27.676: W/webview_proxy(14876):     at android.webkit.WebView.<init>(WebView.java:482)
01-05 15:55:27.676: W/webview_proxy(14876):     at android.webkit.WebView.<init>(WebView.java:468)
01-05 15:55:27.676: W/webview_proxy(14876):     at android.webkit.WebView.<init>(WebView.java:457)
01-05 15:55:27.676: W/webview_proxy(14876):     at android.webkit.WebView.<init>(WebView.java:447)
01-05 15:55:27.676: W/webview_proxy(14876):     at com.startapp.android.publish.Ad.load(Unknown Source)
01-05 15:55:27.676: W/webview_proxy(14876):     at com.startapp.android.publish.HtmlAd.load(Unknown Source)
01-05 15:55:27.676: W/webview_proxy(14876):     at com.startapp.android.publish.StartAppAd.loadAds(Unknown Source)
01-05 15:55:27.676: W/webview_proxy(14876):     at com.startapp.android.publish.StartAppAd.load(Unknown Source)
01-05 15:55:27.676: W/webview_proxy(14876):     at com.startapp.android.publish.StartAppAd.loadAd(Unknown Source)
01-05 15:55:27.676: W/webview_proxy(14876):     at com.startapp.android.publish.StartAppAd.loadAd(Unknown Source)
01-05 15:55:27.676: W/webview_proxy(14876):     at com.startapp.android.publish.StartAppAd.onResume(Unknown Source)
01-05 15:55:27.676: W/webview_proxy(14876):     at com.audiack.freetappgame.MainActivity.resume(MainActivity.java:41)
01-05 15:55:27.676: W/webview_proxy(14876):     at Screens.GameScreen.resume(GameScreen.java:699)
01-05 15:55:27.676: W/webview_proxy(14876):     at com.badlogic.gdx.Game.resume(Game.java:41)
01-05 15:55:27.676: W/webview_proxy(14876):     at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:480)
01-05 15:55:27.676: W/webview_proxy(14876):     at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1516)
01-05 15:55:27.676: W/webview_proxy(14876):     at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
01-05 15:55:27.706: W/dalvikvm(14876): threadid=11: thread exiting with uncaught exception (group=0x4159a438)
01-05 15:55:27.716: E/AndroidRuntime(14876): FATAL EXCEPTION: GLThread 8584
01-05 15:55:27.716: E/AndroidRuntime(14876): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
01-05 15:55:27.716: E/AndroidRuntime(14876):    at android.os.Handler.<init>(Handler.java:121)
01-05 15:55:27.716: E/AndroidRuntime(14876):    at android.webkit.WebViewClassic$PrivateHandler.<init>(WebViewClassic.java:10636)
01-05 15:55:27.716: E/AndroidRuntime(14876):    at android.webkit.WebViewClassic.<init>(WebViewClassic.java:1258)
01-05 15:55:27.716: E/AndroidRuntime(14876):    at android.webkit.WebViewClassic$Factory.createWebView(WebViewClassic.java:2031)
01-05 15:55:27.716: E/AndroidRuntime(14876):    at android.webkit.WebView.ensureProviderCreated(WebView.java:1882)
01-05 15:55:27.716: E/AndroidRuntime(14876):    at android.webkit.WebView.setOverScrollMode(WebView.java:1935)
01-05 15:55:27.716: E/AndroidRuntime(14876):    at android.view.View.<init>(View.java:3316)
01-05 15:55:27.716: E/AndroidRuntime(14876):    at android.view.View.<init>(View.java:3383)
01-05 15:55:27.716: E/AndroidRuntime(14876):    at android.view.ViewGroup.<init>(ViewGroup.java:432)
01-05 15:55:27.716: E/AndroidRuntime(14876):    at android.widget.AbsoluteLayout.<init>(AbsoluteLayout.java:52)
01-05 15:55:27.716: E/AndroidRuntime(14876):    at android.webkit.WebView.<init>(WebView.java:505)
01-05 15:55:27.716: E/AndroidRuntime(14876):    at android.webkit.WebView.<init>(WebView.java:482)
01-05 15:55:27.716: E/AndroidRuntime(14876):    at android.webkit.WebView.<init>(WebView.java:468)
01-05 15:55:27.716: E/AndroidRuntime(14876):    at android.webkit.WebView.<init>(WebView.java:457)
01-05 15:55:27.716: E/AndroidRuntime(14876):    at android.webkit.WebView.<init>(WebView.java:447)
01-05 15:55:27.716: E/AndroidRuntime(14876):    at com.startapp.android.publish.Ad.load(Unknown Source)
01-05 15:55:27.716: E/AndroidRuntime(14876):    at com.startapp.android.publish.HtmlAd.load(Unknown Source)
01-05 15:55:27.716: E/AndroidRuntime(14876):    at com.startapp.android.publish.StartAppAd.loadAds(Unknown Source)
01-05 15:55:27.716: E/AndroidRuntime(14876):    at com.startapp.android.publish.StartAppAd.load(Unknown Source)
01-05 15:55:27.716: E/AndroidRuntime(14876):    at com.startapp.android.publish.StartAppAd.loadAd(Unknown Source)
01-05 15:55:27.716: E/AndroidRuntime(14876):    at com.startapp.android.publish.StartAppAd.loadAd(Unknown Source)
01-05 15:55:27.716: E/AndroidRuntime(14876):    at com.startapp.android.publish.StartAppAd.onResume(Unknown Source)
01-05 15:55:27.716: E/AndroidRuntime(14876):    at com.audiack.freetappgame.MainActivity.resume(MainActivity.java:41)
01-05 15:55:27.716: E/AndroidRuntime(14876):    at Screens.GameScreen.resume(GameScreen.java:699)
01-05 15:55:27.716: E/AndroidRuntime(14876):    at com.badlogic.gdx.Game.resume(Game.java:41)
01-05 15:55:27.716: E/AndroidRuntime(14876):    at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:480)
01-05 15:55:27.716: E/AndroidRuntime(14876):    at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1516)
01-05 15:55:27.716: E/AndroidRuntime(14876):    at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
01-05 15:57:29.236: E/System(14876): Uncaught exception thrown by finalizer
01-05 15:57:29.246: E/System(14876): java.lang.NullPointerException
01-05 15:57:29.246: E/System(14876):    at android.webkit.WebViewClassic.destroy(WebViewClassic.java:2954)
01-05 15:57:29.246: E/System(14876):    at android.webkit.WebViewClassic.finalize(WebViewClassic.java:5366)
01-05 15:57:29.246: E/System(14876):    at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:186)
01-05 15:57:29.246: E/System(14876):    at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:169)
01-05 15:57:29.246: E/System(14876):    at java.lang.Thread.run(Thread.java:856)

这似乎与试图在开始活动中加载广告有关。这是我的启动活动(也是由于libgdx只在android上运行一个活动而导致的唯一活动):
package com.audiack.freetappgame;

import Screens.FreeTappGame;
import UtilityClasses.StartAppResolver;
import android.os.Bundle;
import android.os.Looper;

import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.badlogic.gdx.graphics.Texture;
import com.searchboxsdk.android.StartAppSearch;
import com.startapp.android.publish.StartAppAd;

public class MainActivity extends AndroidApplication implements StartAppResolver{
    public StartAppAd startAppAd = new StartAppAd(this);
    public StartAppSearch startAppSearch= new StartAppSearch();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();

        StartAppAd.init(this, "101164873", "201640058");
        StartAppSearch.init(this, "101164873", "201640058");

        Texture.setEnforcePotImages(false);
        cfg.useGL20 = true;

        initialize(new FreeTappGame(this), cfg);
        StartAppSearch.showSearchBox(this);
    }
    @Override
    public boolean showAds() {
        startAppAd.load();
        startAppAd.show();
        return true;
    }

    @Override
    public boolean resume() {
        startAppAd.onResume();
        return true;
    }
    @Override
    public boolean backButton() {

        return true;
    }
    @Override
    public boolean pause() {
        startAppAd.onPause();
        return true;
    }
}

如您所见,我让这个活动实现了我创建的一个接口,然后将活动本身传递给applicationlistener,从而允许libgdx方法与该活动交互。
这是一个界面;根据我数小时的研究,这是在libgdx中集成广告的最佳方法(但这些方法是针对类似的广告sdk的,而不是直接的startapp):
package UtilityClasses;

public interface StartAppResolver {
    public boolean showAds();
    public boolean resume();
    public boolean backButton();
    public boolean pause();
}

最后是applicationListener(freetappgame类):
package Screens;


import UtilityClasses.Assets;
import UtilityClasses.StartAppResolver;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;

public class FreeTappGame extends Game implements ApplicationListener {
    static float Width;
    static float Height;

    public StartAppResolver startAppResolver;

    public SplashScreen splash_screen;
    public MenuScreen menu_screen;
    public GameScreen game_screen;
    public HighscoreScreen highscore_screen;
    public AboutScreen about_screen;

    public FreeTappGame(StartAppResolver startAppResolver) {
        this.startAppResolver = startAppResolver;
    }

    @Override
    public void create() {
        Width = Gdx.graphics.getWidth();
        Height = Gdx.graphics.getHeight();
        Assets.load();

        splash_screen = new SplashScreen(this);
        menu_screen = new MenuScreen(this);
        game_screen = new GameScreen(this);
        highscore_screen = new HighscoreScreen(this);
        about_screen = new AboutScreen(this);

        setScreen(splash_screen);
    }
}

非常感谢你抽出时间。

最佳答案

我会删除这个线程,因为这里有一个类似的问题:WebView Methods is not called in android
但是在stackoverflow上并没有很多startapp解决方案,我认为这个问题将有利于那些试图集成startapp的人。我取代了

@Override
    public boolean showAds() {
        startAppAd.load();
        startAppAd.show();
        return true;
    }

具有
@Override
public boolean showAds() {
    this.runOnUiThread(new Runnable() {
        @Override
        public void run() {
        startAppAd.show();

        }
    });
    return true;
}

我把startPad.load()放在initialize方法之后:
initialize(new FreeTappGame(this), cfg);
startAppAd.load();

这对我很有帮助,我希望这能帮助别人。

07-25 21:15