application stops running and exit when press the button in android studio - android

cancelBtn = (Button) findViewById(R.id.btnCncl);
cancelBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
countDownTimer.cancel();
//txtViewOff.setText("");
txtViewOn.setText("Countdown Timer Canceled");
//Timer to set visible text view
Timer t = new Timer(false);
t.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
txtViewOn.setText("");
}
});
}
}, 3000);
}
});
when i click on the button related to this code the app get crashes..i made this to cancel the timer

I have no clear idea about this without a crash log. But according to this code most probably it may be a null pointer exception. try below thing by adding a null check before call countdowntimer
if(countDownTimer != null){
countDownTimer.cancel();
}

Related

android code to click button multiple times using for loop

can any 1 give me example to perform onClick on button for multiple times when user clicked for 1 time. when i click on button 1 time it should automatically click after delay of 5 seconds for 100 times. how to perform that.
This is my sample code
mUnlock.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//It should be already ensured that this mSelectedLock is something user is authorized to access
if (mSelectedLock.unlock("RANDOM")) {
mUnlock.setVisibility(View.INVISIBLE);
mUnlock.postDelayed(new Runnable() {
public void run() {
mUnlock.setVisibility(View.VISIBLE);
}
}, 5000);
} else {
Toast.makeText(MainActivity.this, "Unable to unlock.", Toast.LENGTH_LONG).show();
}
}
});
#Override
public void onClick(View v) {actionToBeDone();startLoop(0);}
private void startLoop(final int i) {
if(i!=100) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Log.e("i",""+i);
actionToBeDone();
startLoop(i+1);
}
}, 2000);
}
}
private void actionToBeDone() {
//enter actions you want to be done
Log.e("actionToBeDone","Button Action");
}
int count = 0;
Timer timer = new Timer();
timer.schedule(new TimerTask(){
#Override
public void run(){
if(count < 100){
mUnlock.performClick();
}
}
}, 0, 5000);
5000 is the time in miliseconds you can +/- from here.

How I make an ImageView show-up immediatlly

public class MainActivity extends Activity {
ImageView img;
MediaPlayer failure;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bButton= (Button)findViewById(R.id.blueBtn);
img = (ImageView)findViewById(R.id.image);
img.setVisibility(View.VISIBLE);
failure= MediaPlayer.create(this,R.raw.failure_sound);
bButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
img.setVisibility(View.VISIBLE);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
failure.start();
}
});
}
well thats my code.
I just want to make that when I press the button it'll show the image
then wait 1000 miliseconds, and then make a sound.
BUT(!) unfortunaltly when I press that button: the process waits 1000 ms, and then make the sound and shows the img.
help plz!!!
Replace your onClick handler:
public void onClick(View v){
img.setVisibility(View.VISIBLE);
v.postDelayed(new Runnable() {
public void run() { failure.start(); }
},1000);
}
With Thread.sleep(10000); you are causing the main thread to freeze and thus the UI cannot be updated for that amount of time.
If you want to wait for a certain amount of time to do something, you may use a Handler instead, so the UI can be updated in the meantime:
public void onClick(View v){
img.setVisibility(View.VISIBLE);
new Handler().postDelayed(new Runnable() {
public void run() {
failure.start();
}
}, 1000);
}
You're putting your UI thread to sleep, which causes your app to freeze and the image not showing up.
A better approach for this is to use a handler, which will execute code after a delay and will not freeze your UI.
A quick adaption of your clickListener would be:
bButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
img.setVisibility(View.VISIBLE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
failure.start();
}
}, 1000);
});
}

How make a button invisible for 1 or 2 second on another button click

