从AsyncTask的类onPostExecute方法的返回值返回值、方法、AsyncTask、onPostExecute

由网友(是有多难忘才会刻苦铭心)分享简介:好了,所以现在我有 A类包含了一些纺纱厂的值将被填充B级扩展 AsnycTask 从Web服务抓住了微调值。在B类我设法检索值,显示举杯。现在的问题是我如何通过这些微调值回A类? Ok so now I have Class A that contains some spinners that values will...

好了,所以现在我有 A类包含了一些纺纱厂的值将被填充B级扩展 AsnycTask 从Web服务抓住了微调值。在B类我设法检索值,显示举杯。现在的问题是我如何通过这些微调值回A类?

Ok so now I have Class A that contains some spinners that values will be populated by Class B that extends AsnycTask which grabs the spinner values from a web service. In class B i manage to retrieve the values, showing in a Toast. The problem now is how do I pass those spinner values back to Class A?

我试过

Can OnPostExcecute方法AsyncTask的返回值?

通过传递 A类 B类和值存储在 A类象下面这样

by passing Class A to Class B and store the value in a public variable of Class A like below

@Override
protected void onPostExecute(String result)
{
  classA.classAvariable = result;
}

不过,每当我尝试读取 classAvariable 我总是一空指针异常。 好像变从未分配的结果。 为便于阅读的目的,我需要单独的 B类而不是使用作为一个内联类。

However whenever I try to read the classAvariable i always get a NullPointer Exception. Seems like the variable was never assigned with the result. For readability purpose I needed to seperate Class B instead of using as an inline class.

任何想法,我的同胞的Java程序员?

Any ideas my fellow Java programmers?

推荐答案

我想你想读A类变量,它被设置前.. 试试使用callbacks..in回调函数传递值,并刷新你的纺纱厂做..

I guess you are trying to read class A variable before it is being set.. Try to do it using callbacks..in the callback function pass the values and refresh your spinners..

您可以创建接口,把它传递给的AsyncTask (在构造函数),然后调用方法 onPostExecute

You can create interface, pass it to AsyncTask (in constructor), and then call method in onPostExecute

例如:

您接口:

public interface OnTaskCompleted{
    void onTaskCompleted(values);
}

您活动:

public YourActivity implements OnTaskCompleted{
    //your Activity
    YourTask  task = new YourTask(this); // here is the initalization code for your asyncTask
}

和你的AsyncTask:

And your AsyncTask:

public YourTask extends AsyncTask<Object,Object,Object>{ //change Object to required type
    private OnTaskCompleted listener;

    public YourTask(OnTaskCompleted listener){
        this.listener=listener;
    }

    //required methods

    protected void onPostExecute(Object o){
        //your stuff
        listener.onTaskCompleted(values);
    }
}
阅读全文

相关推荐

最新文章