Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 这项任务对我不起作用_Android_Android Asynctask - Fatal编程技术网

Android 这项任务对我不起作用

Android 这项任务对我不起作用,android,android-asynctask,Android,Android Asynctask,我有一个IME服务类和一个长操作方法。我想在IME服务类中的asyncTask类中运行LongOperation任务 public class Myimeservice extends InputMethodService implements KeyboardView.OnKeyboardActionListene { //... //some code here.... //... public void setDictionary

我有一个IME服务类和一个长操作方法。我想在IME服务类中的asyncTask类中运行LongOperation任务

     public class Myimeservice extends InputMethodService
        implements KeyboardView.OnKeyboardActionListene {
    //...
    //some code here....
    //...

    public void setDictionary(){
          //....
    }

private class LongOperation extends AsyncTask<String, Void, String> {

        private Myimeservice parent;

        public LongOperation(Myimeservice pim){
            parent = pim;
        }
        @Override
        protected String doInBackground(String... params) {
                Myimeservice tmp = new Myimeservice();
                tmp.setDictionary();
            return "Executed";
        }

        @Override
        protected void onPostExecute(String result) {
            //app.hideLoading();
        }

        @Override
        protected void onPreExecute() {
            //app.showLoading();
        }

        @Override
        protected void onProgressUpdate(Void... values) {}
    }
公共类Myimeservice扩展了InputMethodService
实现KeyboardView.OnKeyboardActionListene{
//...
//这里有一些代码。。。。
//...
公共字典(){
//....
}
私有类LongOperation扩展了异步任务{
私有Myimeservice父级;
公共长期运营(Myimeservice pim){
父项=pim;
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
Myimeservice tmp=新的Myimeservice();
tmp.setDictionary();
返回“已执行”;
}
@凌驾
受保护的void onPostExecute(字符串结果){
//app.hideLoading();
}
@凌驾
受保护的void onPreExecute(){
//app.showLoading();
}
@凌驾
受保护的void onProgressUpdate(void…值){}
}

当我运行它时,应用程序被迫关闭。请帮助我。

我认为错误在您的
public void setDictionary()
方法中的某个地方。 我假设您正在操作一个绑定到UIThread/MainThread的变量,应用程序将崩溃,因为doInBackground位于另一个线程上

相反,让
setDictionary()
方法返回字典,并在
doInBackground()
中返回它,而不是“执行”

这将调用在UIThread/MainThread上运行的
onPostExecute(对象结果)

大概是这样的:

private class LongOperation extends AsyncTask<String, Void, Dictionary> {

@Override
protected Dictionary doInBackground(String... params) {
     Myimeservice tmp = new Myimeservice();
     Dictionary dict = tmp.setDictionary();
     return dict;
}

@Override
protected void onPostExecute(Dictionary result) {
                //do what ever you meant to do with it;
}

}

我使用Runnable而不是AsyncTask,问题就解决了

final Runnable r = new Runnable(){
        public void run(){
            setDictionary();
        }
    };
此代码位于服务的onCreate()方法中。
Tristan Richard有很多问题。

请添加LogCat输出,以便我们更好地了解错误所在。您不需要重写每个方法。只需重写您使用的方法。setDictionary()方法仅操作服务的一些变量,并调用Myimeservice的一个方法,setDictionary()不返回任何内容(void)。我想在其他线程中完成这项繁重的类工作。我想从asynctask类调用Myimeservice的setDictionary()方法。请帮助我…..Runnable可能是比。更好的选择。但由于它崩溃,您必须操作UIThread变量。而不是Myimeservice tmp=new Myimeservice();您不应该调用parent.setDictionary()吗??
final Runnable r = new Runnable(){
        public void run(){
            setDictionary();
        }
    };