Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Admob广告添加到Java文件而不是Xml_Java_Android_Xml_Eclipse_Admob - Fatal编程技术网

将Admob广告添加到Java文件而不是Xml

将Admob广告添加到Java文件而不是Xml,java,android,xml,eclipse,admob,Java,Android,Xml,Eclipse,Admob,添加Admob广告时遇到问题。首先,我正在开发一个不使用xml布局的游戏。我只是用Java。 在Google上的说明中,它查找linearlayout id。 任何帮助都会很好。 以下是清单中声明的我的主要活动java文件的代码: import android.app.Activity; import android.os.Bundle; import android.view.Window; import android.view.WindowManager; import android.w

添加Admob广告时遇到问题。首先,我正在开发一个不使用xml布局的游戏。我只是用Java。 在Google上的说明中,它查找linearlayout id。 任何帮助都会很好。 以下是清单中声明的我的主要活动java文件的代码:

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import com.google.android.gms.ads.*;

public class CrazyEightsActivity extends Activity {
/** Called when the activity is first created. */
private AdView adView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TitleView tView = new TitleView(this);
    tView.setKeepScreenOn(true);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                         WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);
 // Create the adView.
    adView = new AdView(this);
    adView.setAdUnitId("ca-app-pub-6705249916909813/9815575080");
    adView.setAdSize(AdSize.BANNER);

    // Lookup your LinearLayout assuming it's been given
    // the attribute android:id="@+id/mainLayout".
    LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);

    // Add the adView to it.
    layout.addView(adView);

    // Initiate a generic request.
    AdRequest adRequest = new AdRequest.Builder().build();

    // Load the adView with the ad request.
    adView.loadAd(adRequest);
  }    
}
以下是我不应该使用的xml布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

下面是我想作为主要内容视图的java文件的代码:

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.View;

public class TitleView extends View {

private Bitmap titleGraphic;
private Bitmap playButtonUp;
private Bitmap playButtonDown;
private int screenW;
private int screenH;
private boolean playButtonPressed;
private Context myContext;

public TitleView(Context context) {
    super(context);
    myContext = context;
    titleGraphic = BitmapFactory.decodeResource(getResources(), R.drawable.title_graphic);
    playButtonUp = BitmapFactory.decodeResource(getResources(), R.drawable.play_button_up);
    playButtonDown = BitmapFactory.decodeResource(getResources(), R.drawable.play_button_down);
}

@Override
public void onSizeChanged (int w, int h, int oldw, int oldh){
    super.onSizeChanged(w, h, oldw, oldh);
    screenW = w;
    screenH = h;
    System.out.println("SCREEN W: " + screenW);
    System.out.println("SCREEN H: " + screenH);
}

@Override 
protected void onDraw(Canvas canvas) {
    canvas.drawBitmap(titleGraphic, (screenW-titleGraphic.getWidth())/2, 0, null);
    if (playButtonPressed) {
        canvas.drawBitmap(playButtonDown, (screenW-playButtonUp.getWidth())/2, (int)(screenH*0.7), null);   
    } else {
        canvas.drawBitmap(playButtonUp, (screenW-playButtonUp.getWidth())/2, (int)(screenH*0.7), null);         
    }
}



