Reputation: 322
I have ActionBarDialog (DialogFragment), CarAdapter (RecycleView) and SportFragment. I am create method (infoMethod) in SportFragment to show DialogFragment and work fine. But if i want to call that infoMethod in CarAdapter not work (java.lang.NullPointerException)
infoMethod in SportFragment
public void infoMethod(){
Bundle args = new Bundle();
args.putString("title", "Dialog with Action Bar");
ActionBarDialog actionbarDialog = new ActionBarDialog();
actionbarDialog.setArguments(args);
actionbarDialog.show(getActivity().getSupportFragmentManager(),
"action_bar_frag");
}
I was try to call infoMethod from CarAdapter
public MyViewHolder(View view) {
super(view);
image= (ImageView) view.findViewById(R.id.imageViewlist);
image.setOnClickListener(this);
}
@Override
public void onClick(View v) {
SportFragment infoFrag = new SportFragment();
if (v.getId() == image.getId()){
v.startAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.imagelist));
infoFrag.infoMethod(); //call infoMethod not work
Toast.makeText(v.getContext(), "ITEM PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(v.getContext(), "ROW PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
}
}
Logcat
09-29 13:30:09.292 18661-18661/com.paijostudio.hitungmasa E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.paijostudio.hitungmasa.fragment.SportFragment.infoMethod(SportFragment.java:210)
at com.paijostudio.hitungmasa.adapter.CarAdapter$MyViewHolder.onClick(CarAdapter.java:73)
at android.view.View.performClick(View.java:4204)
at android.view.View$PerformClick.run(View.java:17355)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Any solution to show DialogFragment ?thanks
Upvotes: 0
Views: 342
Reputation: 6790
Since you're trying to create the dialog in two separate areas of the app, I suggest moving it out to it's own class
public class DialogHelper {
public static void infoMethod(FragmentManager fragmentManager) {
Bundle args = new Bundle();
args.putString("title", "Dialog with Action Bar");
ActionBarDialog actionbarDialog = new ActionBarDialog();
actionbarDialog.setArguments(args);
actionbarDialog.show(fragmentManager, "action_bar_frag");
}
}
Then in SportFragment
replace your call with DialogHelper.infoMethod(getActivity().getSupportFragmentManager());
As for your CarAdapter
, you'll need to take in a FragmentManager
.
recyclerView.setAdapter(new CarAdapter(getActivity().getSupportFragmentManager()))
...
public class CarAdapter extends RecyclerView.Adapter<MyViewHolder> {
private FragmentManager fragmentManager;
public CarAdapter(FragmentManager fragmentManager) {
this.fragmentManager = fragmentManager;
}
public class MyViewHolder {
...
@Override
public void onClick(View v) {
if (v.getId() == image.getId()){
v.startAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.imagelist));
DialogHelper.infoMethod(fragmentManager);
Toast.makeText(v.getContext(), "ITEM PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(v.getContext(), "ROW PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
}
}
}
}
Upvotes: 1
Reputation: 1093
Seem
infoFrag.infoMethod(); //call infoMethod not work
Is indeed not working, as @SunnySydeUp might be right. Maybe try to add the line numbers in front as
java.lang.NullPointerException at com.paijostudio.hitungmasa.fragment.SportFragment.infoMethod(SportFragment.java:210)
Is pretty specific about it. You trying to do something on something that is null. And seeing from what is happening here, it is most likely the SportFragment.
My suggestion is to break things down for debug sake. So I would move the method
public void infoMethod(){
Outside the SportFragment. And move it to the class
CarAdapter
Meaning that SportFragment can be null. And just check if this works. If so you, and that is what I expect, you have to fix the issue in creating the SportFragment. And once that is fixed you can move back the method. Have a look for getting, or creating Fragments in the Android docs.
https://developer.android.com/guide/components/fragments.html
UPDATE: As an addition and receiving more info. As the Fragment has no activity attached as the SportFragment is created incorrect.
Have a look at the following answer to communicate correctly to your fragment How to create interface between Fragment and adapter?
Upvotes: 0