经过无数个小时后,我无法让新的admob与我的应用程序一起工作

时间:2014-08-05 14:49:05

标签: android

我已经跟踪了我能找到的每个链接,但是我一直收到同样的错误,说有一个错误的膨胀类come.google.android.gsm.ads.Adview有人可以帮助我吗?

我曾尝试使用https://developers.google.com/mobile-ads-sdk/docs/admob/play-migration来帮助我实现这一目标,但仍然无法让它运行。我正在运行4.4.3的nexus 5上运行此代码。

public class MainActivity extends Activity{

double distanceTraveled, fuelUsed, part1, mpg;
EditText distance, fuel, total;
Button calculate;

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

    distance = (EditText) findViewById(R.id.etDistance);
    fuel = (EditText) findViewById(R.id.etFuel);
    calculate = (Button) findViewById(R.id.btCalc); 
    total = (EditText) findViewById(R.id.etMPG);

    AdView adView = (AdView)this.findViewById(R.id.advert);
    AdRequest adRequest = new AdRequest.Builder()
        .build();
    adView.loadAd(adRequest);

    calculate.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

        InputMethodManager inputManager = (InputMethodManager)    getSystemService(Context.INPUT_METHOD_SERVICE); 
        inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

        if(distance.getText().toString().length() > 0){
            distanceTraveled = Double.parseDouble(distance.getText().toString());
        }else{
            distanceTraveled=0;
        }
        if(fuel.getText().toString().length() > 0){  
            fuelUsed = Double.parseDouble(fuel.getText().toString());
        }else{
            fuelUsed=1;
        }
            part1 = fuelUsed / 4.55;
            mpg = distanceTraveled / part1;
            total.setText(String.format("%.2f", mpg));
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.aliancegaming.pictureexample"
      android:versionCode="2"
      android:versionName="1.1" >
<uses-sdk android:minSdkVersion="9"
          android:targetSdkVersion="20" />

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

<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" 
             android:theme="@style/AppTheme" android:allowBackup="true">
    <activity android:name="com.groupdigital.ukmpgcalculator.MainActivity"
              android:label="@string/title_activity_main" 
              android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

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

    <meta-data android:name="com.google.android.gms.version"
           android:value="@integer/google_play_services_version" />

</application>

</manifest>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/libs/com.groupdigital.ukmpgcalculator"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<com.google.android.gms.ads.AdView
    android:id="@+id/advert"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginTop="48dp"
    ads:adSize="SMART_BANNER"
    ads:adUnitId="a150bb48a445a2f" />

<TextView
    android:id="@+id/tvDistance"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/advert"
    android:layout_marginLeft="24dp"
    android:layout_marginTop="8dp"
    android:text="@string/distance" />

<EditText
    android:id="@+id/etDistance"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/tvDistance"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/tvDistance"
    android:layout_marginRight="24dp"
    android:layout_marginTop="8dp"
    android:ems="10"
    android:hint="@string/dis"
    android:inputType="numberDecimal" >

