Material Container Transform animation not working in Java

76
April 20, 2022, at 9:40 PM

Im trying to use Material Container Transform in Recyclerview Adapter as described by this post MaterialContainerTransform transition is not Working on Return , but after implementing this in my project im getting null pointer exception error. As everyone know Material Container Transform animation tutorials are in kotlin and im doing it in java. So if anyone know how to properly use material container transform animation in recyclerview and point out my mistakes i will be very much thankful to him.

RecyclerView Adapter Class(source)

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> implements Filterable {

    private Context context;
    private List<Astadhyayi> wordList;
    private List<Astadhyayi> filwordList;
    private DictionaryDB dictionaryDB;
    private OnclickListenerInterface listener;

    public RecyclerViewAdapter(Context context, List<Astadhyayi> itemList, DictionaryDB dictionaryDB) {
        this.context = context;
        this.wordList = itemList;
        this.dictionaryDB = dictionaryDB;
        filwordList = new ArrayList<Astadhyayi>(itemList);
    }
    public void setClickListener(OnclickListenerInterface onclickListener) {
        this.listener = onclickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);     
        View view = inflater.inflate(R.layout.wordslist_two_rv, parent, false);          
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        Astadhyayi word = wordList.get(position);
        holder.rsutras.setText(word.mSutras);

        // Animation******
        
        holder.rsutras.setTransitionName(word.getMSutras());

        if (word.mStatus != null && word.mStatus.equals(DictionaryDB.BOOKMARKED)) {
            holder.bookmark.setImageResource(R.drawable.cards_heart);
        } else {         
            holder.bookmark.setImageDrawable(null);
        }
    }
    @Override
    public Filter getFilter() {
        return Searched_Filter;
    }
    private Filter Searched_Filter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            List<Astadhyayi> filteredList = new ArrayList<>();
            if (constraint == null || constraint.length() == 0) {
                filteredList.addAll(filwordList);
            } else {
                String filterPattern = constraint.toString().toLowerCase().trim();
                for (Astadhyayi item : filwordList) {
                    if (item.getMSutras().toLowerCase().contains(filterPattern)) {
                        filteredList.add(item);
                    }
                }
            }
            FilterResults results = new FilterResults();
            results.values = filteredList;
            return results;
        }
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            wordList.clear();
            wordList.addAll((ArrayList<Astadhyayi>)results.values);
            notifyDataSetChanged();
        }
    };

    @Override
    public int getItemCount() {      
        return wordList.size();    
    }


    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView rsutras;
        private ImageView bookmark;
        public ViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            rsutras =  itemView.findViewById(R.id.tvSansTerm);          
            bookmark = itemView.findViewById(R.id.bookmarkBtn);      
        }
        /*
         @Override
         public void onClick(View view) {
         if(listener!=null) listener.onItemClicked(getAbsoluteAdapterPosition(), rsutras);
         }
         */

        @Override
        public void onClick(View view) {

            int position = this.getAbsoluteAdapterPosition();   
            Astadhyayi word = wordList.get(position);
            //*************** Bundle  **************\\
            Bundle bundle = new Bundle();
            bundle.putParcelable("allData", word);
            bundle.putString("transition_name", word.getMSutras());
            Fragment detailsFragment = new TermThreeDetailsFragment();
            detailsFragment.setSharedElementEnterTransition(new MaterialContainerTransform());
            detailsFragment.setArguments(bundle);
            FragmentTransaction fT =((AppCompatActivity)context).getSupportFragmentManager().beginTransaction();
            fT.setReorderingAllowed(true);
            fT.addSharedElement(rsutras, rsutras.getTransitionName()); // Shared element!
            fT.replace(R.id.fragment_container, detailsFragment);
            fT.addToBackStack(null);
            fT.commit();

        }

    }

} 

RecyclerView Fragment Class(source)

public class ShowtermsTwoFragment extends Fragment {
    private RecyclerView rv;
    private EditText tV;
    private Toolbar toolbar;
    private LinearLayoutManager layoutManager;
    private static DictionaryDB db;
    private RecyclerViewAdapter rvAdapter;
    private SearchView searchView = null;
    private SearchView.OnQueryTextListener queryTextListener;
    private List<Astadhyayi> wordsList = new ArrayList<>();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setExitTransition(new MaterialElevationScale(false));
        setReenterTransition(new MaterialElevationScale(true));
    }

    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_terms_recyclerview, container, false);          
        return view;     
    }
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        toolbar = ((Toolbar) view.findViewById(R.id.toolbar_rv));
        rv = view.findViewById(R.id.rvTerms);
        tV = view.findViewById(R.id.tVinput);
        //*********** TOOLBAR
        AppCompatActivity activity = (AppCompatActivity) getActivity();
        activity.setSupportActionBar(toolbar);
        setHasOptionsMenu(true);   
        // new Get_DbData().execute();

        databaseCheck();
        recyclerCheck1();
        postponeEnterTransition();
        ((ViewGroup) view.getParent()).getViewTreeObserver()
            .addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                public boolean onPreDraw() { 
                    startPostponedEnterTransition();
                    return true;
                }
            });
        
        }
    private void databaseCheck() {
        String sanskritWord = getActivity().getIntent().getStringExtra("sanskritWord");
        //*********** @@@ DATABASE OPENING @@@ ***********\\
        db = DictionaryDB.getInstance(this.getActivity());
        wordsList = db.getWords((sanskritWord));
    }

    private void recyclerCheck1() {
        layoutManager = new LinearLayoutManager(getContext());
        rv.setLayoutManager(layoutManager);
        rv.setHasFixedSize(true);

        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(rv.getContext(), layoutManager.getOrientation());
        rv.addItemDecoration(dividerItemDecoration);
        rvAdapter = new RecyclerViewAdapter(getContext(), wordsList, db);
        rv.setAdapter(rvAdapter);       
     /////**********   rvAdapter.setClickListener(this);      
        rvAdapter.notifyDataSetChanged();
    }
}

