I'm new to Android development, I'm trying to implement a music player. I tried to implement a Fragment which there, if the user would clicks on "delete", the selected index would be deleted from the ArrayList and gone from the RecyclerView.
When app reaches onClick anonymous function, it crashes at the assignment of "position" variable, indicating "Null Pointer Exception".
I attached MainActivity.java and DeleteFragment.java to this thread. Thank you!
public class MainActivity extends AppCompatActivity {
static ArrayList<SongModel> songModelArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView songsRV = findViewById(R.id.activity_main_recycler);
songsRV.setHasFixedSize(true);
songsRV.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
songModelArrayList = new ArrayList<SongModel>();
songModelArrayList.add(new SongModel("Bob Dylan", "One More Cup Of Coffee", "https://www.syntax.org.il/xtra/bob.m4a", R.drawable.bob1));
songModelArrayList.add(new SongModel("Bob Dylan", "Sara", "https://www.syntax.org.il/xtra/bob1.m4a", R.drawable.bob2));
songModelArrayList.add(new SongModel("Bob Dylan", "The Man In Me", "https://www.syntax.org.il/xtra/bob2.mp3", R.drawable.bob3));
SongAdapter songAdapter = new SongAdapter(this, songModelArrayList);
ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP|ItemTouchHelper.DOWN, ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
if(direction == ItemTouchHelper.RIGHT || direction == ItemTouchHelper.LEFT){
DeleteFragment deleteFragment = new DeleteFragment();
Bundle args = new Bundle();
args.putParcelableArrayList("arrayList", songModelArrayList);
int position = viewHolder.getAdapterPosition();
args.putInt("position", position);
deleteFragment.setArguments(args);
getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.enter_from_right, 0, 0, R.anim.exit_from_left).add(R.id.fragment_container, new DeleteFragment(), null).addToBackStack("Delete").commit();
}
}
};
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
itemTouchHelper.attachToRecyclerView(songsRV);
songsRV.setAdapter(songAdapter);
}
}
public class DeleteFragment extends Fragment {
public DeleteFragment() {
// Required empty public constructor
}
public static DeleteFragment newInstance(ArrayList<SongModel> songs){
DeleteFragment deleteFragment = new DeleteFragment();
Bundle args = new Bundle();
args.putParcelableArrayList("arrayList", songs);
deleteFragment.setArguments(args);
return deleteFragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_delete, container, false);
FloatingActionButton floatingActionButton = view.findViewById(R.id.exit_btn);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(requireActivity().getSupportFragmentManager().getBackStackEntryCount() > 0)
requireActivity().getSupportFragmentManager().popBackStack();
}
});
MaterialButton cancelBtn = view.findViewById(R.id.cancel_btn);
cancelBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(requireActivity().getSupportFragmentManager().getBackStackEntryCount() > 0)
requireActivity().getSupportFragmentManager().popBackStack();
}
});
MaterialButton deleteBtn = view.findViewById(R.id.delete_btn);
deleteBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle args = getArguments();
int position = args.getInt("position");
ArrayList<SongModel> changedSongArrayList = getArguments().getParcelableArrayList("arrayList");
changedSongArrayList.remove(position);
changedSongArrayList.notifyAll();
if(requireActivity().getSupportFragmentManager().getBackStackEntryCount() > 0)
requireActivity().getSupportFragmentManager().popBackStack();
}
});
return view;
}
}
CodePudding user response:
In line getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.enter_from_right, 0, 0, R.anim.exit_from_left).add(R.id.fragment_container, new DeleteFragment(), null).addToBackStack("Delete").commit();
you are creating new Deletefragment
instead of using already created one with arguments