2012-03-01 2 views
0

하나의 라이너 레이아웃으로 사용자 정의 상대 레이아웃을 추가 add_relative_layout.xml내가 XML 파일에 상대적 레이아웃 다음 만든

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:id="@+id/addAccountLinearLayout"> 



</LinearLayout> 

위 내가 코드 파일 아래의 복사본을 추가 할 메인 레이아웃이라고 할 수 있습니다 .

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/UIContainer" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

<RelativeLayout 

    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:color/white" > 

    <TextView 
     android:id="@+id/amountLabel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="5dp" 
     android:text="Amount" 
     android:textColor="@android:color/black" 
     android:textStyle="bold" /> 

    <EditText 
     android:id="@+id/amount" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:layout_marginRight="5dp" > 

    </EditText> 

나는 Show_all.xml라는 이름으로 또 다른 안드로이드 XML 파일이 있습니다. 그것은 내가 내가이 코드를 사용하고이 SHOW_ALL 레이아웃 현재

에서 원하는만큼 이상이 상대적 레이아웃을 추가 할 선형 레이아웃 XML

입니다

private void callOnCreate() 
     { 
      linear = (LinearLayout) findViewById(R.id.addAccountLinearLayout); // the layout in which i want to make dynamic copies of this layout. 
      layout = (RelativeLayout) findViewById(R.layout.ui_relative_layout_style); // name of xml File of above code. 

      for (int i=0; i < 4; i++) 
      { 
       Account account = accountArray.get(i); 
       linear.addView(layout, i); 
      } 
     } 

내가 널 포인트를 얻고있다 예외. 어떻게해야할지 말해줘.

안부

+0

아래입니다. 우리가 당신을 대답하는 데 도움이되도록 logcat을 게시하십시오. – deepa

+0

@umar 전체 logcat 넣고 NullPointerexception 당신에게 줄 줄. 귀하의 활동 중 어떤 줄이 Null이되는지 확인하십시오 –

답변

0

안녕하세요 우마르 코드 아래 사용하려고하지만 당신은 당신의 필수 건에 추가 얻을 코드 아래 사용할 수있는 XML 레이아웃 파일 LinearLayout

당신이 널 포인터 예외를 얻는 곳
public RelativeLayout createViewTOAdd(){ 
    lp=new RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.FILL_PARENT,android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT); 
    RelativeLayout mRelativeLayout=new RelativeLayout(this); 
    mRelativeLayout.setBackgroundColor(Color.WHITE); 
    TextView mTextView=new TextView(this); 
    RelativeLayout.LayoutParams Textview_lp=new RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT); 
    mTextView.setText("Amout"); 
    Textview_lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
    Textview_lp.addRule(RelativeLayout.CENTER_VERTICAL); 
    Textview_lp.leftMargin=10; 
    mTextView.setTextColor(Color.BLACK); 
    mTextView.setTextAppearance(this, R.style.TextStyle); 
    //mTextView.setLayoutParams(Textview_lp); 
    EditText mEditText=new EditText(this); 
    RelativeLayout.LayoutParams EditText_param=new RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT); 
    EditText_param.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
    EditText_param.addRule(RelativeLayout.CENTER_VERTICAL); 
    EditText_param.rightMargin=10; 
    //mEditText.setLayoutParams(EditText_param); 
    //mRelativeLayout.addView(mTextView, 0); 
    //mRelativeLayout.addView(mEditText, 1); 
    //mRelativeLayout.addView(mTextView); 
    //mRelativeLayout.addView(mEditText); 
    mRelativeLayout.addView(mTextView, Textview_lp); 
    mRelativeLayout.addView(mEditText, EditText_param); 
    return mRelativeLayout; 
} 

지금의 LinearLayout에에서보기를 추가하는 방법

mLinearLayout=(LinearLayout)findViewById(R.id.mainLinearView); 

    mLinearLayout.removeAllViews(); 

    for(int i=0;i<4;i++){ 
     mLinearLayout.addView(createViewTOAdd(), i); 
    } 
+0

drawable/abc.xml을 사용하여 relativelayout의 배경을 설정하는 방법을 알고 있습니까? –

+0

잘 목록보기를 추가하고 사용자 지정 어댑터를 만들었습니다. 하지만 어쨌든 도움을 주셔서 감사합니다 :) –

0

이봐, 당신은 당신이 특정 RelativeLayout의 별도의 레이아웃을 만들 exception.So 널 포인터를 얻고있는 이유는 현재의 레이아웃 show_all.xml의 .thats에서 사용할 수 없습니다 RelativeLayout의 인스턴스를 얻기 위해 findViewById를를 사용하는 와 이름이 UIContianer 그때 난 당신이 동적으로 추가하는 XML 레이아웃을 사용하는 방법을 몰라

private void callOnCreate() 
     { 
      linear = (LinearLayout) findViewById(R.id.addAccountLinearLayout); // the layout in which i want to make dynamic copies of this layout. 

LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); 
View vi = inflater.inflate(R.layout.ui_relative_layout_style, null); 
      for (int i=0; i < 4; i++) 
      { 
       Account account = accountArray.get(i); 
       linear.addView(vi, i); 
      } 
     } 
+0

새 코드를 확인하십시오. 오류가 발생했습니다 –

+0

오류가 발생하는 행을 알려주고 두 레이아웃 파일의 XML 코드도 게시하십시오 – Maneesh

+0

오류가 linear.addView (layout, i)에 있습니다. –

관련 문제