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.access$600(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 #0x38
03-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 {
@Override
protected 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,countdowntimer 来源: https://codeday.me/bug/20190725/1531141.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。