2013-03-20 158 views
0

我已經提出了一個更長的問題,其中包括此problem。試圖將更大的問題分割成更小的問題。以編程方式更改視圖的可見性屬性

我試圖改變機器人:知名度=「水漲船高」程序是這樣的:

的XML: 這是MainAction.java佈局和有3個容器碎片。當用戶使用.replace(R.id.fragment_C,new FragmentC())輕擊按鈕時,不應該出現fragment_C,並且它被填充。

<?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和更改可見性屬性爲按鈕的代碼:

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; 
    } 
} 

誰能告訴我爲什麼會在這個世界上一個NullPointerException和我在做什麼代碼中出現錯誤?



其他數據:

調試我發現我的的FrameLayout變量是應用程序之後。爲什麼這是空的,我不知道。

+0

此論壇上的用戶建議添加額外的標誌。但是那也行不通。 – sebster 2013-03-20 12:47:21

回答

2

Found found the problem!and a solution

錯誤是針對我想成立能見度查看搜索時的屬性我使用:

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

如果你在看代碼,觀點是:

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

當然fragment_C不位於我的fragment_B佈局。所以frameLayout將是null。點擊按鈕時會導致nullPointerException。

解決方案是在搜索要爲其設置.setVisibility()的視圖時使用getActivity()而不是視圖。

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



希望這有助於丟了魂......像我了。

相關問題