    <requestFocus
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</EditText>

<TextView
    android:id="@+id/tvFuel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/etDistance"
    android:layout_below="@+id/etDistance"
    android:layout_marginTop="8dp"
    android:text="@string/litres" />

<EditText
    android:id="@+id/etFuel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/tvFuel"
    android:layout_below="@+id/tvFuel"
    android:layout_marginTop="8dp"
    android:layout_alignParentRight="true"
    android:layout_marginRight="24dp"
    android:hint="@string/lit"
    android:inputType="numberDecimal" />

<Button
    android:id="@+id/btCalc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/etFuel"
    android:layout_below="@+id/etFuel"
    android:layout_marginTop="8dp"
    android:text="@string/calculate" />

<TextView
    android:id="@+id/tvMPG"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/btCalc"
    android:layout_below="@+id/btCalc"
    android:layout_marginTop="8dp"
    android:text="@string/mpg" />

<EditText
    android:id="@+id/etMPG"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/tvMPG"
    android:layout_below="@+id/tvMPG"
    android:layout_marginTop="8dp"
    android:layout_alignParentRight="true"
    android:layout_marginRight="24dp"
    android:ems="10"
    android:clickable="false"
    android:focusable="false"
    android:inputType="numberDecimal" />

<TabHost
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <LinearLayout
                android:id="@+id/tab1"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                 android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab3"
                 android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>

</RelativeLayout>

Logcat输出:

08-05 16:06:16.602: D/AndroidRuntime(8240): Shutting down VM
08-05 16:06:16.602: E/AndroidRuntime(8240): FATAL EXCEPTION: main
08-05 16:06:16.602: E/AndroidRuntime(8240): Process: com.aliancegaming.pictureexample, PID: 8240
08-05 16:06:16.602: E/AndroidRuntime(8240): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aliancegaming.pictureexample/com.groupdigital.ukmpgcalculator.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class com.google.android.gms.ads.AdView
08-05 16:06:16.602: E/AndroidRuntime(8240):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
08-05 16:06:16.602: E/AndroidRuntime(8240):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
08-05 16:06:16.602: E/AndroidRuntime(8240):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-05 16:06:16.602: E/AndroidRuntime(8240):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-05 16:06:16.602: E/AndroidRuntime(8240):     at android.os.Handler.dispatchMessage(Handler.java:102)
08-05 16:06:16.602: E/AndroidRuntime(8240):     at android.os.Looper.loop(Looper.java:136)
08-05 16:06:16.602: E/AndroidRuntime(8240):     at android.app.ActivityThread.main(ActivityThread.java:5001)
08-05 16:06:16.602: E/AndroidRuntime(8240):     at java.lang.reflect.Method.invoke(Native Method)
08-05 16:06:16.602: E/AndroidRuntime(8240):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
08-05 16:06:16.602: E/AndroidRuntime(8240):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
08-05 16:06:16.602: E/AndroidRuntime(8240): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class com.google.android.gms.ads.AdView
08-05 16:06:16.602: E/AndroidRuntime(8240):     at android.view.LayoutInflater.createView(LayoutInflater.java:620)
08-05 16:06:16.602: E/AndroidRuntime(8240):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
08-05 16:06:16.602: E/AndroidRuntime(8240):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
08-05 16:06:16.602: E/AndroidRuntime(8240):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
08-05 16:06:16.602: E/AndroidRuntime(8240):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
08-05 16:06:16.602: E/AndroidRuntime(8240):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
08-05 16:06:16.602: E/AndroidRuntime(8240):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
08-05 16:06:16.602: E/AndroidRuntime(8240):     at android.app.Activity.setContentView(Activity.java:1929)
08-05 16:06:16.602: E/AndroidRuntime(8240):     at com.groupdigital.ukmpgcalculator.MainActivity.onCreate(MainActivity.java:23)
08-05 16:06:16.602: E/AndroidRuntime(8240):     at android.app.Activity.performCreate(Activity.java:5231)
08-05 16:06:16.602: E/AndroidRuntime(8240):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-05 16:06:16.602: E/AndroidRuntime(8240):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
08-05 16:06:16.602: E/AndroidRuntime(8240):     ... 9 more
08-05 16:06:16.602: E/AndroidRuntime(8240): Caused by: java.lang.reflect.InvocationTargetException
08-05 16:06:16.602: E/AndroidRuntime(8240):     at java.lang.reflect.Constructor.newInstance(Native Method)
08-05 16:06:16.602: E/AndroidRuntime(8240):     at android.view.LayoutInflater.createView(LayoutInflater.java:594)
08-05 16:06:16.602: E/AndroidRuntime(8240):     ... 20 more
08-05 16:06:16.602: E/AndroidRuntime(8240): Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/R$styleable;
08-05 16:06:16.602: E/AndroidRuntime(8240):     at com.google.android.gms.internal.ao.<init>(Unknown Source)
08-05 16:06:16.602: E/AndroidRuntime(8240):     at com.google.android.gms.internal.au.<init>(Unknown Source)
08-05 16:06:16.602: E/AndroidRuntime(8240):     at com.google.android.gms.internal.au.<init>(Unknown Source)
08-05 16:06:16.602: E/AndroidRuntime(8240):     at com.google.android.gms.ads.AdView.<init>(Unknown Source)
08-05 16:06:16.602: E/AndroidRuntime(8240):     ... 22 more
08-05 16:06:16.602: E/AndroidRuntime(8240): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.R$styleable" on path: DexPathList[[zip file "/data/app/com.aliancegaming.pictureexample-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.aliancegaming.pictureexample-2, /vendor/lib, /system/lib]]
08-05 16:06:16.602: E/AndroidRuntime(8240):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
08-05 16:06:16.602: E/AndroidRuntime(8240):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
08-05 16:06:16.602: E/AndroidRuntime(8240):     at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
08-05 16:06:16.602: E/AndroidRuntime(8240):     ... 26 more
08-05 16:06:16.602: E/AndroidRuntime(8240):     Suppressed: java.lang.ClassNotFoundException: com.google.android.gms.R$styleable
08-05 16:06:16.602: E/AndroidRuntime(8240):         at java.lang.Class.classForName(Native Method)
08-05 16:06:16.602: E/AndroidRuntime(8240):         at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
08-05 16:06:16.602: E/AndroidRuntime(8240):         at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
08-05 16:06:16.602: E/AndroidRuntime(8240):         at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
08-05 16:06:16.602: E/AndroidRuntime(8240):         ... 27 more
08-05 16:06:16.602: E/AndroidRuntime(8240):     Caused by: java.lang.NoClassDefFoundError: Class "Lcom/google/android/gms/R$styleable;" not found
08-05 16:06:16.602: E/AndroidRuntime(8240):         ... 31 more

2 个答案:

答案 0 :(得分:0)

您需要ad xml命名空间:

xmlns:ads="http://schemas.android.com/apk/res-auto"

您可以将其添加到父布局节点:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/libs/com.groupdigital.ukmpgcalculator"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"

答案 1 :(得分:0)

它现在已经修复了我没有意识到,你现在导入一个名为google-play-services-lib的整个项目,而不是像google-play-services-lib那样添加google-play-services.jar,然后告诉应用程序将它添加为底部的android下的库而不是Java Build路径的通常位置。它现在没有作为库添加,也没有按顺序和导出。