java – 如何在UI线程中运行此类?
发布时间:2020-12-15 04:26:48 所属栏目:Java 来源:网络整理
导读:我有类ScheduleTimer,它适用于日期数组.这里是: class ScheduleTimer { public TextView textView; private Timer dateTimer; private Timer remainderTimer; private Date formatDate = new Date(); private Date nextDate; private boolean remainderTime
我有类ScheduleTimer,它适用于日期数组.这里是:
class ScheduleTimer { public TextView textView; private Timer dateTimer; private Timer remainderTimer; private Date formatDate = new Date(); private Date nextDate; private boolean remainderTimerStarted; private static final long REMINDER_UPDATE_INTERVAL = 1000; private static String[] DATES; private int currentIndex; public ScheduleTimer(final TextView t) { textView = t; dateTimer = new Timer(); } public void main(String[] dates) throws ParseException { checkDates(dates); run(); } private void checkDates(String[] dates) throws ParseException { List<String> list = new ArrayList<>(); DateFormat format = new SimpleDateFormat("dd.MM.yyyy HH:mm",Locale.ENGLISH); for(String date : dates) { long current = System.currentTimeMillis() + 1000; if(format.parse(date).getTime() - current > 0) { list.add(date); } } DATES = new String[list.size()]; list.toArray(DATES); } private void run() { nextDate = parseDate(DATES[currentIndex]); schedule(); } public void schedule() { runSecondsCounter(); dateTimer.schedule(new TimerTask() { @Override public void run() { System.out.println("Current date is:" + new Date()); currentIndex++; if (currentIndex < DATES.length) { nextDate = parseDate(DATES[currentIndex]); System.out.println("Next date is:" + nextDate); schedule(); } else { remainderTimer.cancel(); } } },nextDate); } private Date parseDate(String nextDate) { Date date = null; DateFormat format = new SimpleDateFormat("dd.MM.yyyy HH:mm",Locale.ENGLISH); try { date = format.parse(nextDate); } catch (ParseException e) { e.printStackTrace(); } return date; } private void runSecondsCounter() { if (remainderTimerStarted) { remainderTimer.cancel(); } remainderTimer = new Timer(); remainderTimer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { remainderTimerStarted = true; long remains = nextDate.getTime() - new Date().getTime(); System.out.println("Remains: " + (remains / 1000) + " seconds"); formatDate.setTime(remains); textView.setText(formatDate.toString()); } },REMINDER_UPDATE_INTERVAL,REMINDER_UPDATE_INTERVAL); } } 如果我像Java应用程序一样运行它,而不是android,它可以正常运行,并且它会在控制台中打印出每个计数秒.但是当它在android环境中运行它时,它要么说不能从任何其他线程触及UI线程,要么它在ScheduleTimer类的方法run()中给我NullPointerException. 我正在使用它:new ScheduleTimer(textView).main(new String [] {“13.04.2015 13:59”,“13.04.2015 14:14”,“13.04.2015 14:15”}); 我尝试使用AsyncTask或Handler,但可能,我做得不对. 有人可以帮我吗?如何在我的onCreateView方法中正常运行它并正确传递所需的TextView? 解决方法
完整的答案是:
你的片段: public class PlaceholderFragment extends Fragment { public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main,container,false); TextView textView = (TextView) rootView.findViewById(R.id.tv); try { new ScheduleTimer(textView,getActivity()) .main(new String[] {"13.04.2015 13:59","13.04.2015 14:14","13.04.2015 14:15"}); } catch (ParseException e) { e.printStackTrace(); } return rootView; } } 您的ScheduleTimer类: class ScheduleTimer { public TextView textView; private Timer dateTimer; private Timer remainderTimer; private Date formatDate = new Date(); private Date nextDate; private boolean remainderTimerStarted; private static final long REMINDER_UPDATE_INTERVAL = 1000; private static String[] DATES; private int currentIndex; private Activity activity; public ScheduleTimer(final TextView t,Activity a) { textView = t; activity = a; dateTimer = new Timer(); } public void main(String[] dates) throws ParseException { checkDates(dates); run(); } private void checkDates(String[] dates) throws ParseException { List<String> list = new ArrayList<>(); DateFormat format = new SimpleDateFormat("dd.MM.yyyy HH:mm",Locale.ENGLISH); try { date = format.parse(nextDate); } catch (ParseException e) { e.printStackTrace(); } return date; } private void runSecondsCounter() { if (remainderTimerStarted) { remainderTimer.cancel(); } remainderTimer = new Timer(); remainderTimer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { remainderTimerStarted = true; long remains = nextDate.getTime() - new Date().getTime(); System.out.println("Remains: " + (remains / 1000) + " seconds"); formatDate.setTime(remains); activity.runOnUiThread(new Runnable() { @Override public void run() { textView.setText(formatDate.toString()); } }); } },REMINDER_UPDATE_INTERVAL); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |