android– 创建一个计时器,以秒,分钟,小时,天为单位显示事件剩余时间

android– 创建一个计时器,以秒,分钟,小时,天为单位显示事件剩余时间,第1张

概述HelloIamtryingtocreateatimerwhereinitwilltakethedateandtimeoftheeventthatwillhappenandconvertitintomillisecondsandalsoitwilltakecurrenttimeandthensubtractthecurrenttimefromtheeventtimeandwillcalculatethehour

Hello I am trying to create a timer wherein it will take the date and time of the event that will happen and convert it into milliseconds and also it will take current time and then subtract the current time from the event time and will calculate the hour minutes seconds and days from it. What I want to do is display those values in a textvIEw but as and when I try and set it in textvIEw it stops working and gives error : -     03-25 17:06:03.166: E/AndroIDRuntime(993): FATAL EXCEPTION: main    03-25 17:06:03.166: E/AndroIDRuntime(993): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.indianic.planivore/com.indianic.planivore.vIEw.OnSelectingAnEventFromList}: androID.content.res.Resources$NotFoundException: String resource ID #0x38    03-25 17:06:03.166: E/AndroIDRuntime(993):  at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2059)    03-25 17:06:03.166: E/AndroIDRuntime(993):  at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)    03-25 17:06:03.166: E/AndroIDRuntime(993):  at androID.app.ActivityThread.access0(ActivityThread.java:130)    03-25 17:06:03.166: E/AndroIDRuntime(993):  at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)    03-25 17:06:03.166: E/AndroIDRuntime(993):  at androID.os.Handler.dispatchMessage(Handler.java:99)    03-25 17:06:03.166: E/AndroIDRuntime(993):  at androID.os.Looper.loop(Looper.java:137)    03-25 17:06:03.166: E/AndroIDRuntime(993):  at androID.app.ActivityThread.main(ActivityThread.java:4745)    03-25 17:06:03.166: E/AndroIDRuntime(993):  at java.lang.reflect.Method.invokeNative(Native Method)    03-25 17:06:03.166: E/AndroIDRuntime(993):  at java.lang.reflect.Method.invoke(Method.java:511)    03-25 17:06:03.166: E/AndroIDRuntime(993):  at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)    03-25 17:06:03.166: E/AndroIDRuntime(993):  at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:553)    03-25 17:06:03.166: E/AndroIDRuntime(993):  at dalvik.system.NativeStart.main(Native Method)    03-25 17:06:03.166: E/AndroIDRuntime(993): Caused by: androID.content.res.Resources$NotFoundException: String resource ID #0x38    03-25 17:06:03.166: E/AndroIDRuntime(993):  at androID.content.res.Resources.getText(Resources.java:229)    03-25 17:06:03.166: E/AndroIDRuntime(993):  at androID.Widget.TextVIEw.setText(TextVIEw.java:3620)    03-25 17:06:03.166: E/AndroIDRuntime(993):  at com.indianic.planivore.vIEw.OnSelectingAnEventFromList.onCreate(OnSelectingAnEventFromList.java:97)    03-25 17:06:03.166: E/AndroIDRuntime(993):  at androID.app.Activity.performCreate(Activity.java:5008)    03-25 17:06:03.166: E/AndroIDRuntime(993):  at androID.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)    03-25 17:06:03.166: E/AndroIDRuntime(993):  at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2023)    03-25 17:06:03.166: E/AndroIDRuntime(993):  ... 11 more

我的布局代码是: –

