简体   繁体   English

尝试从asynctask调用其他活动的方法

[英]trying to call an other activity's method from asynctask

I have an async task that downloads a file from the web, it works fine when i preset the URL. 我有一个异步任务,可以从Web下载文件,当我预设URL时,它可以正常工作。

URL url = new URL("http://www.mylocalweather.org.uk/ribblehead/clientraw.txt")

However when i try and use a method defined in an other activity to return a URL it fails. 但是,当我尝试使用其他活动中定义的方法返回URL时,它将失败。

URL url = null;
url = splash.gps();

The method has worked fine in the past and when called from other activities, and the download function works fine when the URL is hardcoded into the async therefore it much be a problem with calling the method from an Async task, whats the work around for this? 该方法在过去以及从其他活动中调用时都可以正常工作,并且当URL硬编码到异步中时,下载功能也可以正常工作,因此从异步任务中调用该方法很成问题,该如何解决? ?

this is the error that it comes up with 这是它带来的错误

E/AndroidRuntime(16426): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()

Any help would be appreciated 任何帮助,将不胜感激

many thanks 非常感谢

as your Log says, you can't use a method of an Activity when that Activity was not created. 作为你的日志说,你不能使用的方法Activity ,当Activity未创建。 Just use some of your classes and define your method there, passing it the parameters you need. 只需使用一些类并在其中定义方法,然后将所需的参数传递给它即可。

That other Activity doesn't exist (as the error notes, onCreate hasn't been called) and therefore can't use whatever system service you're using to get the URL. 该其他Activity不存在(如错误说明所述,尚未调用onCreate ),因此无法使用您用于获取URL的任何系统服务。

Either have the AsyncTask retrieve the URL itself, or pass the information from the splash activity to the activity hosting the AsyncTask. 要么让AsyncTask检索URL本身,要么将信息从启动活动传递到托管AsyncTask的活动。

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

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