简体   繁体   中英

call method from another activity

I want in my second class call a method from first class. Method in first class is:

 public void runLink( final Context context,final String link){
    final ProgressDialog progressDialog = new ProgressDialog(this);
    progressDialog.setMessage(LocaleController.getString("Loading", R.string.Loading));
    progressDialog.setCanceledOnTouchOutside(false);
    progressDialog.setCancelable(false);
    // other code
  }

And in second class i have this:

First f=new First();
f.runLink(getActivity(),"BmJblT5G2bQEaSI6D6Q1Mw");

but when i run it my app will be stop, and log cat reference my error for this line of first class:

 final ProgressDialog progressDialog = new ProgressDialog(this);

how can i solve it?

Replace This Line

final ProgressDialog progressDialog = new ProgressDialog(this);

with this

final ProgressDialog progressDialog = new ProgressDialog(context);

I believe you need to replace 'this' with 'context'. The reason is, that when you call 'runLink()' from the second class, the 'this' parameter refers to the first class's Context, which is not active.

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