2017-03-22 4 views
0

私はゲームにバナー広告を追加しようとしています。そのためには、私のlibgdxゲームの親ViewGroupが必要です。私のコードは次のとおりです。libgdxゲームの親ViewGroupを取得するには?

final Activity activity = (Activity) this; 
final ViewGroup parent = (ViewGroup) ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0); // exception here 

しかしゲームがクラッシュし、私は次のエラーでこれをしようとすると:

java.lang.ClassCastException: 
    com.badlogic.gdx.backends.android.surfaceview.GLSurfaceView20 
    cannot be cast to android.view.ViewGroup 
+0

「ViewGroup」という名前の2つの異なるクラスを混在させる可能性が最も高いです。例外から、 'android.view.ViewGroup'が必要であることがわかります。完全なクラス名を提供し、それが役立つかどうか確認してください。 – munyul

答えて

0

の子孫であるかどうかを確認するためにinstanceofテストを実行することができます。

public RelativeLayout layout; 

@Override 
protected void onCreate (Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     layout = new RelativeLayout(this); 
     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT); 
     layout.setLayoutParams(params); 

     AndroidApplicationConfiguration config = new AndroidApplicationConfiguration(); 
     View gameView=initializeForView(new Main(), config); 

     RelativeLayout.LayoutParams gameViewParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
     gameViewParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); 
     gameViewParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); 
     gameView.setLayoutParams(gameViewParams); 

     layout.addView(gameView); 
     //create banner view and embed/add into layout 

     setContentView(layout); 
} 
0

あなたのルートのレイアウトは、Viewの子孫である、SurfaceViewの子孫であるGLSurfaceView20ですではない a ViewGroupです。

How to get parent ViewGroup of libgdx game?

あなたの質問に答える:親がかViewGroupであってもなくてもよい(あなたの場合にはそうではありません)。あなたは、コンテンツViewを取得し、それはそうsetContentView方法でレイアウトするために取得し、独自のレイアウトにし、エンドsetContentViewに追加initializeForViewメソッドの戻りView、これを試してみてくださいViewGroup

関連する問題