简体   繁体   中英

Method not being called from mainactivity on button click

I have created two activities. Activity Main has button and on click on this button im calling method of another class which is extended to AppCompActivity. The method name is mailconfig as shown below. Confidential Information has deleted from parameters.

public class ButtonActionFrontPage extends AppCompatActivity{


  protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
    }

    public void mailconfig(String message) throws EmailException {

        String username = "";
        String password = "";
        String from = "";
        String replyto = "";

        String mailto = "";
        String subject = "";

        Email email = new SimpleEmail();
        email.setSSLOnConnect(true);
        email.isStartTLSEnabled();
        email.setHostName("");
        email.setSmtpPort(26);
        email.setSubject(subject);
        email.addReplyTo(replyto);
        email.setFrom(from);
        email.setAuthenticator(new DefaultAuthenticator(username, password));
        email.setMsg(message);
        email.addTo(mailto);
        email.send();

       Toast.makeText(ButtonActionFrontPage.this,"Thanks for submitting ",Toast.LENGTH_SHORT).show();
        System.out.println("Sent");

    }
}

I a using below code to call above method.

feedbackbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    ButtonActionFrontPage buttonActionFrontPage = new ButtonActionFrontPage();
                  String  message = quickfbet.getText().toString();
                    buttonActionFrontPage.mailconfig(message);
                } catch (EmailException e) {
                    e.printStackTrace();
                }


            }
        });

What wrong in this code, why not executing.

Class would be like this

public class ButtonActionFrontPage {


    public void mailconfig(Context context,String message) throws EmailException {

        String username = "";
        String password = "";
        String from = "";
        String replyto = "";

        String mailto = "";
        String subject = "";

        Email email = new SimpleEmail();
        email.setSSLOnConnect(true);
        email.isStartTLSEnabled();
        email.setHostName("");
        email.setSmtpPort(26);
        email.setSubject(subject);
        email.addReplyTo(replyto);
        email.setFrom(from);
        email.setAuthenticator(new DefaultAuthenticator(username, password));
        email.setMsg(message);
        email.addTo(mailto);
        email.send();

       Toast.makeText(context,"Thanks for submitting ",Toast.LENGTH_SHORT).show();
        System.out.println("Sent");

    }
}

And Calling function like this

feedbackbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    ButtonActionFrontPage buttonActionFrontPage = new ButtonActionFrontPage();
                  String  message = quickfbet.getText().toString();
                    buttonActionFrontPage.mailconfig(getApplicationContext(),message);
                } catch (EmailException e) {
                    e.printStackTrace();
                }


            }
        });
public class ButtonActionFrontPage extends AppCompatActivity{

static ButtonActionFrontPage instance;


  protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        instance = this;
    }

  public static ButtonActionFrontPage getInstance() {
        return instance;
    }

@Override
    protected void onDestroy() {
        super.onDestroy();
        instance = null;
    }
}

and calling the function:

feedbackbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    ButtonActionFrontPage buttonActionFrontPage =  ButtonActionFrontPage.getInstance();                     String  message = quickfbet.getText().toString();
                    buttonActionFrontPage.mailconfig(message);
                } catch (EmailException e) {
                    e.printStackTrace();
                }
            }
        });

Java classes are different with respect to Android Activity . As Android Activity has something called life cycle .

If some functionality has to be implemented, you don't even create an Activity. Just a plain Java class is enough.

Activity can be used when there is an user interaction (infact which is not always true, but purely depends on the business logic). Inorder to initiate an Activity, Intent is used. Which will initiate the activity with memory allocation and other related features.

For your case, the initiation of button should be done in onCreate of ButtonActionFrontPage and through click listener as shown below

Button feedbackbtn;
protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        feedbackbtn=(Button)findViewById(R.id.button_ID);
        feedbackbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 new PlainJavaClass().mailconfig("msg",ButtonActionFrontPage.class);
                }
        });
    }

For the business logic just use PlainJavaClass with method and context if you have to show any Toast / Dialog / ProgressBar

class PlainJavaClass{

      public void mailconfig(String message, Context context)  {

        Log.v("TAG","mailconfig with message="+message);
        //Your logic
        Toast.makeText(context,"Thanks for submitting ",Toast.LENGTH_SHORT).show();
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM