Home » Java » java – Call void method from another method in the same class-Exceptionshub

java – Call void method from another method in the same class-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

At first my main problem was at how to call a method from the same class, even tough I think I found a way to do this, it’s not working as I expected, and I would like to know what would be the best approach to my case.

This is the code I’m working on:

public class EscolhaAtendimento extends AppCompatActivity {

    private ViewPager mSlideViewPager;
    private LinearLayout mDotLayout;

    String TAG = "TasksSample";

    private TextView[] mDots;

    private SliderAdapter sliderAdapter;
    Dialog myDialog;

    @Override
    public void onCreate (Bundle SavedInstanceState){
        super.onCreate(SavedInstanceState);
        setContentView(R.layout.escolha_atendimento);

        mSlideViewPager = findViewById(R.id.slideViewPager);
        mDotLayout = findViewById(R.id.dotsLayout);



        sliderAdapter = new SliderAdapter(this);
        mSlideViewPager.setAdapter(sliderAdapter);
        addDotsIndicator(0);
        mSlideViewPager.addOnPageChangeListener(viewListener);
        myDialog = new Dialog(this);
    }

    public void addDotsIndicator(int position){

        mDots = new TextView[8];
        mDotLayout.removeAllViews();

        for (int  i= 0; i < mDots.length; i++){
            mDots[i] = new TextView(this);
            mDots[i].setText(Html.fromHtml("•"));
            mDots[i].setTextSize(35);
            mDots[i].setTextColor(getResources().getColor(R.color.colorTransparentWhite));

            mDotLayout.addView(mDots[i]);
        }

        if (mDots.length > 0){
            mDots[position].setTextColor(getResources().getColor(R.color.colorWhite));
        }

    }

    ViewPager.OnPageChangeListener viewListener = new ViewPager.OnPageChangeListener(){

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected (int i) {

            switch (i) {

                case 0: {

                    myDialog.show();

                }

                addDotsIndicator(i);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

   public void ShowPopup(View v) {
            TextView txtclose;
            //Button btnFollow;
            myDialog.setContentView(R.layout.pop_upfinal);
            txtclose = myDialog.findViewById(R.id.txtclose);
            txtclose.setText("X");
            //btnFollow = (Button) myDialog.findViewById(R.id.btnfollow);
            txtclose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    myDialog.dismiss();
                }
            });
            myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            myDialog.show();
    }
}

This class is an activity that on user swipe, the text and image from the buttons will change, even though their IDs will stay the same. (That’s controlled by another class, it’s working well).

Now, I wanted the image button on the activity do something different depending on which page is selected, and that’s why there is a initial switch on the onPageSelected method, inside the Page change listener. The image button in the layout has the android:onClick="ShowPopup" tag, and I guess that also complicates things for me, if I wanted it to do something different in that same activity? Also, calling it that way on the switch, every time I change pages, and go back to the first one the popup window will open, since my call is explicit there. (As I said, even tough I found a way to somehow call my method, or at least it’s result, it’s not working as I expected).

Edit

I tried then changing it like this, so that the button wouldn’t rely on the android:onClick="ShowPopup" Tag, and also wouldn’t need to call a void method directly on the switch:

Added

public ImageButton popupchoice;

And also this to onCreate method:

popupchoice = this.findViewById(R.id.imgslide1);

Inside the switch I called it like this to get the button ID:

popupchoice.setOnClickListener(image1);

And set the View.OnClickListener like this:

View.OnClickListener image1 = new View.OnClickListener() {
        public void onClick(View v) {
            TextView txtclose;
            //Button btnFollow;
            myDialog.setContentView(R.layout.pop_upfinal);
            txtclose = myDialog.findViewById(R.id.txtclose);
            txtclose.setText("X");
            //btnFollow = (Button) myDialog.findViewById(R.id.btnfollow);
            txtclose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    myDialog.dismiss();
                }
            });
            myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            myDialog.show();
        }
    };

But that returns me:

java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.view.View.setOnClickListener(android.view.View$OnClickListener)’ on a null object reference at .EscolhaAtendimento$1.onPageSelected(EscolhaAtendimento.java:81)

Line 81 is the one inside the switch with the popupchoice.setOnClickListener(image1).

This error happens on page change, when coming back to the first Page, and also the button click won’t work anymore.

How to&Answers:

I think you can use

EscolhaAtendimento.this.ShowPopup from inside your switch.