runnable

Runnable is posted successfully but not run

穿精又带淫゛_ 提交于 2019-11-27 00:59:50
问题 In an existing Android project I've encountered the following piece of code (where I inserted the debugging litter) ImageView img = null; public void onCreate(...) { img = (ImageView)findViewById(R.id.image); new Thread() { public void run() { final Bitmap bmp = BitmapFactory.decodeFile("/sdcard/someImage.jpg"); System.out.println("bitmap: "+bmp.toString()+" img: "+img.toString()); if ( !img.post(new Runnable() { public void run() { System.out.println("setting bitmap..."); img.setImageBitmap

Is there a way to make Runnable's run() throw an exception?

拟墨画扇 提交于 2019-11-27 00:16:06
问题 A method I am calling in run() in a class that implements Runnable) is designed to be throwing an exception. But the Java compiler won't let me do that and suggests that I surround it with try/catch. The problem is that by surrounding it with a try/catch I make that particular run() useless. I do want to throw that exception. If I specify throws for run() itself, the compiler complains that Exception is not compatible with throws clause in Runnable.run() . Ordinarily I'm totally fine with not

Runnable with a parameter? [duplicate]

。_饼干妹妹 提交于 2019-11-26 23:34:27
This question already has an answer here: How can I pass a parameter to a Java Thread? 18 answers I have a need for a "Runnable that accepts a parameter" although I know that such runnable doesn't really exist. This may point to fundamental flaw in the design of my app and/or a mental block in my tired brain, so I am hoping to find here some advice on how to accomplish something like the following, without violating fundamental OO principles: private Runnable mOneShotTask = new Runnable(String str) { public void run(String str) { someFunc(str); } }; Any idea how to accomplish something like

Can you have two activities running at the same time?

你。 提交于 2019-11-26 23:31:12
问题 What I have is an activity with a severer, and another activity with different information on it. However when I open the non-sever activity the sever closes. Is there a way that I can get this to stop? If you need to see any code I would be happy to show it. 回答1: You cannot have multiple activities running at the same time. If you want code to run in the background you need to use a Service . For more information checkout the docs: http://developer.android.com/reference/android/app/Service

Android update TextView in Thread and Runnable

孤街醉人 提交于 2019-11-26 22:08:11
I want to make a simple timer in Android that updates a TextView every second. It simply counts seconds like in Minesweeper. The problem is when i ignore the tvTime.setText(...) (make it //tvTime.setText(...), in LogCat will be printed the following number every second. But when i want to set this number to a TextView (created in another Thread), the program crashes. Does anyone have an idea how to solve this easily? Here's the code (method is called on startup): private void startTimerThread() { Thread th = new Thread(new Runnable() { private long startTime = System.currentTimeMillis();

Why is run() not immediately called when start() called on a thread object in java

有些话、适合烂在心里 提交于 2019-11-26 21:00:06
问题 Or is it? I have a thread object from: Thread myThread = new Thread(pObject); Where pObject is an object of a class implementing the Runnable interface and then I have the start method called on the thread object like so: myThread.start(); Now, my understanding is that when start() is called, the JVM implicitly (and immediately) calls the run() method which may be overridden (as it is in my case) However, in my case, it appears that the start() method is not called immediately (as desired)

Android: How do I stop Runnable?

一曲冷凌霜 提交于 2019-11-26 18:42:11
I tried this way: private Runnable changeColor = new Runnable() { private boolean killMe=false; public void run() { //some work if(!killMe) color_changer.postDelayed(changeColor, 150); } public void kill(){ killMe=true; } }; but I can't access kill() method! yorkw Instead implement your own thread.kill() mechanism, using existing API provided by the SDK. Manage your thread creation within a threadpool , and use Future.cancel() to kill the running thread: ExecutorService executorService = Executors.newSingleThreadExecutor(); Runnable longRunningTask = new Runnable(); // submit task to

FacesContext.getCurrentInstance() returns null in Runnable class

萝らか妹 提交于 2019-11-26 17:05:47
问题 I am trying to get the FacesContext by calling FacesContext.getCurrentInstance() in the run() method of a Runnable class, but it returns null . public class Task implements Runnable { @Override public void run() { FacesContext context = FacesContext.getCurrentInstance(); // null! // ... } } How is this caused and how can I solve it? 回答1: The FacesContext is stored as a ThreadLocal variable in the thread responsible for the HTTP request which invoked the FacesServlet , the one responsible for

Why do variables passed to runnable need to be final?

旧时模样 提交于 2019-11-26 17:00:55
问题 If I have a variable int x = 1 , say, and I declare a runnable in the main thread, and I want to pass x to the runnable's run() method, it must be declared final . Why? final int x = 0;//<----must be final... private class myRun implements Runnable { @Override public void run() { x++;// } } 回答1: Because if they are able to be changed, it could cause a lot of problems, consider this: public void count() { int x; new Thread(new Runnable() { public void run() { while(x < 100) { x++; try { Thread

removeCallbacks not stopping runnable

ε祈祈猫儿з 提交于 2019-11-26 16:33:52
I am calling from a method: myHandler.postDelayed(mMyRunnableHide, 6000); which calls: public Runnable mMyRunnableHide = new Runnable() { public void run() { mTextDisplay.setText(""); DisplayX(); } }; if a button on screen is clicked I want to stop the runnable: Button next = (Button) findViewById(R.id.Breaction); next.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { myHandler.removeCallbacks(mMyRunnableHide); mTextDisplay.setText(""); DisplayX(); } }); } the removecallbacks is not stopping the runnable. What am I doing wrong? Am I using the correct method? I