2012-06-28 6 views
1

다른 질문을 둘러 보았지만이 시나리오와 완전히 일치하는 것을 찾지 못했습니다. XML로 RelativeLayout을 정의했다. 이 전체 RelativeLayout을 GraphView 객체 아래에 놓고 싶습니다 (별도로 만든 클래스에서). RelativeLayout을 GraphView 아래에 두는 가장 좋은 방법은 프로그래밍 방식으로 정의하려고 시도한 LinearLayout 내부에이 두 가지를 붙이는 것입니다.프로그래밍 방식으로 정의한 LinearLayout에 RelativeLayout (XML로부터)을 어떻게 추가합니까?

이것은 내가 지금까지 가지고있는 것입니다. 문제가 있습니다. 그러나 나는 무엇이 확실하지 않습니다. 당신의 도움에 대한

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.LinearLayout; 
import android.widget.RelativeLayout; 

public class Grapher extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState);   
     LinearLayout ll = new LinearLayout(this); 
     GraphView gv = new GraphView(this); 
     RelativeLayout rl = (RelativeLayout) findViewById(R.id.mainRelativeLayout); 
     ll.addView(gv); 
     ll.addView(rl); 
     setContentView(ll); 

하고 XML

....

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/mainRelativeLayout" 
    android:orientation="vertical"> 
    <Button 
     android:id="@+id/second" 
     android:clickable="true" 
     android:background="@drawable/button_chooser" 
     android:layout_width="70dp" 
     android:layout_height="40dp" 
     android:text="@string/second" 
     android:textColor="@color/white" 
     android:textSize="15sp"/> 
    <Button 
     android:id="@+id/alpha" 
     android:clickable="true" 
     android:background="@drawable/button_chooser" 
     android:layout_width="70dp" 
     android:layout_height="40dp" 
     android:layout_below="@id/second" 
     android:layout_alignParentLeft="true" 
     android:text="@string/alpha" 
     android:textColor="@color/white" 
     android:textSize="15sp" /> 
    <Button 
     android:id="@+id/mode" 
     android:clickable="true" 
     android:background="@drawable/button_chooser" 
     android:layout_width="70dp" 
     android:layout_height="40dp" 
     android:layout_toRightOf="@id/second" 
     android:text="@string/mode" 
     android:textColor="@color/white" 
     android:textSize="15sp" /> 
</RelativeLayout> 

감사합니다. 나는 이것이 내가보고있는 매우 단순한 무엇인가라는 직감을 가지고있다.

+0

오류 로그가 있습니까? – Guardanis

+0

무엇을하고 있습니까? 그래서 아무것도 얻지 못했습니까? 그래프 뷰 만? – Barak

답변

3

Activity.findViewById은 활동에 바인딩 된보기 (일반적으로 setLayout(R.layout.yourlayout);을 호출 한 결과)를 검색합니다. 호출하지 않으므로 현재 활성 상태 인 뷰가 없으므로 findViewById을 호출하면 null이 반환됩니다.

xml에서 레이아웃을 부 풀린 다음 프로그래밍 방식으로보기에 추가하는 것으로 나타납니다. 당신이해야 할 일은 다음과 같습니다 :

Inflater inflater = LayoutInflater.from(context); 
//this will inflate the mainRelativeLayout into the linear layout you called "ll" 
inflater.inflate(R.id.mainRelativeLayout, ll); 
관련 문제