Details Fragment Class(destination)

public class TermThreeDetailsFragment extends Fragment {

    private TextView sutraTV,vrittiTV,padaTv,adhyayTV,sutraIdTV ;
    private CoordinatorLayout coordinatorLayout;
    private FloatingActionButton fabBtn;
    private static DictionaryDB db;
    private Astadhyayi word;
    private BottomAppBar bottomAppBar;
    
    private TextToSpeech pronunciation;
    private AudioManager audio;
    private String ttsID , trans;
    
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MaterialContainerTransform transform = new MaterialContainerTransform();
        transform.setScrimColor(Color.TRANSPARENT);
        setSharedElementEnterTransition(transform);
        setSharedElementReturnTransition(transform);
    }
    
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.show_three_details, container, false); 
        
        view.findViewById(R.id.tvSutra).setTransitionName(word.getMSutras());
        return view;
    }
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
            
        sutraTV = view.findViewById(R.id.tvSutra);
        adhyayTV = view.findViewById(R.id.tVAdhyay);
        padaTv = view.findViewById(R.id.tVPada);
        sutraIdTV = view.findViewById(R.id.tVSutraId);
        vrittiTV = view.findViewById(R.id.tvVritti);
        fabBtn = view.findViewById(R.id.favFAB);
        coordinatorLayout = view.findViewById(R.id.parentCordlayout);
        bottomAppBar = view.findViewById(R.id.bottom_app_bar);   
   
        
        
        bundleCheckWithDb();  
        WordCheck();
        
        
    }
    
    
    
    
    private void bundleCheckWithDb() {
        
                            //***********  Geting Parcelable Data 
        word = new Astadhyayi(Parcel.obtain());
        Bundle bundle = this.getArguments();
        
        
        if (bundle != null) {
            word = bundle.getParcelable("allData"); 
            trans = bundle.getString("transition_name");
            
            adhyayTV.setText(word.getMAdhyay());
            padaTv.setText(word.getMPada());
            sutraIdTV.setText(word.getMSutra_id()); 
            sutraTV.setText(word.getMSutras());
            vrittiTV.setText(word.getMVritti()); 
        }
                           //*********** DATABASE OPENING
        db = DictionaryDB.getInstance(this.getActivity()); 
    }
    
    
    
    
    private void WordCheck() {
        if (word.mStatus != null && word.mStatus.equals(DictionaryDB.BOOKMARKED)) {
            fabBtn.setImageResource(R.drawable.cards_heart);
        } else {
            fabBtn.setImageResource(R.drawable.cards_heart_white);
        }
        fabBtn.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    bookMarkWord(word, fabBtn);
                }
            });
    }    
}       

Error im getting

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.sanskrit.mysearcher.models.Astadhyayi.getMSutras()' on a null object reference at com.sanskrit.mysearcher.fragments.TermThreeDetailsFragment.onCreateView(TermThreeDetailsFragment.java:69)

Answer 1

From the current given information, I think you should do something like this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    word = new Astadhyayi(Parcel.obtain());
    Bundle bundle = this.getArguments();
    if (bundle != null) {
        word = bundle.getParcelable("allData");
    }
    View view = inflater.inflate(R.layout.show_three_details, container, false); 
    view.findViewById(R.id.tvSutra).setTransitionName(word.getMSutras());
    return view;
}

Again, I am not sure what getMSutras() actually returns and if it's right to use that here.

Rent Charter Buses Company
READ ALSO
Store Image in Room Database using typeconverter to convert bitmap into bytearray. What to write in last parameter of the model class

Store Image in Room Database using typeconverter to convert bitmap into bytearray. What to write in last parameter of the model class

I don't know how to pass the bitmap as byteArray in the code belowI have added the parameter in the note model class and it is of type byte array

119
google reset new key certificate for me but certificate not working

google reset new key certificate for me but certificate not working

i am reset my certificate APK google play console

81
How to update and delete firebase data in android java?

How to update and delete firebase data in android java?

I am getting nothing in response while updating or deleting the data this line but the inserting and retriving works fine and please do answer i am new to android development

112
Flutter: the action button in flutter callkit incoming package didn&#39;t work

Flutter: the action button in flutter callkit incoming package didn't work

the action button in flutter callkit incoming package didn't workI tried a lot but in the background it doesnot work

84