gpt4 book ai didi

java - 自定义 TextView 引发 RuntimeException "Error inflating"

转载 作者:行者123 更新时间:2023-12-01 13:12:33 25 4
gpt4 key购买 nike

我想扩展TextView来实现秒表功能。我遇到的问题是关于膨胀过程的运行时异常。

03-30 12:57:28.749: E/AndroidRuntime(5700): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.lipsksoft.apps.anwendung.t6student/de.lipsksoft.apps.anwendung.t6student.opsActivity}:
android.view.InflateException: Binary XML file line #29: Error inflating class de.lipsksoft.apps.anwendung.t6student.TimerView.t6student.TimerView

由于这里的提示,我做了一些更改。我的新代码:

public class Timer extends TextView{

private Handler handler = new Handler();
private long startTime;
private long elapsedTime;
private final int REFRESH_RATE = 100;
private String hours,minutes,seconds,milliseconds;
private long secs,mins,hrs;
private boolean stopped = false;

Timer(Context context){
super(context);
}

Timer (Context context, AttributeSet attrs) {
super(context, attrs);
}

Timer(Context context, AttributeSet attrs, int defStyle){
super(context, attrs, defStyle);
}

public void start(){
if(stopped){
startTime = System.currentTimeMillis() - elapsedTime;
}
else{
startTime = System.currentTimeMillis();
}
handler.removeCallbacks(startTimer);
handler.postDelayed(startTimer, 0);
}

public void stop(){
handler.removeCallbacks(startTimer);
stopped = true;
}

private Runnable startTimer = new Runnable() {
public void run() {
elapsedTime = System.currentTimeMillis() - startTime;
updateTimer(elapsedTime);
handler.postDelayed(this,REFRESH_RATE);
}
};

private void updateTimer (float time){
secs = (long)(time/1000);
mins = (long)((time/1000)/60);
hrs = (long)(((time/1000)/60)/60);

/* Convert the seconds to String
* and format to ensure it has
* a leading zero when required
*/
secs = secs % 60;
seconds=String.valueOf(secs);
if(secs == 0){
seconds = "00";
}
if(secs <10 && secs > 0){
seconds = "0"+seconds;
}

/* Convert the minutes to String and format the String */

mins = mins % 60;
minutes=String.valueOf(mins);
if(mins == 0){
minutes = "00";
}
if(mins <10 && mins > 0){
minutes = "0"+minutes;
}

/* Convert the hours to String and format the String */

hours=String.valueOf(hrs);
if(hrs == 0){
hours = "00";
}
if(hrs <10 && hrs > 0){
hours = "0"+hours;
}

/* Although we are not using milliseconds on the timer in this example
* I included the code in the event that you wanted to include it on your own
*/
milliseconds = String.valueOf((long)time);
if(milliseconds.length()==2){
milliseconds = "0"+milliseconds;
}
if(milliseconds.length()<=1){
milliseconds = "00";
}
milliseconds = milliseconds.substring(milliseconds.length()-3, milliseconds.length()-2);

/* Setting the timer text to the elapsed time */
this.setText(minutes + ":" + seconds + "." + milliseconds);
//activity.((TextView)findViewById(R.id.timerMs)).setText("." + milliseconds);
}
}

XML:

 <de.lipsksoft.apps.anwendung.t6student.Timer
android:id="@+id/timer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TimerView" />

但我仍然遇到同样的错误。您知道出了什么问题吗?

感谢您的帮助。

最佳答案

你需要有这些构造函数

Android Custom View Constructor

public TimerView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
public TimerView (Context context) {
super(context);
}
public TimerView (Context context, AttributeSet attrs) {
super(context, attrs);
}

你还有

this.setText(milliSeconds + ":" + seconds + ":" + minutes);

您无法从非 ui 线程更新 ui。

阅读

http://developer.android.com/guide/components/processes-and-threads.html

编辑:

你有什么

public class Timer extends TextView{

然后在 xml 中你有

<de.lipsksoft.apps.anwendung.t6student.TimerView // TimerView is not the same as Timer

编辑2:

Activity 中

TimerView  tv = (TimerView) this.findViewById(R.id.timer1);
tv.start();

然后在xml中

我的包名称不同

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.example.testall.TimerView
android:id="@+id/timer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TimerView" />
</RelativeLayout>

然后

public class TimerView extends TextView{

private Handler handler = new Handler();
private long startTime;
private long elapsedTime;
private final int REFRESH_RATE = 100;
private String hours,minutes,seconds,milliseconds;
private long secs,mins,hrs;
private boolean stopped = false;

public TimerView(Context context){
super(context);
//start();
}

public TimerView(Context context, AttributeSet attrs) {
super(context, attrs);
//start();
}

public TimerView(Context context, AttributeSet attrs, int defStyle){
super(context, attrs, defStyle);
//start();
}

public void start(){
if(stopped){
startTime = System.currentTimeMillis() - elapsedTime;
}
else{
startTime = System.currentTimeMillis();
}
handler.removeCallbacks(startTimer);
handler.postDelayed(startTimer, 0);
}

public void stop(){
handler.removeCallbacks(startTimer);
stopped = true;
}

private Runnable startTimer = new Runnable() {
public void run() {
elapsedTime = System.currentTimeMillis() - startTime;
updateTimer(elapsedTime);
handler.postDelayed(this,REFRESH_RATE);
}
};

private void updateTimer (float time){
secs = (long)(time/1000);
mins = (long)((time/1000)/60);
hrs = (long)(((time/1000)/60)/60);

/* Convert the seconds to String
* and format to ensure it has
* a leading zero when required
*/
secs = secs % 60;
seconds=String.valueOf(secs);
if(secs == 0){
seconds = "00";
}
if(secs <10 && secs > 0){
seconds = "0"+seconds;
}

/* Convert the minutes to String and format the String */

mins = mins % 60;
minutes=String.valueOf(mins);
if(mins == 0){
minutes = "00";
}
if(mins <10 && mins > 0){
minutes = "0"+minutes;
}

/* Convert the hours to String and format the String */

hours=String.valueOf(hrs);
if(hrs == 0){
hours = "00";
}
if(hrs <10 && hrs > 0){
hours = "0"+hours;
}

/* Although we are not using milliseconds on the timer in this example
* I included the code in the event that you wanted to include it on your own
*/
milliseconds = String.valueOf((long)time);
if(milliseconds.length()==2){
milliseconds = "0"+milliseconds;
}
if(milliseconds.length()<=1){
milliseconds = "00";
}
milliseconds = milliseconds.substring(milliseconds.length()-3, milliseconds.length()-2);

/* Setting the timer text to the elapsed time */
this.setText(minutes + ":" + seconds + "." + milliseconds);
//activity.((TextView)findViewById(R.id.timerMs)).setText("." + milliseconds);
}
}

快照

enter image description here

关于java - 自定义 TextView 引发 RuntimeException "Error inflating",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22743179/

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