android wait函数使用,wait()函数在android studio中的线程中不起作用(wait() function not working in thread in android s...

在Android开发中,开发者遇到线程中的wait()函数无法按照预期每3秒改变文本颜色的问题。原来,直接在线程中使用wait()会导致程序崩溃。解决方案是改用Handler来实现定时任务,通过postDelayed()方法每3秒更新颜色,避免了线程同步问题。
摘要由CSDN通过智能技术生成

wait()函数在android studio中的线程中不起作用(wait() function not working in thread in android studio)

在提出问题之前,让我告诉你我是android开发的新手。 我的问题是关于线程和wait(); 功能。 现在我正在尝试实现应该每隔3秒更改一次文本颜色的线程,但程序一启动就会崩溃。 这是代码。

package care.client_application;

public class MainActivity extends AppCompatActivity {

Thread Thread3=null; //Coloring thread

public TextView Warning;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Warning= (TextView) findViewById(R.id.warning);

UIHandler = new Handler(); //Initialization of handler

this.Thread3= new Thread(new Thread3());

this.Thread3.start();

}

class Thread3 implements Runnable{

@Override

public void run() {

Warning.setTextColor(Color.rgb(200,200,0));

wait(3000);

Warning.setTextColor(Color.rgb(200,0,0));

}

}

}

Before asking the question let me tell you that I am new in android development. My question is regarding Threads and wait(); function. Right now I am trying to implement the thread which should change the color of text every 3 seconds but the program crashes as soon as I start it. Here is the code.

package care.client_application;

public class MainActivity extends AppCompatActivity {

Thread Thread3=null; //Coloring thread

public TextView Warning;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Warning= (TextView) findViewById(R.id.warning);

UIHandler = new Handler(); //Initialization of handler

this.Thread3= new Thread(new Thread3());

this.Thread3.start();

}

class Thread3 implements Runnable{

@Override

public void run() {

Warning.setTextColor(Color.rgb(200,200,0));

wait(3000);

Warning.setTextColor(Color.rgb(200,0,0));

}

}

}

原文:https://stackoverflow.com/questions/42831993

2020-01-09 06:01

满意答案

我想每3秒更改一次文本的颜色

为什么不使用Handler而不是Thread

private Runnable task = new Runnable () {

public void run() {

//update textview color

Warning.setTextColor(Color.rgb(200,200,0));

mHandler.postDelayed(task, 3000); //repeat task

}

};

用它来调用:

private Handler mHandler = new Handler();

// call updateTask after 3 seconds

mHandler.postDelayed(task, 3000);

要停止任务:

@Override

protected void onPause() {

mHandler.removeCallbacks(task);

super.onPause();

}

I want to change the color of text every 3 seconds

Why not use a Handler instead of Thread

private Runnable task = new Runnable () {

public void run() {

//update textview color

Warning.setTextColor(Color.rgb(200,200,0));

mHandler.postDelayed(task, 3000); //repeat task

}

};

Call it using:

private Handler mHandler = new Handler();

// call updateTask after 3 seconds

mHandler.postDelayed(task, 3000);

To stop the task:

@Override

protected void onPause() {

mHandler.removeCallbacks(task);

super.onPause();

}

2017-03-16

相关问答

加 compile 'com.android.support:design:24.1.1'

到您的gradle文件。 它应该工作。 它的设计库的一部分。 所以你需要添加这个来访问CoordinatorLayout 。 Add compile 'com.android.support:design:24.1.1'

to your gradle file. It should work. Its part of design library. So you need to add this one...

原来,我不得不使用本地genymotion recorder , https: genymotion recorder Turns out I had to use the native genymotion recorder, https://docs.genymotion.com/Content/03_Virtual_Devices/Emulating_sensors_and_features/Capture.htm