<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:background="#B7B1B1" >    <ImageVIEw        androID:ID="@+ID/imageOfAnEventAfterSelection"        androID:layout_wIDth="fill_parent"        androID:layout_height="wrap_content"        androID:contentDescription="@string/imageVIEwOfAnEvent"        androID:src="@drawable/img_location_image" />    <ImageVIEw        androID:ID="@+ID/shareTheEvent"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_alignBottom="@+ID/imageOfAnEventAfterSelection"        androID:layout_alignParentleft="true"        androID:layout_marginBottom="2dp"        androID:layout_marginleft="10dp"        androID:contentDescription="@string/shareOnSocialNetwork"        androID:src="@drawable/ic_shareicon" />    <ImageVIEw        androID:ID="@+ID/getSomeinformationOfEvent"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_alignBottom="@+ID/imageOfAnEventAfterSelection"        androID:layout_marginBottom="2dp"        androID:layout_marginleft="5dp"        androID:layout_toRightOf="@+ID/shareTheEvent"        androID:contentDescription="@string/getSomeinformation"        androID:src="@drawable/ic_information" />    <ImageVIEw        androID:ID="@+ID/setItToTheFavouriteIcon"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_alignBottom="@+ID/imageOfAnEventAfterSelection"        androID:layout_marginBottom="2dp"        androID:layout_marginleft="5dp"        androID:layout_toRightOf="@+ID/getSomeinformationOfEvent"        androID:contentDescription="@string/favouriteIconbactch"        androID:src="@drawable/ic_starbatchicon" />    <linearLayout        androID:layout_wIDth="fill_parent"        androID:layout_height="wrap_content"        androID:layout_below="@+ID/imageOfAnEventAfterSelection"        androID:layout_margin="5dp"        androID:background="@drawable/bg_clock"        androID:baselineAligned="false"        androID:orIEntation="horizontal"        androID:weightSum="4" >        <relativeLayout            androID:layout_wIDth="0dp"            androID:layout_height="match_parent"            androID:layout_weight="1" >            <ImageVIEw                androID:ID="@+ID/imgCollenDays"                androID:layout_wIDth="wrap_content"                androID:layout_height="wrap_content"                androID:layout_alignParentRight="true"                androID:layout_margintop="20dp"                androID:contentDescription="@string/collonSign"                androID:src="@drawable/collen" />            <TextVIEw                androID:ID="@+ID/numberOfDays"                androID:layout_wIDth="wrap_content"                androID:layout_height="wrap_content"                androID:layout_alignParentRight="true"                androID:layout_marginRight="20dp"                androID:layout_margintop="10dp"                androID:text="@string/defaultNull"                androID:textcolor="#E50303"                androID:textSize="30sp" />            <TextVIEw                androID:layout_wIDth="match_parent"                androID:layout_height="wrap_content"                androID:layout_below="@+ID/imgCollenDays"                androID:layout_margintop="10dp"                androID:gravity="center"                androID:text="@string/days"                androID:textcolor="#05660F" />        </relativeLayout>        <relativeLayout            androID:layout_wIDth="0dp"            androID:layout_height="match_parent"            androID:layout_weight="1" >            <ImageVIEw                androID:ID="@+ID/imgCollenHours"                androID:layout_wIDth="wrap_content"                androID:layout_height="wrap_content"                androID:layout_alignParentRight="true"                androID:layout_margintop="20dp"                androID:contentDescription="@string/collonSign"                androID:src="@drawable/collen" />            <TextVIEw                androID:ID="@+ID/numberOfHours"                androID:layout_wIDth="wrap_content"                androID:layout_height="wrap_content"                androID:layout_alignParentRight="true"                androID:layout_marginRight="20dp"                androID:layout_margintop="10dp"                androID:text="@string/defaultNull"                androID:textcolor="#E50303"                androID:textSize="30sp" />            <TextVIEw                androID:layout_wIDth="match_parent"                androID:layout_height="wrap_content"                androID:layout_below="@+ID/imgCollenHours"                androID:layout_margintop="10dp"                androID:gravity="center"                androID:text="@string/hours"                androID:textcolor="#05660F" />        </relativeLayout>        <relativeLayout            androID:layout_wIDth="0dp"            androID:layout_height="match_parent"            androID:layout_weight="1" >            <ImageVIEw                androID:ID="@+ID/imgCollenMinutes"                androID:layout_wIDth="wrap_content"                androID:layout_height="wrap_content"                androID:layout_alignParentRight="true"                androID:layout_margintop="20dp"                androID:contentDescription="@string/collonSign"                androID:src="@drawable/collen" />            <TextVIEw                androID:ID="@+ID/numberOfMinutes"                androID:layout_wIDth="wrap_content"                androID:layout_height="wrap_content"                androID:layout_alignParentRight="true"                androID:layout_marginRight="20dp"                androID:layout_margintop="10dp"                androID:text="@string/defaultNull"                androID:textcolor="#E50303"                androID:textSize="30sp" />            <TextVIEw                androID:layout_wIDth="match_parent"                androID:layout_height="wrap_content"                androID:layout_below="@+ID/imgCollenMinutes"                androID:layout_margintop="10dp"                androID:gravity="center"                androID:text="@string/minutes"                androID:textcolor="#05660F" />        </relativeLayout>        <relativeLayout            androID:layout_wIDth="0dp"            androID:layout_height="match_parent"            androID:layout_weight="1" >            <TextVIEw                androID:ID="@+ID/numberOfSeconds"                androID:layout_wIDth="wrap_content"                androID:layout_height="wrap_content"                androID:layout_alignParentRight="true"                androID:layout_marginRight="20dp"                androID:layout_margintop="10dp"                androID:text="@string/defaultNull"                androID:textcolor="#E50303"                androID:textSize="30sp" />            <TextVIEw                androID:layout_wIDth="match_parent"                androID:layout_height="wrap_content"                androID:layout_below="@+ID/numberOfSeconds"                androID:layout_margintop="2dp"                androID:gravity="center"                androID:text="@string/seconds"                androID:textcolor="#05660F" />        </relativeLayout>    </linearLayout>    <EditText         androID:ID="@+ID/chatEditText"        androID:layout_wIDth="fill_parent"        androID:layout_height="wrap_content"        androID:inputType="text"        androID:layout_alignParentBottom="true"        androID:hint="@string/EventTextVIEw"/></relativeLayout>

与此相关的Java文件如下所示我也试过片段,但它不工作在哪里我出错任何帮助任何尝试欢迎无论是对还是错,但我会尝试任何想法: –

