向Android应用添加广告

时间:2013-09-18 03:11:48

标签: android android-layout admob

我有一个非常简单的Android应用程序用于贷款计算。我正在用它来学习如何编写我正在研究的更大规模的程序。我使用过Android和AdMob的开发页面,但我似乎无法将广告显示在应用程序的顶部。

我有以下文件 MainActivity.java,activity_main.xml

activity_main.xml如下

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".LoanCalculator" >

<EditText
    android:id="@+id/editBalance"
    android:layout_width="80dp"
    android:layout_height="30dp"
    android:layout_alignBottom="@+id/balance"
    android:layout_alignParentRight="true"
    android:layout_marginRight="17dp"
    android:ems="10"
    android:textSize="10dp"
    android:inputType="number" >
</EditText>

<TextView
    android:id="@+id/rate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/balance"
    android:layout_below="@+id/balance"
    android:layout_marginTop="30dp"
    android:text="Rate (APR)" />

<EditText
    android:id="@+id/editRate"
    android:layout_width="80dp"
    android:layout_height="30dp"
    android:layout_alignBottom="@+id/rate"
    android:layout_alignParentRight="true"
    android:layout_marginRight="17dp"
    android:ems="10"
    android:textSize="10dp"
    android:inputType="number|numberDecimal" >
</EditText>

<TextView
    android:id="@+id/term"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/rate"
    android:layout_below="@+id/rate"
    android:layout_marginTop="30dp"
    android:text="Term (Mo.)" />

<EditText
    android:id="@+id/editTerm"
    android:layout_width="80dp"
    android:layout_height="30dp"
    android:layout_alignBottom="@+id/term"
    android:layout_alignParentRight="true"
    android:layout_marginRight="17dp"
    android:ems="10"
    android:textSize="10dp"
    android:inputType="number" >
</EditText>

<TextView
    android:id="@+id/payment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/term"
    android:layout_below="@+id/term"
    android:layout_marginTop="30dp"
    android:text="Monthly Payment" />

<TextView
    android:id="@+id/textMntlypmt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/payment"
    android:layout_alignBottom="@+id/payment"
    android:layout_alignLeft="@+id/editTerm"
    android:layout_alignRight="@+id/editTerm"
    android:text="" />

<Button
    android:id="@+id/calculate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/payment"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="60dp"
    android:onClick="ButtonOnClick"
    android:text="Calculate" />

<TextView
    android:id="@+id/balance"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/adView"
    android:layout_below="@+id/adView"
    android:layout_marginTop="20dp"
    android:text="Balance" />

<com.google.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    ads:adSize="BANNER"
    ads:adUnitId="ca-app-pub-2754978410424403/9803362252"
    ads:loadAdOnCreate="true" >
</com.google.ads.AdView>

</RelativeLayout>

我的.java是:

package biz.midl.loancalculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

public class MainActivity extends Activity implements OnClickListener {
    Button Home;
    private AdView adView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

        public void ButtonOnClick(View v) {
        // TODO Auto-generated method stub
            switch (v.getId()) {
            case R.id.calculate:

                EditText myEdit = (EditText) findViewById(R.id.editBalance);
                String myEditValue = myEdit.getText().toString();
                double loanAmount = Double.parseDouble(myEditValue);

                EditText myEdit2 = (EditText) findViewById(R.id.editRate);
                String myEditValue2 = myEdit2.getText().toString();
                double interestRate = Double.parseDouble(myEditValue2);

                EditText myEdit3 = (EditText) findViewById(R.id.editTerm);
                String myEditValue3 = myEdit3.getText().toString();
                Double loanPeriod = Double.parseDouble(myEditValue3);

                double r = interestRate/1200;
                double r1 = Math.pow(r+1,loanPeriod);

                double editMnthlypmt = (double) ((r+(r/(r1-1))) * loanAmount);

                TextView textMnthlypmt = (TextView)findViewById(R.id.textMntlypmt);
                textMnthlypmt.setText("" + String.valueOf(editMnthlypmt));

            break;
        }
    }

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
}

任何建议都将受到赞赏。

错误如下:

应用程序贷款计算器(进程.....)意外停止。

LogCats如下:

