1

I am trying to create a switch that makes a TextView appear and disappear when the switch is toggled on/off. My app keeps crashing since adding the java reference objects for the textview and switch, throwing a null pointer exception. I feel like it's something simple, any help is appreciated :)

I've updated this post since moving the code to the appropriate java file for the XML where my button is located as per Mike's recommendation. Now I am getting "Cannot resolve symbol" errors. Do I have my code in the proper methods?

public class OutputsFragment extends Fragment {


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

        //find audio switch and textview on outputs fragment
        Switch splitAudioSwitch = (Switch)findViewById(R.id.splitSwitch);
        TextView splitAlertText = (TextView)findViewById(R.id.splitText);

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_outputs, container, false);

    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // do your variables initialisations here except Views!!!
    }


    //Code for audio splitting switch on outputs fragment
        splitAudioSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                splitAlertText.setVisibility(splitAlertText.VISIBLE);
            } else {
                splitAlertText.setVisibility(splitAlertText.INVISIBLE);
            }
        }
    });
}

fragment_outputs.XML

  <Switch
        android:id="@+id/splitSwitch"
        android:layout_width="216dp"
        android:layout_height="101dp"
        android:layout_marginTop="124dp"
        android:button="@drawable/baseline_router_black_24dp"
        android:checked="false"
        android:text="@string/SplitSwitch"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.541"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_conversion_absoluteHeight="225dp"
        tools:layout_conversion_absoluteWidth="354dp" />

    <TextView
        android:id="@+id/splitText"
        android:layout_width="110dp"
        android:layout_height="51dp"
        android:text="@string/splitAlertText"
        android:textAlignment="center"
        android:textColor="@color/colorPrimary"
        android:textSize="18sp"
        android:textStyle="bold"
        android:typeface="normal"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.385"
        tools:visibility="invisible" />
  • Is your XML named `activity_main`? – Leonardo Velozo Aug 22 '19 at 23:58
  • If that is indeed where the Exception is happening, then it would seem that the `` with ID `splitSwitch` is not in the currently-loaded `activity_main` layout. As for how to prevent the error, just don't call methods on nulls. There's really no general panacea for every possible NPE. – Mike M. Aug 23 '19 at 00:05
  • Hi Leo, no my XML is named fragment_outputs.xml, I assumed I should put the core logic of my app in the MainActivity.java. Is this incorrect? I am new to posting here so I apologize for any missing information. – Dingle_Dweller Aug 24 '19 at 14:54
  • Hi Mike, I have a switch object in my xml with an id tag, is there something else that I need to do for the object to be properly loaded? – Dingle_Dweller Aug 24 '19 at 14:57
  • If the `` is in a `Fragment`s layout, you need to be handling it in the `Fragment`, not the `Activity`. The `Activity` shouldn't be trying to directly access `View`s in `Fragment`s. Indeed, in the `Activity`'s `onCreate()`, the only `View`s it's normally going to have immediate access to are those in the layout you pass to the `setContentView()` call. If I'm understanding your other comment correctly, the XML you've shown is from the `fragment_outputs` layout, which is why `findViewById(R.id.splitSwitch)` is returning null in `onCreate()`. Follow me? – Mike M. Aug 24 '19 at 17:16
  • Ok, I think I understand, the logic for each one of my fragments needs to reside in each fragments respective java file? I was planning on just putting all the code in the one file because it is a relatively simple app lol. Yes, the provided XML is for the fragment_outputs. I have put the code into the corresponding java file but I am still getting errors, should it still be in the onCreate method? Thanks so much for your help so far! – Dingle_Dweller Aug 25 '19 at 21:34
  • 1
    I've solved the problem! Added my code the the to the onCreateView method, made some changes to the "inflater" and added it to my findViewById's (view.findViewById). Thank you Mike and Leo for your help. – Dingle_Dweller Aug 25 '19 at 22:42

0 Answers0