![](/img/trans.png)
[英]NullPointerException in ListView inside a LinearLayout
[英]LinearLayout NullPointerException in Eclipse
我在此代碼的第40行上收到NullPointerException。 以下是Google實施新的ad-mob廣告服務的示例,該服務將於今年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);
我修好了它。 這是問題所在:
為了啟動測試應用程序,我在eclipse中創建了一個新項目。 這帶有2個布局(activity_main和fragment_main)。
我已經在fragment_main布局中看到了hello world文本,由於某種原因,我錯誤地將linearLayout添加到了該xml中。
當我以為它指向正確的布局時,系統發現該布局確實不存在。 只是忽略了它。 這個小錯誤讓我煮了大約2個小時。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.