 public boolean onTouchEvent(MotionEvent event) {
        int eventaction = event.getAction();   
        int X = (int)event.getX();
        int Y = (int)event.getY();

        switch (eventaction ) {

        case MotionEvent.ACTION_DOWN: 
            if (X > (screenW-playButtonUp.getWidth())/2 &&
                X < ((screenW-playButtonUp.getWidth())/2) + playButtonUp.getWidth() &&
                Y > (int)(screenH*0.7) &&
                Y < (int)(screenH*0.7) + playButtonUp.getHeight()) {
                playButtonPressed = true;
            }                   
            break;

        case MotionEvent.ACTION_MOVE: 
            break;

        case MotionEvent.ACTION_UP:
            if (playButtonPressed) {
                Intent gameIntent = new Intent(myContext, GameActivity.class);
                myContext.startActivity(gameIntent);                    
            }
            playButtonPressed = false;
            break;
        } 
        invalidate();
        return true;      
}
}
导入android.content.Context;
导入android.content.Intent;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.graphics.Canvas;
导入android.view.MotionEvent;
导入android.view.view;
公共类标题视图扩展了视图{
私有位图标题;
私人位图播放按钮;
私人位图playButtonDown;
私人int screenW;
私人互联网;
私有布尔播放按钮按下;
私人语境;
公共标题视图(上下文){
超级(上下文);
myContext=上下文;
titleGraphic=BitmapFactory.decodeResource(getResources(),R.drawable.title\u-graphic);
playButtonUp=BitmapFactory.decodeResource(getResources(),R.drawable.play\u button\u up);
playButtonDown=BitmapFactory.decodeResource(getResources(),R.drawable.play_按钮向下);
}
@凌驾
已更改尺寸上的公共空隙(整数w、整数h、整数oldw、整数oldh){
super.onSizeChanged(w,h,oldw,oldh);
屏幕w=w;
屏幕h=h;
System.out.println(“屏幕W:+screenW”);
System.out.println(“屏幕H:+screenH”);
}
@凌驾
受保护的void onDraw(画布){
drawBitmap(titleGraphic,(screenW titleGraphic.getWidth())/2,0,null);
如果(按播放按钮){
drawBitmap(playButtonDown,(screenW playbutnup.getWidth())/2,(int)(screenH*0.7),null);
}否则{
drawBitmap(playbutnup,(screenW playbutnup.getWidth())/2,(int)(screenH*0.7),null);
}
}
公共布尔onTouchEvent(运动事件){
int eventaction=event.getAction();
int X=(int)event.getX();
int Y=(int)event.getY();
开关(事件操作){
case MotionEvent.ACTION\u DOWN:
如果(X>(screenW playButtonUp.getWidth())/2&&
X<((屏幕w playButtonUp.getWidth())/2)+playButtonUp.getWidth()&&
Y>(整数)(屏幕H*0.7)&&
Y<(int)(屏幕高度*0.7)+Playbutnup.getHeight(){
playButtonPressed=真;
}                   
打破
case MotionEvent.ACTION\u移动:
打破
case MotionEvent.ACTION\u UP:
如果(按播放按钮){
Intent gameIntent=新的Intent(myContext,GameActivity.class);
myContext.startActivity(游戏意图);
}
playButtonPressed=假;
打破
} 
使无效();
返回true;
}
}

首先将谷歌移动广告SDK放入lib文件夹

在上激活帐户后,您需要在account中配置一个应用程序。之后,Admob将为您提供Admob的Android SDK,其中包含Admob.jar和工作示例

private void AdmobFooterInitialize(){
    adView = new AdView(this, AdSize.SMART_BANNER, "Your_AdMobPUBLISHERID");
    LinearLayout layout = (LinearLayout)findViewById(R.id.linear);
    layout.addView(adView);
    AdRequest request = new AdRequest();
    request.addTestDevice("your device id"); // this for testing 
    request.setTesting(false);
    adView.loadAd(request);
}
在manifest.xml中

 <meta-data android:value="true" android:name="ADMOB_ALLOW_LOCATION_FOR_ADS" />
 <activity android:name="com.google.ads.AdActivity"
  android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
字符串deviceid=tm.getDeviceId()


我认为这个示例对您有帮助

我发现使用
adMob
在文件中明确定义布局可能是一个非常头痛的问题,因为有时它可能会因为某些参数和布局组合而变得不可预测。似乎总是有效的方法是将
AdView
动态添加到一些
线性布局中

这应该起作用:

final LinearLayout yourLayout = (LinearLayout) findViewById(R.id.your_linearlayout_id_where_you_want_to_put_your_adview);

final AdView adView = new AdView(this);
adView.setAdUnitId("your-admob-id");
adView.setAdSize(AdSize.BANNER);

yourLayout.addView(adView);

final AdRequest.Builder adReq = new AdRequest.Builder();
// You should include a line like this for testing purposes,
// but only after you've tested whether your AdView works!
// This will prevent your ad being loaded each time you test
// your ad, so it will prevent you being blocked from AdMob.
// You'll find your device_id in the LogCat.
adReq.addTestDevice("your_device_id");

final AdRequest adRequest = adReq.build();
adView.loadAd(adRequest);

我将通过XML配置标题视图。例如,将布局更改为:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<your.titleview.package.name.here.TitleView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

</LinearLayout>


并删除在onCreate中构造它的代码。

您的问题是什么?对不起,我的问题是当我将ContentView设置为activity_main时,我得到了广告,但没有内容,因为我的所有内容都在TitleView.java文件中。那么,我该如何让这些广告出现在标题视图文件上呢?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<your.titleview.package.name.here.TitleView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

</LinearLayout>