以下是从异步线程更新UI元素的正确方法: public class SampleActivity extends Activity {

private TextView hourText;

private TextView minuteText;

private void updateHourText(final String text) {

runOnUiThread(new Runnable() {

@Override

...

您是在谈论命令行应用程序还是Windows应用程序? 你必须记住VC2008是一个特定于Windows的开发环境,所以如果你在linux / unix或旧的dos系统上学习编程,那么事情就不一样了。 我知道在Windows下更改控制台应用程序中光标位置的唯一方法是使用Windows函数SetConsoleCursorPositon。 http://msdn.microsoft.com/es-es/library/windows/desktop/ms686025(v=vs.85).aspx 我希望这...

你是100%正确的,有些JNI值会被缓存和重用。 类引用和方法ID是很好的例子。 请记住, FindClass()返回一个本地引用,因此您需要为保存在缓存中的每个类使用NewGlobalRef() 。 Android Studio无法帮助我们完成此设置,而且我不知道可以为我们做这种重构的可靠工具。 您可以从开源代码学习良好实践,例如从WebRTC JNI包装器或Spotify JNI帮助器 。 Android Studio只能跟踪本机方法,而不是缓存对象,转换等。 You are 100% rig...

文件>设置>项目设置>代码样式 - Java 使用制表符 如果选中此复选框,则使用选项卡字符:按Tab键进行缩进对于代码重新格式化清除复选框后,IntelliJ IDEA使用空格而不是制表符。 智能标签 如果选中此复选框,则IntelliJ IDEA会插入用于缩进和重新格式化的选项卡,但只有通过空格才能完成与必要列的精确对齐,而无需使用制表符替换它们。 这样做是为了在更改选项卡大小时保留源代码的可视化表示。 如果清除此复选框,则仅使用选项卡。 这意味着符合指定选项卡大小的一组空格会自动替换为选项卡...

在您的visual studio cordova项目中检查您的id是否在config.xml中具有android签名 。 如果不重命名id并重建项目。 id = mytestapp.test - 错了 id = com .mytestapp.test - 正确 In your visual studio cordova project check whether your id have the android signature in config.xml. If not rename the ...

我不能说为什么你的测试似乎在之前运行,而不是现在,但我克隆了项目,并能够通过以下方式运行测试: 将AdventureTest从src/androidTest源代码树移动到src/test 从AdventureTest注释/删除所有Android类 添加一个简单的虚拟@Test方法(例如assertTrue(false) ) 一般经验法则: 如果您要在测试中使用Android类,请使用Robolectric 否则,请使用你喜欢的测试跑步者的任何实现(Mockito在你的情况下) 编辑 - 添加样本机...

我自己花了一天的时间,终于搞清楚了。 这是一个Android Studio功能 - 称为功能,但我认为它是一个错误。 要使仪器测试正常工作,您需要将您的Build Variants设置为以下内容: Test Artifact: Android Instrumentation Tests

Build Variant: debug

详情请看这里 我个人认为是没有道理的; 它不像你正在使用androidTestCompileDebug ,并且运行gradle :dependenci...

我想每3秒更改一次文本的颜色 为什么不使用Handler而不是Thread private Runnable task = new Runnable () {

public void run() {

//update textview color

Warning.setTextColor(Color.rgb(200,200,0));

mHandler.postDelayed(task, 3000); //repea...

相关文章

This was written in December, 1999 Lots of programm

...

http://helephant.com/2012/07/14/javascript-function

...

引用 public void start() Causes this thread to

...

不使用线程thread可以正常接受报文,加了后就在recvfrom那里不动了,是怎么回事? usin

...

作用: 作为 table 的子节点,用于声明路由规则 属性: ref – Function 类型的 b

...

/** * 只能这么写 * 不然 腾讯微博会报Can't create handler inside

...

请问在android应用中,能不能加载指定目录下的布局文件,比如:从服务器上下载一个布局文件存放在SD

...

注意: 本文介绍的是Share SDK 2.x版本的集成流程和注意事项,对于Share SDK 1.x

...

在你的app应用里增加微信分享的功能,可以分享给好友、朋友圈。 首先,看官方文档这是必须的: 微信An

...

李刚编著的《疯狂Android讲义》全面地介绍了Android应用开发的相关知识,全书内容覆盖了And

...

最新问答

解释我的评论,React本身会抛出无效的PropType错误,因此阅读其控制台日志应该可以解决您的问题。 正如你所说:如果你使用任何包装进行测试,日志可能会被压制,所以从裸机上运行它们会有所帮助。 Paraphrasing my comment, React itself throws invalid PropType errors so reading its console log should solve your problem. As you noted: The log might

调用PDO对象的lastInsertId。 if($stmt->execute()) { return $pdo->lastInsertId(); } return false; call lastInsertId of your PDO object. if($stmt->execute()) { return $pdo->lastInsertId(); } return false;

应该可以使用命令 sudo /etc/init.d/apache2 reload 希望有所帮助 should be possible using the command sudo /etc/init.d/apache2 reload hope that helps

是的; 此外,您的close()将始终执行并不保证。 (例如,如果发生Exception )。 with open('somefile.txt') as my_file: 1/0 # raise Exception my_file.close() # Your close() call is never going to be called 但是, 始终执行with语句的__exit__()函数,因为它遵循try...except...finally模式。 with语句用于使用

尝试使用: location.reload(true); 当此方法接收到true值作为参数时,将导致始终从服务器重新加载页面。 如果它是假的或没有指定,浏览器可能从它的缓存重新加载页面。 更多信息: 位置对象 Try to use: location.reload(true); When this method receives a true value as argument, it will cause the page to always be reloaded from the se

数组具有固定大小,在编译时确定。 切片具有固定的大小,在运行时确定。 最简单的方法是接受一片切片: fn convolve(matrix: &[&[i32]]) { println!("{:?}", matrix); } fn main() { let matrix = &[ &[0, 0, 0][..], &[0, 1, 0][..], &[0, 0, 0][..] ]; convolve(matrix); }

答案是像这样使用SUM(1) OVER(PARTITION BY group_field ORDER BY order field ROWS UNBOUNDED PRECEDING)构造: SELECT id, name, cnt FROM (SELECT id, name, count(*) cnt, sum(1) over (partition BY id ORDER BY cnt DESC ROWS

我看仁 和 会计还可以吧,他们在襄阳也是有一个学校的,很多地方都有他们的学校,我以前在武汉读书的时候就知道他们,寝室的一个同学就是在他们那儿学的,她是先考了一个会计证,然后再去他们那儿学的做账方面的一些。她学习本来就很认真的

Brandon Haston的评论很好地回答了第一个问题。 对第二个问题的回答不符合评论。 threads[i] = thread(...); 表示已创建std :: thread并且其代表性std :: thread对象已分配给std :: thread数组中的插槽。 这提出了一个问题,当我有一个编译器可以使用时,我将不得不自己查看:刚被覆盖的线程发生了什么? 无论如何,新线程不在另一个线程内。 线程没有任何所有权相互的概念。 进程拥有线程,但线程没有。 线程可以启动另一个线程。 例如, v

没有进一步调试,400错误太过模糊,无法提供太多帮助。 根据经验,我可以告诉你,在geowebcache服务器不喜欢为您请求的wms层提供服务之前,我已经看到了一个问题。 Mapfish试图用不同的瓷砖尺寸做奇怪的事情(最终你得到10%的门槛误差)。 您的日志是否显示它请求的图像? 你能在我们的浏览器中找到那个瓷砖,看看服务器究竟说了什么吗? 这就是我最终揭露问题的方式。 为了便于调试,我还创建了一个单独的mapfish日志,以便更容易找到我的mapfish问题。 使用Geoserver管理界面

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值