所以我按照这里找到的教程:developer site。
我的代码如下所示:
import android.os.Bundle;
import android.app.Activity;
import android.widget.LinearLayout;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
// A simple {@link Activity} that embeds an AdView.
public class MainActivity extends Activity {
// The view to show the ad.
private AdView adView;
// Your ad unit id. Replace with your actual ad unit id.
private static final String AD_UNIT_ID = "INSERT_YOUR_AD_UNIT_ID_HERE";
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create an ad.
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
// Add the AdView to the view hierarchy. The view will have no size
// until the ad is loaded.
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
layout.addView(adView);
// Create an ad request. Check logcat output for the hashed device ID to
// get test ads on a physical device.
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
.build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
}
@Override
public void onResume() {
super.onResume();
if (adView != null) {
adView.resume();
}
}
@Override
public void onPause() {
if (adView != null) {
adView.pause();
}
super.onPause();
}
// Called before the activity is destroyed.
@Override
public void onDestroy() {
// Destroy the AdView.
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
}
但是,我收到一个未解决的错误,导致我无法测试该应用。在线
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
我收到有关linearLayout未解决的错误消息。 R和R.id很好。当我查看我的R.java时,确实没有linearLayout。显然我不想把它放在那里,因为生成了R.java。
我仍然是Android开发的新手,我不知道如何解决这个问题。你如何把东西包括在R.java中?我已经尝试编辑activity_main.xml等文件以包含linearLayout,但是当我这样做并且我尝试运行我的应用程序时,我得到“安装错误:未知错误。请检查logcat以获取更多详细信息。”。当我看到它时,我不知道logcat试图告诉我什么。我有“import android.widget.LinearLayout”。我不知道该做什么/我做错了什么。
答案 0 :(得分:0)
你在解决这个问题上做了一些工作。比如
1)可能无法识别LinearLayout的id linearLayout 2)导入LinearLayout类 3)你做清理项目。 4)你删除bin和gen文件夹