2014-05-15 13 views
0

레이아웃 폴더 main.xmlhidden.xml에 두 개의 xml 파일이 있습니다. 내 활동에서 콘텐츠보기는 으로 설정되고 setcontentview을 사용합니다. 내가 원하는 것은 main.xmlhidden.xml으로 부풀리기 위해서 특정 버튼을 눌렀을 때이다. 이를 위해 아래 게시 된 코드를 작성했습니다.LinearLayout은 동적으로 만들 때 null을 반환합니다.

그러나 문제는 LinearLayout이 null인지 여부를 확인하는 동안 toast에 각각의 메시지가 표시 될 때 null을 반환합니다.

내 질문에 hidden.xml에서 사용할 수 있기 때문에 LinearLayoutnull이 될 수 있습니까?

JavaCode :

public void onClick(View v) { 
     // TODO Auto-generated method stub 

     switch (v.getId()) { 
     case R.id.addBtn00: 
      LinearLayout hiddenLinearLayout = (LinearLayout) findViewById(R.id.hiddenLayout00); 
      if (hiddenLinearLayout == null) { 
       Toast.makeText(getBaseContext(), "hidden layout is null", Toast.LENGTH_SHORT).show(); 
      }else { 
       LinearLayout myLinearLayout = (LinearLayout) findViewById(R.id.linearLayout01); 
       View hiddenInfo = getLayoutInflater().inflate(R.id.hiddenLayout00, myLinearLayout,false); 
       myLinearLayout.addView(hiddenInfo); 
      } 
      //TextView mTv = (TextView) findViewById(R.id.textView00); 
      //mTv.setText("This is not the original Text"); 
      break; 

     case R.id.removeBtn00: 
      break; 

     default: 
      break; 
     } 

hidden.xml : 당신은 잘못된 방법으로 팽창하는

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/hiddenLayout00"> 
    <TextView 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="This is a Text View" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView00" 
     android:layout_width="wrap_content" /> 
+0

을 당신은 – user3455363

+0

확인 user3455363 @ 첫번째 레이아웃을 팽창해야, 내가 무엇을 제안 "nikis"다음 만, 여전히있는 LinearLayout hiddenLinearLayout = (있는 LinearLayout) findViewById를 (R .id.hiddenLayout00); null를 돌려 주는가? –

+0

모범 사례로 부풀게하려는 xml의 부분은 실제로 ViewStubs를 사용하는 것입니다. http://developer.android.com/reference/android/view/ViewStub.html – for3st

답변

0

, 당신은하지 레이아웃 내부의 ID로는 first 매개 변수에 레이아웃을 가리켜 야합니다 . 이 교체 :

getLayoutInflater().inflate(R.id.hiddenLayout00, myLinearLayout,false); 

를이와 함께 :

getLayoutInflater().inflate(R.layout.hidden, myLinearLayout,false); 
+0

ok 제가 정정했지만 그것은 하나입니다. 왜냐하면 "LinearLayout hiddenLinearLayout = (LinearLayout) findViewById (R.id.hiddenLayout00);" null을 반환합니다 –

+0

@Elpharaoh 처음으로'null'을 반환해야합니다 – nikis

+0

@Elpharaoh도'main.xml'의'myLinearLayout'입니까? – nikis

관련 문제