Reputation: 41
I am trying to implement an infinite loop as i want the code to run the follwing code again and again...but it is causing my app to crash...as i am a beginner in this tech, i am not aware of this...any help will be appreciated..thank you..
Here is the Logcat
06-20 22:04:38.029 22848-22848/sensor.swapni E/AndroidRuntime: FATAL
EXCEPTION: main
Process:
sensor.swapni, PID: 22848
java.lang.OutOfMemoryError: Failed to allocate a 13571692 byte allocation
with 7716072 free bytes and 7MB until OOM
at
java.util.ArrayList.add(ArrayList.java:118)
at
android.view.ViewRootImpl$RunQueue.postDelayed(ViewRootImpl.java:8656)
at
android.view.View.postDelayed(View.java:14275)
at
sensor.swapni.Second.onCreate(Second.java:54)
at
android.app.Activity.performCreate(Activity.java:6904)
at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
at
android.app.ActivityThread.access$1100(ActivityThread.java:229)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at
android.os.Handler.dispatchMessage(Handler.java:102)
at
android.os.Looper.loop(Looper.java:148)
at
android.app.ActivityThread.main(ActivityThread.java:7325)
at
java.lang.reflect.Method.invoke(Native Method)
at
And here is the java code
while(true) {
tv1.postDelayed(new Runnable() {
@Override
public void run() {
tv1.setVisibility(View.INVISIBLE);
tv2.setVisibility(View.VISIBLE);
}
}, 1000);
tv1.postDelayed(new Runnable() {
@Override
public void run() {
tv2.setVisibility(View.INVISIBLE);
tv1.setVisibility(View.VISIBLE);
}
}, 2000);
Upvotes: 0
Views: 1174
Reputation: 157
You Shouldn`t use it inside main thread specially when you are doing operation inside it. You may have to use some thing like that.
viewModelScope.launch(Dispatchers.IO) {
while (statues==true){
if (statue==true){
delay(500)
//ToDo
}
}
}
Upvotes: 0
Reputation: 2386
Add these two methods to your class:
private void setVisibility1() {
tv1.setVisibility(View.INVISIBLE);
tv2.setVisibility(View.VISIBLE);
tv1.postDelayed(new Runnable() {
@Override
public void run() {
setVisibility2();
}
}, 1000);
}
private void setVisibility2() {
tv1.setVisibility(View.VISIBLE);
tv2.setVisibility(View.INVISIBLE);
tv1.postDelayed(new Runnable() {
@Override
public void run() {
setVisibility1();
}
}, 1000);
}
And replace the whole loop with one call to this method:
setVisibility1();
setVisibility1 switches the visibility and starts the timer for setVisibility2. When setVisibility2 is executed it changes the visibility again and starts the timer for setVisibility1.
Upvotes: 0
Reputation: 1794
It is very likely an out of memory issue. Try to set a timer instead of creating tons of new Object.
http://java.sun.com/javase/6/docs/api/java/util/Timer.html
Upvotes: 0
Reputation: 93589
That loop will run thousands of times per second. Each time it runs, it adds at least 4 objects to the heap (2 Runnables, 2 Messages to the handler). Eventually you run out of memory.
If you want to continually do something every 2 seconds, you call postDelayed once on the actual thread, and inside the runnable you call it again. Like this:
tv1.postDelayed(new Runnable() {
@Override
public void run() {
tv1.setVisibility(View.INVISIBLE);
tv2.setVisibility(View.VISIBLE);
tv1.postDelayed(this, 1000);
}
}, 1000);
Upvotes: 5