09-18 03:32:47.534: D/AndroidRuntime(335): Shutting down VM
09-18 03:32:47.534: W/dalvikvm(335): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
09-18 03:32:47.594: E/AndroidRuntime(335): FATAL EXCEPTION: main
09-18 03:32:47.594: E/AndroidRuntime(335): java.lang.RuntimeException: Unable to start activity ComponentInfo{biz.midl.loancalculator/biz.midl.loancalculator.MainActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class com.google.ads.AdView
09-18 03:32:47.594: E/AndroidRuntime(335):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-18 03:32:47.594: E/AndroidRuntime(335):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-18 03:32:47.594: E/AndroidRuntime(335):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-18 03:32:47.594: E/AndroidRuntime(335):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-18 03:32:47.594: E/AndroidRuntime(335):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-18 03:32:47.594: E/AndroidRuntime(335):  at android.os.Looper.loop(Looper.java:123)
09-18 03:32:47.594: E/AndroidRuntime(335):  at android.app.ActivityThread.main(ActivityThread.java:4627)
09-18 03:32:47.594: E/AndroidRuntime(335):  at java.lang.reflect.Method.invokeNative(Native Method)
09-18 03:32:47.594: E/AndroidRuntime(335):  at java.lang.reflect.Method.invoke(Method.java:521)
09-18 03:32:47.594: E/AndroidRuntime(335):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-18 03:32:47.594: E/AndroidRuntime(335):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-18 03:32:47.594: E/AndroidRuntime(335):  at dalvik.system.NativeStart.main(Native Method)
09-18 03:32:47.594: E/AndroidRuntime(335): Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class com.google.ads.AdView
09-18 03:32:47.594: E/AndroidRuntime(335):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
09-18 03:32:47.594: E/AndroidRuntime(335):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
09-18 03:32:47.594: E/AndroidRuntime(335):  at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
09-18 03:32:47.594: E/AndroidRuntime(335):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
09-18 03:32:47.594: E/AndroidRuntime(335):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
09-18 03:32:47.594: E/AndroidRuntime(335):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
09-18 03:32:47.594: E/AndroidRuntime(335):  at android.app.Activity.setContentView(Activity.java:1647)
09-18 03:32:47.594: E/AndroidRuntime(335):  at biz.midl.loancalculator.MainActivity.onCreate(MainActivity.java:23)
09-18 03:32:47.594: E/AndroidRuntime(335):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-18 03:32:47.594: E/AndroidRuntime(335):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-18 03:32:47.594: E/AndroidRuntime(335):  ... 11 more
09-18 03:32:47.594: E/AndroidRuntime(335): Caused by: java.lang.ClassNotFoundException: com.google.ads.AdView in loader dalvik.system.PathClassLoader[/data/app/biz.midl.loancalculator-1.apk]
09-18 03:32:47.594: E/AndroidRuntime(335):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
09-18 03:32:47.594: E/AndroidRuntime(335):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
09-18 03:32:47.594: E/AndroidRuntime(335):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
09-18 03:32:47.594: E/AndroidRuntime(335):  at android.view.LayoutInflater.createView(LayoutInflater.java:466)
09-18 03:32:47.594: E/AndroidRuntime(335):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
09-18 03:32:47.594: E/AndroidRuntime(335):  ... 20 more

3 个答案:

答案 0 :(得分:0)

请阅读Developer's Guide并按照说明操作。在您的情况下,错误似乎是:

java.lang.ClassNotFoundException: com.google.ads.AdView

表示AdMob SDK jar文件未添加到项目中。

答案 1 :(得分:0)

您必须遵循一些步骤:

第1步:注册AdMob

第2步:使用您的应用设置AdMob

第3步:获取AdMob库jar

第4步:设置Android应用

第5步:添加资料库:

第6步:设置视图

第7步:设置清单

如果出现任何问题,请参考此步骤。

您可以看到更多信息this示例。

答案 2 :(得分:0)

如文档中所述:https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals#android

       <com.google.ads.AdView android:id="@+id/adView"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     ads:adUnitId="MY_AD_UNIT_ID"
                     ads:adSize="BANNER"
                     ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
                     ads:loadAdOnCreate="true"/>
  

您还必须在ads:testDevices属性中添加自己的设备ID,才能在您的设备上投放测试广告。

on Activity onCreate执行以下操作:

// Look up the AdView as a resource and load a request.
AdView adView = (AdView)this.findViewById(R.id.adView);
adView.loadAd(new AdRequest());

还要确保在Mainfest.xml上添加了AdMob活动

  <activity android:name="com.google.ads.AdActivity"                   
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

最后

  

制作广告请求需要网络权限INTERNET和ACCESS_NETWORK_STATE,因此这些也必须在清单中声明:

使用以下内容:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>