2014-06-04 7 views
0

이 코드의 40 번째 줄에 NullPointerException이 발생합니다. 다음은 올해 8 월에 시행되는 새로운 광고 - 몹 광고 서비스의 Google 구현 예입니다. 이 코드는 문자 그대로 잘라내어 해당 문서에서 붙여 넣었지만 예외가 발생하여이를 해결할 수 없습니다. 여기 LinearLayout Eclipse에서 NullPointerException이 발생했습니다.

은 클래스입니다 :

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> 

어떤 아이디어?

추신 - 문제를 일으키는 특정 줄을 읽

layout.addView(adView); 
+0

LogCat에서''LinearLayout이 null입니까? –

+0

예. 나는 그 진술을 디버깅에 추가했다. – user2962806

+0

이미'Project-> Clean'을 시도해 보셨습니까? –

답변

0

내가 그것을 해결했습니다. 문제는 다음과 같습니다.

테스트 응용 프로그램을 시작하기 위해 이클립스에서 새 프로젝트를 만들었습니다. 이것은 2 개의 레이아웃 (activity_main 및 fragment_main)과 함께 제공됩니다.

나는 fragment_main 레이아웃에서 hello world 텍스트를 보았습니다. 그리고 어떤 이유로 인해이 XML에 linearLayout을 잘못 추가했습니다.

내가 올바른 레이아웃을 가리키고 있다고 생각하면서 시스템은 실제로 레이아웃이 실제로 존재하지 않는다고 생각하고있었습니다. 간단히 간과했다. 이 작은 실수로 약 2 시간 동안 나를 요리했습니다.

관련 문제