I tried to transfer data from View Fragment which contains RecycleView to Display Fragment... but I'm getting null Exception Error every time while using bundle
Here is my code.
Inside Adapter
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
StringIntString ping=arrayList.get(position);
holder.name.setText(ping.getmString());
holder.desc.setText(ping.getnString());
holder.num.setText(Integer.toString(ping.getmInt()));
holder.layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bundle bundle=new Bundle();
bundle.putString("name", ping.getmString());
bundle.putString("desc", ping.getnString());
bundle.putString("num",Integer.toString(ping.getmInt()));
DisplayFrag frag=new DisplayFrag();
frag.setArguments(bundle);
((FragmentActivity)view.getContext()).getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.fade_in,R.anim.slide_out).replace(R.id.fragview,DisplayFrag.class,null).commit();
}
});
}
Inside Display view
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v=inflater.inflate(R.layout.fragment_viewfrag2, container, false);
name=v.findViewById(R.id.box1);
desc=v.findViewById(R.id.box2);
num=v.findViewById(R.id.box3);
Bundle bundle=this.getArguments();
// if(bundle != null) {
name.setText(bundle.getString("name"));
desc.setText(bundle.getString("desc"));
num.setText(bundle.getInt("num"));
// }
return v;
}
Log Message
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.myapplication, PID: 31856 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference at com.example.myapplication.DisplayFrag.onCreateView(DisplayFrag.java:31) at androidx.fragment.app.Fragment.performCreateView(Fragment.java:3104) at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:524) at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261) at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1899) at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1817) at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1760) at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:547) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:201) at android.os.Looper.loop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7839) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
I'm expecting to transfer data from View fragment to Display fragmnet
In your constants class define new (you can make it Static) variables like below
public class MyUtils
{
public static MutableLiveData<String> nameMutableLiveDataString = new MutableLiveData<>();
public static MutableLiveData<String> descMutableLiveDataString = new MutableLiveData<>();
public static MutableLiveData<Integer> numMutableLiveDataString = new MutableLiveData<>();
and your onBindViewHolder
will look something like this
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
StringIntString ping=arrayList.get(position);
holder.name.setText(ping.getmString());
holder.desc.setText(ping.getnString());
holder.num.setText(Integer.toString(ping.getmInt()));
holder.layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MyUtils.nameMutableLiveDataString.postValue(ping.getmString());
MyUtils.descMutableLiveDataString.postValue(ping.getnString());
MyUtils.numMutableLiveDataString.postValue(ping.getmInt());
// Bundle bundle=new Bundle();
// bundle.putString("name", ping.getmString());
// bundle.putString("desc", ping.getnString());
// bundle.putString("num",Integer.toString(ping.getmInt()));
DisplayFrag frag=new DisplayFrag();
// frag.setArguments(bundle);
((FragmentActivity)view.getContext()).getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.fade_in,R.anim.slide_out).replace(R.id.fragview,DisplayFrag.class,null).commit();
}
});
}
and your view class something like this
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v=inflater.inflate(R.layout.fragment_viewfrag2, container, false);
name=v.findViewById(R.id.box1);
desc=v.findViewById(R.id.box2);
num=v.findViewById(R.id.box3);
Bundle bundle=this.getArguments();
// if(bundle != null) {
MyUtils.nameMutableLiveDataString.observe((LifecycleOwner) this, new Observer<String>()
{
@Override
public void onChanged(String s)
{
name.setText(s);
}
});
MyUtils.descMutableLiveDataString.observe((LifecycleOwner) this, new Observer<String>()
{
@Override
public void onChanged(String s)
{
desc.setText(s);
}
});
MyUtils.numMutableLiveDataString.observe((LifecycleOwner) this, new Observer<Integer>()
{
@Override
public void onChanged(Integer s)
{
num.setText(s.toString());
}
});
// }
return v;
}