我在这段代码的第40行得到一个NullPointerException。 以下是Google广告移动广告服务的实施示例,该服务将于今年8月生效。这段代码实际上是从他们的文档中剪切和粘贴的,但是我得到了例外并且无法解决它。
这是班级:
package com.example.com.stringtheory.newadmobtest;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
/**
* 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 = "ca-app-pub-6817177749834558/8445203226";
/** 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.linear);
if (layout == null)
{ Log.w("", "LinearLayout is null");
}
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()
.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();
}
}
这是相关的XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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="com.example.com.stringtheory.newadmobtest.MainActivity$PlaceholderFragment" >
<LinearLayout
android:id="@+id/linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_marginTop="112dp"
android:orientation="vertical" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</LinearLayout>
有什么想法导致这个?
PS - 导致问题的特定行读取:
layout.addView(adView);
答案 0 :(得分:0)
我修好了。这是问题所在:
为了启动测试应用程序,我在eclipse中创建了一个新项目。这带有2个布局(activity_main和fragment_main)。
我在fragment_main布局中看到了hello world文本,由于某种原因,我错误地将我的linearLayout添加到该xml中。
虽然我认为我指的是正确的布局,但系统看到布局确实不存在于我认为的位置。简单地忽略了它。在这个小错误上煮了大约2个小时。