Note that there are some explanatory texts on larger screens.

plurals
  1. POTaking a Nap: Using Threads and 'Waits'/Sleep in Android Activities
    primarykey
    data
    text
    <p>Ok, I am making a basic app where there is a picture of a potato that will grow and shrink by the application editing the padding of the element. I am really new to Threads and I am for sure doing something wrong with the sleep/waits.</p> <p>Here is my code; it is relatively self explanatory:</p> <pre><code>package com.Potato.growingpotato; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.view.Menu; import android.view.View; import android.widget.ImageView; public class GrowScreen extends Activity { private Thread thread; private Handler handler = new Handler(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_grow_screen); thread = new Thread() { public void run() { growPotato(); handler.postDelayed(this, 1000); } }; thread.start(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_grow_screen, menu); return true; } @Override public void onResume(){ super.onResume(); handler.removeCallbacks(thread); handler.postDelayed(thread, 0); } public void HarvestPotato(View view) { ImageView ThePicture = (ImageView) findViewById(R.id.imageView); ThePicture.setPadding(150,150,150,150); thread.start(); } public void NapTime(int Time) { //Try to make the application sleep for 'Time' try { Thread.sleep(Time); } catch (Exception e) { e.getLocalizedMessage(); } } public void growPotato() { //Fancy Padding manipulation long base = System.currentTimeMillis(); long WorkingNum = 0; ImageView PotatoPicture = (ImageView) findViewById(R.id.imageView); do { NapTime(100); WorkingNum = 150 - ((System.currentTimeMillis() - base)/100); PotatoPicture.setPadding((int)WorkingNum, (int)WorkingNum, (int)WorkingNum, (int)WorkingNum); } while(WorkingNum &gt; 5 ); } @Override protected void onPause() { super.onPause(); handler.removeCallbacks(thread); } } </code></pre> <p>The application freezes after starting and it is a white screen. Eventually, the picture pops up at full size and so does the button; when the button is pressed, it freezes completely</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload