gpt4 book ai didi

java - AsyncTask 回调不调用

转载 作者:行者123 更新时间:2023-11-29 21:32:33 25 4
gpt4 key购买 nike

我在从单独类中的 asyncTask 获取结果时遇到问题。我已经从这里的类似问题的答案中得到了关注,但我看不出哪里出了问题。

我的 AsyncTask 在一个单独的类中以便于调用,我需要能够通知 asyntask 已经完成,然后开始下一个 Activity 。

我欢迎任何帮助,因为我不太确定哪里出了问题。

 public class StartScreen extends Activity{

ProgressDialog pd;
CountDownTimer waitTimer;
public static final String APP_PREFERENCES = "AppPrefs";
SharedPreferences settings;
SharedPreferences.Editor prefEditor;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_screen);

settings = getSharedPreferences(APP_PREFERENCES, MODE_PRIVATE);

// getPreferences();
// prefEditor = settings.edit();

waitTimer = new CountDownTimer(2000, 300) {

public void onTick(long millisUntilFinished) {
//called every 300 milliseconds, which could be used to
//send messages or some other action
}
public void onFinish() {
//After 2000 milliseconds (2 sec) finish current
//if you would like to execute something when time finishes
pd = ProgressDialog.show(StartScreen.this,"Title","Detail text",true,false,null);
getPreferences();
}
}.start();
}

private void getPreferences() {

String UserName = settings.getString("UserName", null);

if (UserName != null) {
// the key does not exist
Intent intent=new Intent(StartScreen.this,InitialPreferences.class);
startActivity(intent);

} else{
//if (UserName.equals(UserName)){
// handle the value
dataTask();
//pd.dismiss();
}
}
private void dataTask() {
// TODO Auto-generated method stub
new DATATask(this).execute(new FragmentCallback(){

@Override
public void onTaskDone() {
startMainAct();

}
});
}
private void startMainAct() {
Intent intent=new Intent(StartScreen.this,MainActivity.class);
startActivity(intent);
}

public interface FragmentCallback {
public void onTaskDone();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.start_screen, menu);
return true;
}
}

异步任务:

 public class DATATask extends AsyncTask<Void, Void, ArrayList<String>> {

private FragmentCallback mFragmentCallback;
public void execute(FragmentCallback fragmentCallback) {
mFragmentCallback = fragmentCallback;
}

ArrayList<String> arr_data=new ArrayList<String>();

private Context context;

public DATATask(Context context)
{
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();

}
@Override
protected ArrayList<String> doInBackground(Void... params) {

Document docVts, docTide;
String shippingList, tideTimes;


try {
docVts = Jsoup.connect("https://vts.mhpa.co.uk/main_movelistb.asp").timeout(600000).get();
Elements tableRows = docVts.select("table.dynlist td:eq(0),td:eq(1),td:eq(3),td:eq(4),td:eq(7),td:eq(8)");
tableRows.size();
for(int i = 1; i < 80; i++){//only allows x results from vts list, from 1 not 0. 0 produces needless results
shippingList = tableRows.get(i).text().replaceAll("&nbsp;| ", "") +"\n";


arr_data.add(shippingList);// add value to ArrayList
System.out.println(shippingList);
};

docTide = Jsoup.connect("http://www.mhpa.co.uk/search-tide-times/").timeout(600000).get();
Elements tideTimeOdd = docTide.select("div.tide_row.odd div:eq(0)");
Elements tideTimeEven = docTide.select("div.tide_row.even div:eq(0)");
Elements tideHightOdd = docTide.select("div.tide_row.odd div:eq(2)");
Elements tideHightEven = docTide.select("div.tide_row.even div:eq(2)");
Element firstTideTime = tideTimeOdd.first();
Element secondTideTime = tideTimeEven.first();
Element thirdTideTime = tideTimeOdd.get(1);
Element fourthTideTime = tideTimeEven.get(1);

Element firstTideHight = tideHightOdd.first();
Element secondTideHight = tideHightEven.first();
Element thirdTideHight = tideHightOdd.get(1);
Element fourthTideHight = tideHightEven.get(1);

System.out.println("first tide time: " + firstTideTime.text() + " " + firstTideHight.text());
System.out.println("second tide time: " + secondTideTime.text() + " " + secondTideHight.text() );
System.out.println("third tide time: " + thirdTideTime.text() + " " + thirdTideHight.text());
System.out.println("fourth tide time: " + fourthTideTime.text() + " " + fourthTideHight.text());

{
/*
Work with data - all is OK
*/
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return arr_data;//<< return ArrayList from here

}
@Override
protected void onPostExecute(ArrayList<String> result) {
mFragmentCallback.onTaskDone();
}
}

感谢您的帮助。

最佳答案

您没有调用正确的 AsyncTask.execute()。正确的执行将调用 onPreExecute(),然后调用 doInBackground(),然后调用 onPostExecute()。

 new DATATask(this).execute(new FragmentCallback(){

@Override
public void onTaskDone() {
startMainAct();

}
});
}

将调用此方法(错误的方法):

   public void execute(FragmentCallback fragmentCallback) {
mFragmentCallback = fragmentCallback;
}

您要做的是将此方法更改为 - setFragmentCallBack(FragmentCallback);

然后在 OnPostExecute() 中添加:startMainAct();

而不是这样做:

                @Override
public void onTaskDone() {
startMainAct();

}

完成后,调用new DATATask(this).execute();这将调用 preExecute()、doInbackground 和 PostExecute()

您所做的是将 FragCallback 添加到 DataTask 而不是调用正确的执行函数。

希望对您有所帮助。

关于java - AsyncTask 回调不调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19124964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com