简体   繁体   English

在静态方法上获取异步任务计数始终返回0

[英]Get Count of Async Task on Static Method always return 0

Mans, im here around 2 hours in one thing it seems very simple but never return the numbers of objects. 勒芒,在这里大约2个小时是一件很简单的事情,但是从不返回对象的数量。

I will explain whit code: 我将解释白色代码:

DownloadFileAsyncTask.java (is my Async Task) DownloadFileAsyncTask.java(是我的异步任务)

in OnPostExecute i make this: 在OnPostExecute我做到这一点:

if(customAdapter != null){
    Log.i("solteiroApp","object count is "+customAdapter.getCount());
    ResultActivity.rec_count  = customAdapter.getCount(); // HERE I PASS COUNT
}

ResultActivity.java ResultActivity.java

public class ResultActivity extends Activity {

    public static int rec_count;
...

The problem is: When i get the ResultActivity.rec_count in onCreate always return 0, but i make the log how i show before: 问题是:当我在onCreate获取ResultActivity.rec_count ,始终返回0,但是我将日志显示为以前的ResultActivity.rec_count

Log.i("solteiroApp","object count is "+customAdapter.getCount());

and this return the count correctly. 这将正确返回计数。

I dont know because this, if someone have a sugestio to get this count please say me, im hours here try return this count to appear in the activity but nothing return the value in time of i execute, if i use again the AsyncTask return the previous value, please someone help me. 我不知道这是因为,如果有人有Sugestio来获取此计数,请告诉我,我在这里的小时数尝试返回此计数以显示在活动中,但如果我再次使用AsyncTask返回,则没有时间返回我执行的值以前的值,请有人帮我。

1, is your getting rec_count in onCreate before OnPostExecute be invoked? 1,是否在调用OnPostExecute之前在onCreate中获得rec_count? check your code 2, if not, add volatile before rec_count: public static volatile int rec_count, please have a try, hope can help you. 检查您的代码2,如果没有,在rec_count之前添加volatile:public static volatile int rec_count,请尝试一下,希望对您有所帮助。

You can't do what you are trying to do. 您无法做您想做的事。 The Android OS will destroy and recreate static classes and their static variables when it (re)creates the Context they are to run in. I also learned (the hard way) that singleton classes can't be used to save data from one Activity to another. 当Android OS(重新)创建要在其中运行的Context时,Android操作系统将销毁并重新创建静态类及其静态变量。我还(很难地)了解到,单例类不能用于将一个Activity中的数据保存到另一个。

What you need to do is either pass that data to the new Activity in its Intent Bundle, or save it in a SharedPreference. 您需要做的是将数据传递到其“意图捆绑包”中的新活动,或者将其保存在SharedPreference中。 Using a callback interface for that is a bad idea, because you want the action to move forward to your next Activity and (if needed) allow the OS to destroy the previous Activity. 为此使用回调接口是个坏主意,因为您希望操作前进到下一个活动,并且(如果需要)允许操作系统销毁上一个活动。

When creating the Intent for the next Activity, you use the Intent.putExtra(...) methods to add data to pass to the new Activity. 为下一个活动创建Intent时,可以使用Intent.putExtra(...)方法添加数据以传递给新的Activity。 In the new Activity, you use getIntent().get*Datatype*Extra(..) methods to retrieve the data. 在新的Activity中,使用getIntent()。get * Datatype * Extra(..)方法检索数据。

I got it ! 我知道了 !

In my AsyncTask i pass how parameter the TextView and declare in AsyncTask how WeakReference, this: 在我的AsyncTask中,我传递参数TextView的方式,并在AsyncTask中声明WeakReference的方式,这是:

public class DownloadFileAsyncTask extends AsyncTask<String,Void,String> {
..
    private WeakReference list=null,count_text=null; // Views i update in my AsyncTask
..

constructor: 构造函数:

public DownloadFileAsyncTask(View v,Context contextt,View txt) {
    list = new WeakReference(v);
    count_text = new WeakReference(txt);
    this.context = contextt;
}

and onPostExecute: 和onPostExecute:

   ListView l = (ListView) list.get();
   TextView t = (TextView) count_text.get();

   t.setText(customAdapter.getCount()+" objects found");

Thanks all by the help, sometime is good leave sometime of the error and back, new ideas come back hehe, thanks all. 谢谢大家的帮助,有时是个好消息,有时会出错,然后回来,新的想法回来了,谢谢。

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

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