0

I have a very simple android application for loan calculations. I'm using this to learn how to program a larger scale program i'm working on. I've used the development pages from Android and AdMob, but I cannot seem to get the advert to show at the top of the app.

I have the following files MainActivity.java, activity_main.xml

activity_main.xml is as follows

<?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>

My .java is:

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

        }
}

any suggestions would be appreciated.

Error is as follows:

The application Loan Calculator (process.....) has stopped unexpectedly.

LogCats are as follows:

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
4

3 回答 3

0

Please read the Developer's Guide and follow the instructions. In your case, the error seems to be:

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

which means that the AdMob SDK jar file is not added to the project.

于 2013-09-18T04:01:05.587 回答
0

你必须遵循一些步骤:

第 1 步:注册 AdMob

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

第 3 步:获取 AdMob 库 jar

第 4 步:设置 Android 应用程序

第 5 步:添加库:

第 6 步:设置视图

第 7 步:设置清单

如果有任何问题发表评论,请参考此步骤。

更多信息你可以看到这个例子。

于 2013-09-18T04:18:51.760 回答
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,才能在您的设备上获取测试广告。

在 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"/>
于 2013-09-18T04:24:16.800 回答