I have two buttons and an EditText in a fragment. When I click the addMusicButton, I want it to disappear and make newMusic(my EditText) and confirmAddButton appear. If I only change the visibility of the addMusicButton in the onClick method, it works, but if I try to change the visibility of newMusic or confirmAddButton in the onClick method, it gives me this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setVisibility(int)' on a null object reference
and my app crashes. I don't understand why it's saying my button is null because I initialized it right before I changed the visibility in the onClick method(the same thing happens if I try to change the visibility of the EditText). I am able to change the visibility of both my buttons and my EditText in onCreate, just not in onClick. I thought it might be because newMusic and confirmAddButton were in a different linear layout from addMusicButton, so I tried moving them all to one layout and it still gave me the same error.
This is my XML file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/addMusicButton"
android:layout_width="match_parent"
android:layout_height="109dp"
android:layout_weight="1"
android:text="@string/add_music" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="109dp"
android:layout_weight="1"
android:orientation="horizontal">
<EditText
android:id="@+id/newMusic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="330dp"
android:autofillHints=""
android:ems="10"
android:gravity="left|center_vertical"
android:inputType="textAutoComplete" />
<Button
android:id="@+id/confirmAddButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center|center_vertical"
android:text="@string/add" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="585dp"
android:layout_weight="1">
<LinearLayout
android:id="@+id/musicLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
</LinearLayout>
And my fragment code
public class MusicFragment extends Fragment implements View.OnClickListener {
private MusicViewModel musicViewModel;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
musicViewModel = ViewModelProviders.of(this).get(MusicViewModel.class);
View root = inflater.inflate(R.layout.fragment_music, container, false);
Button addMusicButton = root.findViewById(R.id.addMusicButton);
EditText newMusic = root.findViewById(R.id.newMusic);
Button confirmAddButton = root.findViewById(R.id.confirmAddButton);
newMusic.setVisibility(View.GONE);
confirmAddButton.setVisibility(View.GONE);
addMusicButton.setOnClickListener(this);
confirmAddButton.setOnClickListener(this);
return root;
}
@Override
public void onClick(View view) {
Button addMusicButton = view.findViewById(R.id.addMusicButton);
Button confirmAddButton = view.findViewById(R.id.confirmAddButton);
EditText newMusic = view.findViewById(R.id.newMusic);
switch (view.getId()) {
case R.id.addMusicButton:
confirmAddButton.setVisibility(View.VISIBLE);
newMusic.setVisibility(View.VISIBLE);
addMusicButton.setVisibility(View.GONE);
break;
case R.id.confirmAddButton:
(...)
break;
}
}
}