2012-01-04 2 views
2

이 상대 레이아웃에서 결국 하단에 광고를로드하려고합니다.기본 레이아웃을 참조 할 때 Null 포인터 예외

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:ads="http://schemas.android.com/lib/com.google.ads" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:gravity="center" > 

어떤 생각이 왜 이런 일 : mLayout이 레이아웃

RelativeLayout.LayoutParams rparams = (LayoutParams) mLayout.getLayoutParams(); 

E/AndroidRuntime(4066): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.name.project/com.name.project.Main}: java.lang.NullPointerException

XML 호출 될 때

public class Main extends Activity { 
RelativeLayout mLayout; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);   
    setContentView(R.layout.main); 
    mLayout = (RelativeLayout) findViewById(R.layout.main); 

    ImageView View = new ImageView(this, null);  //example view 

    mLayout.addView(View); 

그런 다음 Null 포인터 아래에 발생 h 오히려?

답변

6

오류는이 라인에서 발생

mLayout = (RelativeLayout) findViewById(R.layout.main); 

그것은 findViewById 방법의 이름으로 꽤 명백하다 - 그것의 ID로를 찾을하는 데 사용하지만, 당신은을 사용하여보기를 찾기 위해 노력하고 있습니다 레이아웃 참조, ID 참조가 아닙니다. 다음으로 시도해보십시오

또한
mLayout = (RelativeLayout) findViewById(R.id.main); 

, 당신의 RelativeLayoutandroid:id 태그를 포함해야한다. RelativeLayout에 대해 다음 XML을 사용해보십시오.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:ads="http://schemas.android.com/lib/com.google.ads" 
android:id="@+id/main" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:gravity="center" >