2013-03-20 1 views
0

나는이 problem을 포함하는 더 긴 질문을 이미 요청했습니다. 큰 문제를 작은 문제로 세분화하려고합니다. 뷰의 가시성 특성을 프로그래밍 방식으로 변경

나는 안드로이드를 변경하려고 해요 : 가시성 = 프로그램과 같이 "사라"

XML을 : 이것은 MainAction.java의 레이아웃과 조각 3 개 컨테이너가. fragment_C는 표시되지 않아야하며 사용자가 .replace (R.id.fragment_C, new FragmentC())를 사용하여 버튼을 탭하면 채워집니다.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" 
    android:paddingLeft="0dp" 
    android:paddingRight="0dp" > 

    <LinearLayout 
     android:id="@+id/fragments_container" 
     android:layout_width="fill_parent" 
     android:layout_height="200dp" 
     android:baselineAligned="false" > 

     <FrameLayout 
      android:id="@+id/fragment_A" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="0.5" 
      android:background="#CCCCCC" > 
     </FrameLayout> 

     <FrameLayout 
      android:id="@id/fragment_B" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="0.5" 
      android:background="#B4B4B4" 
      > 
     </FrameLayout> 
    </LinearLayout> 

    <FrameLayout 
     android:id="@+id/fragment_C" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_below="@id/fragment_container" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="0dp" 
     android:background="#A3A3A3" 
     android:visibility="gone" > 
    </FrameLayout> 

</RelativeLayout> 

FragmentC을 추가하고 가시성 속성을 변경 해야하는 버튼의 코드 :이 세계에서 NullPointerException이 무엇을 얻을 않는 이유

public class FragmentB extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     View view = inflater.inflate(R.layout.fragmentB, container, false); 

     ListView listView = (ListView) view.findViewById(R.id.listView1); 
     Button button = (Button) view.findViewById(R.id.button); 

     String[] machines = new String[] { "MachineId-001", "MachineId-002", "MachineId-003", "MachineId-004", "MachineId-005", "MachineId-006", "MachineId-007", "MachineId-008"}; 

     listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
     listView.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.select_dialog_multichoice, machines)); 
     final FrameLayout frameLayout = (FrameLayout) view.findViewById(R.id.fragment_C); 

     button.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Activity activity = getActivity(); 

       if (activity != null) { 
        getFragmentManager().beginTransaction().replace(R.id.fragment_C, new FragmentC()).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).addToBackStack(null).commit(); 
       // When using setVisibility the app crashes with a 
       // java.lang.NullPointerException 
        frameLayout.setVisibility(View.VISIBLE); 
       } 
      } 

     }); 

     return view; 
    } 
} 

사람이 말해 줄 수 오전 나는 일을 코드가 잘못 되었나요?



추가 데이터 :

FrameLayout이 변수가 것을 발견 응용 프로그램을 디버깅 후. 이게 왜 널 모르는지 모르겠어.

+0

이 포럼의 사용자는 추가 플래그를 추가 할 것을 제안했습니다. 하지만 그건 효과가 없었습니다. – sebster

답변

2

발견 된 ! 해결책.

final FrameLayout frameLayout = (FrameLayout) view.findViewById(R.id.fragment_C); 

당신이 코드를 보면, 뷰는 다음과 같습니다 :

View view = inflater.inflate(R.layout.fragmentB, container, false); 
내가 가시성을 설정하고 싶어하는보기를 검색 할 때 내가 사용 된 속성 것을

이었다 오류

물론 fragments_C은 내 fragment_B 레이아웃에 없습니다. 따라서 frameLayoutnull이됩니다. 이 버튼을 클릭하면 nullPointerException이 발생합니다.

해결 방법은 .setVisibility()를 사용하려는보기를 검색 할 때 view 대신 getActivity()를 사용하는 것입니다. 내가처럼

final FrameLayout frameLayout = (FrameLayout) getActivity().findViewById(R.id.fragment_C); 



희망이 ... 잃어버린 영혼을하는 데 도움이됩니다.

관련 문제