Search code examples
javaandroidonclicklistener

The performClick () method returns an error


I have 2 classes that are covered by fragment. home.java hometwo.java

Clicking the button named btn of my class named home.java. I want my class named hometwo.java to be clicked automatically on the button named btnctwo.

home.java

public class home extends Fragment {
    Button btn;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.home, container, false);



        btn =  rootView.findViewById(R.id.btnc);
btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        hometwo hometwox = new hometwo();

        hometwox.btnctwo
                .post(new Runnable(){
                    @Override
                    public void run() {
                        hometwox.btnctwo.performClick();

                    }
                });
    }
});

        return rootView;



    }
}

hometwo.java

public class hometwo extends Fragment {
    Button btnctwo;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.home, container, false);



        btnctwo =  rootView.findViewById(R.id.btnctwo);






btnctwo.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.e("testler","testlertwo");

    }
});

        return rootView;



    }



}

logcat

2020-05-18 11:17:54.907 11290-11290/com.sahnartsappone.sahnartsapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.sahnartsappone.sahnartsapp, PID: 11290
    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.widget.Button.post(java.lang.Runnable)' on a null object reference
        at com.sahnartsappone.sahnartsapp.home$1.onClick(home.java:30)
        at android.view.View.performClick(View.java:7125)
        at android.view.View.performClickInternal(View.java:7102)
        at android.view.View.access$3500(View.java:801)
        at android.view.View$PerformClick.run(View.java:27336)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

Why does the performClick () method not work? Where am I making mistakes?


Solution

  • The problem is here:

    hometwo hometwox = new hometwo();
    
            hometwox.btnctwo
                    .post...
    

    If you only create the fragment it will not reach the onCreateView method and therefore it won't inject the btnctwo button from hometwox with the button instance from the view (as there won't be any view). So basically you will call the post method on a null reference.

    So the solution would be to render the fragment using something like this:

     FragmentTransaction transaction = this.fragmentManager.beginTransaction();
            transaction.replace(R.id.frame_container, fragment);
            transaction.addToBackStack(null);
            transaction.commit();