0

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

Irvan Dwi Pangga
  • 322
  • 4
  • 15
  • Show your log here... that will be helpful for you. – Kuldeep Kulkarni Sep 29 '16 at 06:49
  • I'm just guessing here but I think that you're calling the `infoMethod`, which expects an activity attached. But at that point, the activity hasn't been attached yet. And it won't unless you add it to the fragment manager and it goes through the whole fragment lifecycle. Is there a reason why the `infoMethod` has to live in the SportFragment? – SunnySydeUp Sep 29 '16 at 06:59
  • @Kuldeep Kulkarni log cat add – Irvan Dwi Pangga Sep 29 '16 at 07:03
  • @SunnySydeUp in SportFragment there is recyclerview installed. I was try create button to show DialogFragment in SportFragment and call infoMethod work fine. I think if infoMethod can call in CarAdapter can work too but that not work like in button onClick at SportFragment. Any idea to show DialogFragment if imageview onClick? – Irvan Dwi Pangga Sep 29 '16 at 07:12

2 Answers2

1

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();
            }
        }
    }
}
SunnySydeUp
  • 6,680
  • 4
  • 28
  • 32
0

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?

Community
  • 1
  • 1
QVDev
  • 1,093
  • 10
  • 17
  • I was try to move infoMethod in CarAdapter but i dont found solution in this line actionbarDialog.show(getActivity().getSupportFragmentManager(), – Irvan Dwi Pangga Sep 29 '16 at 07:32
  • Now then it is obvious that getActivity().getSupportFragmentManager() is null as you creating a SportFragment without attaching this to any activity. – QVDev Sep 29 '16 at 07:39
  • Updated answer, a better structure for adapter to fragment communication is needed. Make sure you Fragment is attached to an activity, or you can create the dialog from an activity as well – QVDev Sep 29 '16 at 07:43