2012-06-17 81 views
1

我想在應用程序中包含admob橫幅,並且出現了這個奇怪的錯誤。任何幫助深表謝意。AdView未能實例化

下面是XML代碼

<com.google.ads.AdView android:id="@+id/adView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="bottom" 
    ads:adUnitId="..." 
    ads:adSize="BANNER" 
/> 

在活動中,我寫了下面的代碼

adview = (AdView)findViewById(R.id.adView); 
adview.setVisibility(View.VISIBLE); 
AdRequest re = new AdRequest(); 
adview.loadAd(re); 

在logcat中我碰到下面的錯誤

Not enough space to show ad! Wants: <480, 75>, Has: <0, 800> 

我懷疑必須有一個額外的問題,因爲我將AdView的寬度設置爲「wrap_content」

在XML文件中的圖形視圖有此警告

enter image description here

,當我點擊這個錯誤我看到有一個java.lang.UnsupportedOperationException

如果你想以進一步調查此處是完整異常堆棧跟蹤

java.lang.UnsupportedOperationException 
at com.android.layoutlib.bridge.android.BridgeContext.getApplicationContext(BridgeContext.java:1272) 
at com.google.ads.AdView.a(SourceFile:155) 
at com.google.ads.AdView.a(SourceFile:329) 
at com.google.ads.AdView.<init>(SourceFile:115) 
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) 
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:513) 
at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.instantiateClass(ProjectCallback.java:397) 
at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.loadView(ProjectCallback.java:165) 
at com.android.layoutlib.bridge.android.BridgeInflater.loadCustomView(BridgeInflater.java:205) 
at com.android.layoutlib.bridge.android.BridgeInflater.createViewFromTag(BridgeInflater.java:133) 
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:83) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:702) 
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:86) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:702) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:454) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:367) 
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:324) 
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:321) 
at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:325) 
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.RenderService.createRenderSession(RenderService.java:372) 
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.renderWithBridge(GraphicalEditorPart.java:1323) 
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.recomputeLayout(GraphicalEditorPart.java:1077) 
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.activated(GraphicalEditorPart.java:903) 
at com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditor.pageChange(LayoutEditor.java:399) 
at org.eclipse.ui.part.MultiPageEditorPart$2.widgetSelected(MultiPageEditorPart.java:290) 
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:234) 
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84) 
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:3783) 
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1375) 
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1398) 
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1383) 
at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:1195) 
at org.eclipse.swt.custom.CTabFolder.setSelection(CTabFolder.java:2743) 
at org.eclipse.swt.custom.CTabFolder.onMouse(CTabFolder.java:1429) 
at org.eclipse.swt.custom.CTabFolder$1.handleEvent(CTabFolder.java:257) 
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84) 
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:3783) 
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1375) 
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1398) 
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1383) 
at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:1195) 
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3629) 
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3284) 
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2640) 
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2604) 
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2438) 
at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:671) 
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332) 
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:664) 
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149) 
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:115) 
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196) 
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110) 
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79) 
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:369) 
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
at java.lang.reflect.Method.invoke(Method.java:597) 
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:620) 
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:575) 
at org.eclipse.equinox.launcher.Main.run(Main.java:1408) 
+0

問題將在運行時間之前。我的意思是你的圖形視圖的佈局需要工作。它應該顯示「com.google ... AdView」,如果它工作正常。我不能說出其他佈局有什麼問題。如果你把整件事放在這裏,有人 - 也許是我 - 可以看看。 – DrA

+0

您的佈局高度和寬度可能會阻止它正確顯示 – xDragonZ

+0

您確定@xDragonZ,因爲我將寬度和高度設置爲wrap_content。你怎麼看待相關的UnsupportedOperationException?我想這個例外可以防止廣告被顯示,這就是爲什麼我得到「空間不夠」的錯誤。 AdView不包含任何廣告,因此wrap_content屬性的寬度爲0。 – 0x5f3759df

回答

1

Adview請求在佈局創建後開始。所以你必須給佈局提供特定的高度和寬度,使佈局具有一定的權重。你提供了佈局的高度和寬度的wrap_content,所以它最初是一個0dp的寬度,所有的視圖元素基於這個加載到頁面中。只需用固定的版面大小類似這樣的嘗試,

<com.google.ads.AdView android:id="@+id/adView" 
    android:layout_width="480dp" 
    android:layout_height="75dp" 
    android:gravity="bottom" 
    ads:adUnitId="..." 
    ads:adSize="BANNER" 
/> 

,你也不得不提一下在XMLNS AdMob的佈局XML

xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 

在資源,你必須聲明的AdMob包這樣

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="com.admob.android.ads.AdView"> 
    </declare-styleable> 
</resources> 
+0

嗯,我不相信這可以解決我的問題,但確實如此。無論如何,謝謝你的時間。 – 0x5f3759df

0

我有同樣的問題,我解決了它。你只是做出admob的正確尺寸,它需要480,75.