I'm trying to make a fighting screen where I have two sprites, and on top of them I have health bars with their health points written (ProgressBar with a TextView on top of it).
I also have a AnimationDrawable. And it is started inside of onWindowsFocusChanged(). I want to have the text in front of the progressBar change after the animation. So, for example, before the animation a bar has 150/150 written and after the animation I want it to change to, for example, 80/150. The thing is, whenever I try to call setText, the app crashes (I guess because onWindowFocusChanged is the last thing that's called). Is there a way to do this?
Here's a snippet of my code (number_one.start() is the starting of the animation):
private void health_bars(int points_one, int points_two){
healthBarOne.setMax(MAX_HEALTH);
healthBarOne.setProgress(points_one);
health_points_one.setText(points_one + "/" + MAX_HEALTH);
healthBarTwo.setMax(MAX_HEALTH);
healthBarTwo.setProgress(points_two);
health_points_two.setText(points_two + "/" + MAX_HEALTH);
}
public void onWindowFocusChanged(final boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(hasFocus){
Thread th = new Thread(){
public void run(){
number_one.start();
try {
Thread.sleep(2000);
} catch(InterruptedException e){
}
health_bars(new_health_one, new_health_two);
try {
Thread.sleep(2000);
} catch(InterruptedException e){
}
finish();
}
};
th.start();
attackAnimation();
}
}
Thank you for your time!
EDIT:
Error Log
You cannot update UI elements from any other thread than UI. That is basically what the error is saying. To fix this, use runOnUiThread method in Android:
Thread th = new Thread(){
public void run(){
runOnUiThread(new Runnable() {
#Override
public void run() {
health_bars(new_health_one, new_health_two);
}
});
}
}
Related
Im writing a script which consists of a thread with a wait. If the thread get executed, it doesn't update the screen. In the script i change a picture to another picture, but it doesn't show it until I press the button again. So the main question is: After setting an different Image Resource, i need to update the screen.
Here is the code:
public void Start(View view) {
Runnable r = new Runnable() {
#Override
public void run() {
int i =0;
while(i<12) {
synchronized (this) {
try {
Setfoto(i);
Thread.sleep(1000);
Log.i(TAG, "Begonnen met loop");
Log.i(TAG, "i = " + i);
i = i +1;
} catch (Exception e) {}
}
}
}
};
Thread buckysThread = new Thread(r);
buckysThread.start();
}
public void Setfoto(int nummer) {
if (nummer == 1) {
een.setImageResource(R.drawable.eerster);
Log.i(TAG, "Foto 1 wordt rood");
} if(nummer ==2) {
twee.setImageResource(R.drawable.eerster);
Log.i(TAG, "Foto 2 wordt rood");
} if(nummer ==3) {
drie.setImageResource(R.drawable.tweer);
Log.i(TAG, "Foto 3 wordt rood");
}
}
You'll need to have some sort of way to run code on the UI thread, which is responsible for updating the contents of the screen and manipulating views.
Activity.runOnUiThread(Runnable) can be called. Any code placed in there will be run on the UI thread.
You can use runOnUiThread, AsyncTask or Thread-Handler to implement your task
Activity.runOnUiThread
runOnUiThread(new Runnable() {
public void run() {
//Do something on UiThread
}
});
Thread-Handler
Communicating with the UI Thread
AsyncTask
AsyncTask Tutorial
I am making an app to do something in webview automatically.
I want to make a pause between two lines inside (for-loop) until page finished loading without using Thread.sleep because it freezing my application.
this is my code:
webview.loadUrl("http://**********");
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
for(int i=1;i<10;i++){
evaluateJavascript( "document.getElementById('select').value=" + i)
evaluateJavascript("document.getElementById('Search').click();")
//wait until finished loading
while( isloading() ){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
evaluateJavascript( "document.getElementById('any_select').value=5")
.
.
.
.
}
public boolean isloading(){
boolean isloading;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
webview.evaluateJavascript("(function() { return document.getElementById(\"Loading\").style.display; })();", new ValueCallback<String>() {
#Override
public void onReceiveValue(String s) {
if(s.equals("none")){
isloading=false;
}else{
isloading=true;
}
}
});
}
if(isloading=true)return true;
if(isloading=false)return false;
}
If you don't want to use Thread.sleep then the alternative is to use AsyncTask in your application.
You can do your loading task in doInBackground() method of AsyncTask and call it using new AsyncTaskClass.execute();
You can go through it from here : http://developer.android.com/reference/android/os/AsyncTask.html
You can do something similar (instead of AsyncTask):
Edit:
Timer timer = new Timer();
while( isloading() ){
try {
timer.schedule( new TimerTask(){
public void run() {
//
}
}, delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
If you want to achieve, what i understand, that once your second line is called you want to stop there and when after 1000 ms you want to continue. You can do one thing, copy all the code after your second line and put that in run method of below code:
new Handler().postDelayed(new Runnable()
{
#Override
public void run() {
}
}, 1000);
It will execute you code after 1000ms
If you want to execute sequentially actions happening on UIThread and in backgrounds threads you should be looking for the Bolt Library by Parse/Facebook.
It apply Javascript promises to Android application.
You can go through it from here : https://github.com/BoltsFramework/Bolts-Android
My problem is pretty simple. I am creating a card based on the result of a HTTP query performed inside a separate thread. The card also has an onclick method and is defined inside a runOnUiThread() located inside the separate thread. However, when the device is tapped, the onclick event isn't fired.
Here is my code:
private void login() {
Runnable r = new Runnable() {
#Override
public void run() {
// irrelevant code
runOnUiThread(new Runnable() {
#Override
public void run() {
setContentView(buildError(code));
}
}
}
Thread t = new Thread(r);
t.start();
}
private View buildError(String code) {
CardBuilder card = new CardBuilder(this, CardBuilder.Layout.ALERT);
card.setIcon(R.drawable.ic_warning_150);
if (code.equals("1"))
card.setText("Incorrect credientals");
else
card.setText("Unexpected error");
card.setFootnote("Tap to try again");
View cView = card.getView();
cView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("Event", "Clicked"); // This is what isn't triggering
}
});
cView.setFocusable(true);
cView.setFocusableInTouchMode(true);
return cView;
}
Even though the snippet of code contains an error (can't be compiled, missing ; at the Runnable statement), you were on the right track.
The View simply needs to request the focus in order to be clickable right away. Otherwise you'll have to move the focus manually.
cView.setFocusable(true);
cView.setFocusableInTouchMode(true);
cView.requestFocus();
Reference
I have the below code:
new Thread(new Runnable(){
public void run(){
try {
pos = Calculo.Calcular();
mostrarFrases(pos);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
where mostrarFrases is:
void mostrarFrases(int pos){
Mostrar = (TextView) findViewById(R.id.Texto);
Mostrar.setText(Frases[pos*2+1], 0, Frases[pos*2+1].length);
}
It works if i haven't any thread but without it does not work. I need thread because i need to wait until Calculo.Calcular() finish its work.
Rule: You cannot manipulate UI elements outside the UI thread.
Here is the proper way to do things:
//Asuming that your activiy is named MainActivity
new Thread(new Runnable() {
public void run() {
try {
pos = Calculo.Calcular();
//Manipulate your UI elements as following
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
mostrarFrases(pos);
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
You're not allowed to touch Views on any thread other than the UI thread.
To solve this, try the following:
void mostrarFrases(final int pos){
runOnUiThread(new Runnable() {
public void run() {
Mostrar = (TextView) findViewById(R.id.Texto);
Mostrar.setText(Frases[pos*2+1], 0, Frases[pos*2+1].length);
}});
}
You should not manipulate UI elements on any thread except the main UI thread.
So, in your case you got two choices: either use runOnUIThread(Runnable), or use AsyncTask and do UI manipulations in onPostExecute().
I'd go with AsyncTask - it is intended for this kind of scenarios.
UI manipulation must be done in main/ui thread. You have two options for this case:
You have a reference to your Activity object.
myActivity.runOnUiThread(...);
Bind a handler to the main thread and:
// Main thread
Handler handler = new Handler();
// Another thread
handler.post(new Runnable(){...});
I have this method from a separate class wherein when the call ends, the color of my ImageView changes from red to white. Sample code below:
public void endOfCall(){
((Activity)mContext).runOnUiThread(new Runnable(){
#Override
public void run(){
TargetDetails.oncall.setVisibility(View.VISIBLE);
TargetDetails.endcall.setVisibility(View.GONE);
}
});
try{
call.endCall();
}catch (SipException se) {}
call.close();
//this is just a representation; not the actual code
if(true){
Thread.sleep(10000);
}
//new intent here
}
The problem starts when it goes to the 'if' condition where I put the Thread.sleep. It waits 10seconds before the code below gets executed
TargetDetails.oncall.setVisibility(View.VISIBLE);
TargetDetails.endcall.setVisibility(View.GONE);
I think I am missing something here regarding the Thread.sleep. I just wanna get rid of it but I'm not sure of any alternative aside from that. Help. Thanks.
Use Handler instead of putting thread to sleep.
So instead of your if(true) {.....} try this:
Handler h = new Handler();
h.postDelayed(new Runnable() {
#Override public void run() {
//new intent here
}
}, 10000);