In my app I want to make a button invisible for a few seconds after another button has been pressed and then it should become visible again.
How it is possible?
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn.setVisibility(View.INVISIBLE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
btn.setVisibility(View.VISIBLE);
}
}, 1000); // where 1000 is equal to 1 sec (1 * 1000)
}
});
You can do somthing like this:
firstBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
secondBtn.setVisibility(View.INVISIBLE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
secondBtn.setVisibility(View.VISIBLE);
}
}, 2000); //change it for the time you need in milliseconds
}
});
must make buttonView invisible then use btnView.postDelayed
Just inside onClick of second button just do
secondButtonView.setVisibility(View.INVISIBLE);
secondButtonView.postDelayed(new Runnable() {
#Override
public void run() {
secondButtonView.setVisibility(View.VISIBLE);
}
}, 2000);
View.postDelayed() simply calls Handler.postDelayed(). It's a
convenient method that helps avoid creating Handler instances.
This quote is from Romain Guy Android framework engineer https://groups.google.com/forum/#!topic/android-developers/IuG3HgKx89Q
//my button invisible
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// Do something after 5s = 5000ms
//my button visible
}
}, 5000);
U can use handler for it
also u can use Timer and TimerTask
//First button invisible
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// Second visible
// after some MS
}
}, 2000);
Suppose you have two buttons Button button1,button2 properly inflated and displayed in view. You can change visibility of button2 on click of button1 by:
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
button2.setVisibility(View.INVISIBLE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
button2.setVisibility(View.VISIBLE);
}
}, 2 * 1000);//number of seconds *1000
}
});
try this,
write following code on another button's click event.
continuebutton.setVisibility(View.INVISIBLE);
continuebutton.postDelayed(new Runnable() {
public void run() {
continuebutton.setVisibility(View.VISIBLE);
}
}, 2000);

NullPointerException at Timer

I get this error. not on any device, just on some.
This is all the report:
java.lang.NullPointerException
at eu.innovaapps.tpark.ro.MainSms$4$1$1.run(MainSms.java:444)
at java.util.Timer$TimerImpl.run(Timer.java:284)
and it comes from:
buton.setEnabled(false);
Timer buttonTimer = new Timer();
buttonTimer.schedule(new TimerTask() {
#Override
public void run() {
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
buton.setEnabled(true);
}
});
}
}, 5000);
When i press the button it gets disabled and after 5 seconds it gets enabled again.
Line 444 is getActivity().runOnUiThread(new Runnable() {.
I'd appreciate alot if you could help me solve this error.
EDIT:
I got the same problem as here:
Scheduled Task in Fragment returns getActivity as null
If the button in disabled, and i press the back button of the phone, i get this error which sends me at this part of the code.
Is there a way i could stop the timer if the back button is pressed?
Can you please tell what is the line number 444 in your code ?
If line 444 is buton.setEnabled(true); then can you modify the code to add Button buton = (Button) findViewById(R.id. just before buton.setEnabled(true); and then try out the same code.
EDIT
Dear Victor,
I have read your EDIT... I used to get such exception... To go around this, what I have done is that I declared and static variable in my Activity class...
public static sActivity = null;
and then in onCreate method of Activity I assigned the current activity to it...
sActivity = this;
and then used it in the code instead of getActivity()...
So for e.g. my Activity name is myActivity then your code in that case could changed to...
#Override
public void run() {
getActivity().runOnUiThread(new Runnable()
#Override
public void run() {
if (myActivity.sActivity != null) {
myActivity.sActivity.runOnUiThread(new Runnable()
.
.
.
}
Let me know if you find any other better solution...
Timer notificationtask = new Timer();
notificationtask.schedule(new TimerTask()
{
#Override
public void run() {
// TODO Auto-generated method stub
runOnUiThread(new Runnable() {
#Override
public void run() {
try {
buton.setEnabled(true);
} catch (Exception e) {
}
}
});
}
}, 1000, 1000);

Autoclick in Android

I'm developing an Android application in which I have different buttons.
In particular there is one that, in addition to the user whenever pressed, I want "autoclick" every X seconds.
You can use ScheduledExecutorService to create timer and autoclicker like that.
private void yourFunction(){
//whatever you want
}
yourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
yourFunction();
}
});
ScheduledExecutorService scheduleTaskExecutor= Executors.newScheduledThreadPool(1);
scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
public void run() {
yourFunction();
}
}, 0, YourSeconds, TimeUnit.SECONDS);
and you should close ScheduledExecutorService in your activity's onDestroy method like that.
public void onDestroy() {
super.onDestroy();
if (scheduleTaskExecutor != null)
scheduleTaskExecutor.shutdownNow();
}

Categories

Resources