简体   繁体   English

从另一个活动调用方法

[英]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:但是当我运行它时,我的应用程序将停止,并记录 cat 引用我这一行第一类的错误:

 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.原因是,当您从第二个类调用 'runLink()' 时,'this' 参数指的是第一个类的 Context,它不是活动的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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