在 android 上调用调用侦听器的方法会引发异常"只有创建视图层次结构的原始线程可以"这是我的代码:
public class MyClassAsync extends AsyncTask<String, String, ProfileInfo> {
private Context mContext;
private IGetTelegramUserInfoAsync mListener;
protected ProfileInfo doInBackground(String... params) {
// .... some code
mListener.GetResult(profileInfo)
// ...
helperMethod();
}
private void helperMethod(){
mListener.GetResult(profileInfo); //exception place
}
你可以通过调用侦听器来做到这一点定义一个侦听器,如下所示:
public interface YourListener {
public void gotResultOfYourAsync();
}
以这种方式调用它或在活动或片段中实现:
public class Something {
private YourListener yourListener;
public void setTheListener(YourListener listener) {
yourListener = listener;
}
在异步任务中,你可以调用这个
yourListener.gotResultOfYourAsync();
这有点复杂,但你可以问你的问题