0

I have already asked a longer question that includes this problem. Trying to segmentize the bigger problem into smaller ones.

I'm trying to change the android:visibility = "gone" programmatically like this:

The XML: This is the layout for the MainAction.java and has 3 containers for Fragments. The fragment_C is not supposed to appear and it's populated when the user taps a button by using .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>

The code for the button that's supposed to add FragmentC and change the visibility attribute:

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

Can anyone tell me why do I get a NullPointerException and what in this world am I doing wrong in the code?



Additional Data:

After debugging the application I found out that my frameLayout variable is null. Why is this null I have no idea.

Community
  • 1
  • 1
sebster
  • 1,322
  • 2
  • 18
  • 29

1 Answers1

2

Found the problem!.. and a solution.

The error was that when searching for the View for which I wanted to set the visibility attribute I was using:

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

If you look at the code, view is:

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

Of course fragment_C is not located in my fragment_B layout. So frameLayout would be null. This was causing a nullPointerException when clicking the button.

The solution is to use getActivity() instead of view when searching for the view for which you want to .setVisibility().

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



Hope this helps a lost soul... like I was.

sebster
  • 1,322
  • 2
  • 18
  • 29