我刚刚完成了我的第一个应用程序,并希望将广告合并到应用程序的底部。广告横幅的编码仅从 Google 网站https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals获得,并添加到 mainactivity jar 中。一切似乎都很好,但是当我运行程序时,它崩溃了,logcat 和 jar 编码如下:
1)如何解决这些问题?(没有广告的程序可以正常运行)
2) 如何设置广告:testDevices="TEST_EMULATOR, TEST_DEVICE_ID" ???
3) 我的 xml 是一个 TableLayout,这有关系吗?(从 logcat 看来,它似乎无法正确夸大广告?)
提前谢谢了!!
罐子部分:
import com.google.ads.*;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
public class MainActivity extends Activity {
AdView adView;
public static final String MY_PUBLISHER_ID = "a123123123123123c"; //this one is correct
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//request TEST ads to avoid being disabled for clicking your own ads
AdRequest adRequest = new AdRequest();
//test mode on EMULATOR
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
//test mode on DEVICE (this example code must be replaced with your device unique ID)
adRequest.addTestDevice("4G74FC73D62D42B62A7F7DA61EF5F776"); //how to set this? this one is copied from the tutorial only
//create a Banner Ad
adView = new AdView(this, AdSize.BANNER, MY_PUBLISHER_ID);
//call the main layout from xml
TableLayout mainLayout = (TableLayout)findViewById(R.id.tableLayout);
//add the Banner Ad to our main layout
mainLayout.addView(adView);
// Initiate a request to load an ad in TEST mode.
// The test mode will work only on emulators and your specific test device,
// the users will get real ads.
adView.loadAd(adRequest);
}
// ad
@Override
public void onDestroy()
{
if (adView != null) { adView.destroy(); }
super.onDestroy();
}
// ad
Logcat如下:
10-10 01:17:34.230: D/dalvikvm(13612): Late-enabling CheckJNI
10-10 01:17:34.650: E/dalvikvm(13612): Could not find class 'com.google.ads.AdRequest', referenced from method com.pearappx.acctcalc.MainActivity.onCreate
10-10 01:17:34.650: W/dalvikvm(13612): VFY: unable to resolve new-instance 425 (Lcom/google/ads/AdRequest;) in Lcom/pearappx/acctcalc/MainActivity;
10-10 01:17:34.650: D/dalvikvm(13612): VFY: replacing opcode 0x22 at 0x0009
10-10 01:17:34.650: I/dalvikvm(13612): Could not find method com.google.ads.AdView.destroy, referenced from method com.pearappx.acctcalc.MainActivity.onDestroy
10-10 01:17:34.650: W/dalvikvm(13612): VFY: unable to resolve virtual method 3042: Lcom/google/ads/AdView;.destroy ()V
10-10 01:17:34.650: D/dalvikvm(13612): VFY: replacing opcode 0x6e at 0x0006
10-10 01:17:34.650: D/dalvikvm(13612): DexOpt: unable to opt direct call 0x0bdf at 0x0b in Lcom/pearappx/acctcalc/MainActivity;.onCreate
10-10 01:17:34.650: I/dalvikvm(13612): DexOpt: unable to optimize static field ref 0x02e7 at 0x0e in Lcom/pearappx/acctcalc/MainActivity;.onCreate
10-10 01:17:34.700: I/dalvikvm(13612): DexOpt: unable to optimize static field ref 0x02e8 at 0x1a in Lcom/pearappx/acctcalc/MainActivity;.onCreate
10-10 01:17:34.700: D/dalvikvm(13612): DexOpt: unable to opt direct call 0x0be1 at 0x1e in Lcom/pearappx/acctcalc/MainActivity;.onCreate
10-10 01:17:35.085: D/AndroidRuntime(13612): Shutting down VM
10-10 01:17:35.085: W/dalvikvm(13612): threadid=1: thread exiting with uncaught exception (group=0x40c781f8)
10-10 01:17:35.105: E/AndroidRuntime(13612): FATAL EXCEPTION: main
10-10 01:17:35.105: E/AndroidRuntime(13612): java.lang.NoClassDefFoundError: com.google.ads.AdRequest
10-10 01:17:35.105: E/AndroidRuntime(13612): at com.pearappx.acctcalc.MainActivity.onCreate(MainActivity.java:94)
10-10 01:17:35.105: E/AndroidRuntime(13612): at android.app.Activity.performCreate(Activity.java:4470)
10-10 01:17:35.105: E/AndroidRuntime(13612): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
10-10 01:17:35.105: E/AndroidRuntime(13612): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
10-10 01:17:35.105: E/AndroidRuntime(13612): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
10-10 01:17:35.105: E/AndroidRuntime(13612): at android.app.ActivityThread.access$600(ActivityThread.java:127)
10-10 01:17:35.105: E/AndroidRuntime(13612): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
10-10 01:17:35.105: E/AndroidRuntime(13612): at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 01:17:35.105: E/AndroidRuntime(13612): at android.os.Looper.loop(Looper.java:137)
10-10 01:17:35.105: E/AndroidRuntime(13612): at android.app.ActivityThread.main(ActivityThread.java:4511)
10-10 01:17:35.105: E/AndroidRuntime(13612): at java.lang.reflect.Method.invokeNative(Native Method)
10-10 01:17:35.105: E/AndroidRuntime(13612): at java.lang.reflect.Method.invoke(Method.java:511)
10-10 01:17:35.105: E/AndroidRuntime(13612): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
10-10 01:17:35.105: E/AndroidRuntime(13612): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
10-10 01:17:35.105: E/AndroidRuntime(13612): at dalvik.system.NativeStart.main(Native Method)