package com.indianic.planivore.vIEw;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import androID.annotation.Suppresslint;import androID.app.Activity;import androID.os.Bundle;import androID.util.Log;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.TextVIEw;import androID.Widget.Toast;import com.indianic.planivore.R;@Suppresslint("SimpleDateFormat")public class OnSelectingAnEventFromList extends Activity {    String date;    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");    SimpleDateFormat time = new SimpleDateFormat("hh:mm");    Date eventDate = null;    Date eventAdditionalTime = null;    String getEventTime, getEventDate;    long milliSecondFromDate, milliSecondFromTime, totalmillisecond,            currentmilliseconds;    long timeleftinmilliseconds;    int seconds, minutes, hours, days, weeks;    TextVIEw tv_days, tv_hour, tv_minutes, tv_seconds;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        // Todo auto-generated method stub        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.event_postselection_layout);//  @OverrIDe//  public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container,    //      Bundle savedInstanceState) {        // Todo auto-generated method stub//      VIEw v = inflater.inflate(R.layout.event_postselection_layout,//              container, false);        // Here getting findVIEvbyID of all the text vIEw to set the time left.        tv_days = (TextVIEw) findVIEwByID(R.ID.numberOfDays);        tv_hour = (TextVIEw) findVIEwByID(R.ID.numberOfHours);        tv_minutes = (TextVIEw) findVIEwByID(R.ID.numberOfMinutes);        tv_seconds = (TextVIEw) findVIEwByID(R.ID.numberOfSeconds);        Log.d("PRINTING THE DAYS", tv_days.getText().toString());        Bundle getdata = getIntent().getExtras();        getEventTime = getdata.getString("timeleftkey");        getEventDate = getdata.getString("datekey");        Log.d("kjgkjnbjnknlj", getEventDate + "   " + getEventTime);        //tv_days.setText("YO");        // Here I am doing is that the time that I am getting I am converting it        // to milliseconds so I will add it to the millisecond of the date and        // will display it in the time left.        try {            eventAdditionalTime = time.parse(getEventTime);            milliSecondFromTime = eventAdditionalTime.getTime();        } catch (Exception e1) {            // Todo auto-generated catch block            e1.printstacktrace();        }        try {            eventDate = dateFormat.parse(getEventDate);            milliSecondFromDate = eventDate.getTime();        } catch (Exception e) {            // Todo: handle exception        }        totalmillisecond = milliSecondFromDate + milliSecondFromTime;        Log.d("milliSecondFromDate", "" + milliSecondFromDate);        Log.d("milliSecondFromTime", "" + milliSecondFromTime);        Calendar cal = Calendar.getInstance();        currentmilliseconds = cal.getTime().getTime();        if (currentmilliseconds <= totalmillisecond) {            while(currentmilliseconds <= totalmillisecond){                timeleftinmilliseconds = totalmillisecond - currentmilliseconds;                seconds = (int) (timeleftinmilliseconds / 1000);                // This below will give me seconds left.                minutes = seconds / 60;                seconds = seconds - minutes * 60;                tv_seconds.setText(seconds);                // This below will give me minutes left.                hours = minutes / 60;                minutes = minutes - hours * 60;                tv_minutes.setText(minutes);                // This below will give me hours left.                days = hours / 24;                hours = hours - days * 24;                tv_hour.setText(hours);                // This below will give me days left.                weeks = days / 7;                days = days - weeks * 7;                tv_days.setText(days);            }        } else {            Toast.makeText(OnSelectingAnEventFromList.this, "Lol YOU MISSED IT", 5000).show();        }//      return v;    }}

解决方法:

您的ints秒,分钟和小时等被视为资源. textVIEw.setText()有两个签名,一个是String,一个是int来查找资源.这就是你得到错误的原因:

Caused by: androID.content.res.Resources$NotFoundException: String resource ID #0x3803-25 17:06:03.166: E/AndroIDRuntime(993):  at androID.content.res.Resources.getText(Resources.java:229)

你必须将它们转换为这样的String

String.valueOf(days);

更新

public class MainActivity extends Activity {@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    final TextVIEw tv = new TextVIEw(this);    setContentVIEw(tv);    int hoursToGo = 0;    int minutesToGo = 0;    int secondsToGo = 30;    int milListoGo = secondsToGo*1000+minutesToGo*1000*60+hoursToGo*1000*60*60;    new CountDownTimer(milListoGo,1000) {        @OverrIDe        public voID onTick(long millis) {            int seconds = (int) (millis / 1000) % 60 ;            int minutes = (int) ((millis / (1000*60)) % 60);            int hours   = (int) ((millis / (1000*60*60)) % 24);            String text = String.format("%02d hours, %02d minutes, %02d seconds",hours,minutes,seconds);            tv.setText(text);        }        @OverrIDe        public voID onFinish() {            tv.setText("Kabooom");                      }    }.start();}}
总结

以上是内存溢出为你收集整理的android – 创建一个计时器,以秒,分钟,小时,天为单位显示事件剩余时间全部内容,希望文章能够帮你解决android – 创建一个计时器,以秒,分钟,小时,天为单位显示事件剩余时间所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1114823.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存