2011-06-06 42 views
1

我是新来的android开发,但我想添加admob到我的应用程序使用surfaceview(panelView - GameView)。Android - Admob SurfaceView

我试过以下http://rx-games.com/admob-adverts-on-surfaceview-no-xml-tutorial/但我一定是做错了,因为rl.addView(panelView);添加一个nullPointerException。任何帮助是极大的赞赏。

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    //setContentView(R.layout.main); 
    AdView adView = new AdView(this, AdSize.BANNER, "a14ded47ad3779e"); 
    panelView = (GameView) findViewById(R.id.gameScreen); 

    RelativeLayout rl = new RelativeLayout(this); 
    rl.addView(panelView); 
    rl.addView(adView); 

    setContentView(rl); 

    threadView = panelView.getThread(); 
} 

回答

0

您从xml布局中膨胀GameView类并将其添加到根视图中,该视图不是在同一个xml中定义的视图。你有两个选择:

  1. 您创建SurfaceView完全用Java(如您发布的例子)

  2. 用户在使用XML布局AdView的(我宁愿这个选项在另一个)

如果命名xml文件game_layout.xml,您可以使用它作为:

setContentView(R.layout.game_layout.xml); 

game_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <com.google.ads.AdView 
    android:id="@+id/adView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    ads:adUnitId="@string/admob_id" 
    ads:adSize="BANNER" 
    ads:loadAdOnCreate="true" 
    android:layout_alignParentBotom="true" 
    /> 
    <your.package.GameView 
    android:id="@+id/gameScreen" 
    android:layout_alignParentTop="true" 
    android:layout_above="@id/adView" 
    /> 
</RelativeLayout> 
+0

就像我说的,我很新...我已经有4000+行代码创建了gameview类。我将如何在xml中调用这个和adView?如果你知道这个教程,那就行了。我不认为我理解视图如何工作... – teynon 2011-06-07 00:20:21

+0

@Tom在更新的响应中查看示例。关于教程,我建议你首先阅读开发指南:http://developer.android.com/guide/topics/ui/index.html – Aleadam 2011-06-07 00:38:45

+0

我标记你为接受的答案,但我仍然有问题...现在它给我一个运行时错误和一个空白屏幕。感谢您的帮助。我在军队里,在自己的闲暇时间里学习。 – teynon 2011-06-07 01:14:11