My app loads on the emulator but does not do what I ask it to do. I have put the code in the onResume method, where it is supposed to loop 3 times with 15 second breaks in between. It doesn't do this. Are there any methods that can do this?
To elaborate, I need a method which opens the app, displays some textviews and colors it in, waits 15 seconds, displays some more textviews and colors them in, waits 15 seconds, and displays the textviews and colors them in.
Here is my code:
Java
package com.example.ani.opener;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.Random;
import java.util.concurrent.TimeUnit;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
protected void onResume(){
super.onResume();
LinearLayout toplayout = (LinearLayout) findViewById(R.id.toplayout);
int loopthing = 1;
while(loopthing < 4){
int childnum = 0;
LinearLayout row1 = (LinearLayout) toplayout.getChildAt(0);
LinearLayout row2 = (LinearLayout) toplayout.getChildAt(1);
LinearLayout row3 = (LinearLayout) toplayout.getChildAt(2);
while(childnum <=4){
TextView spot1 = (TextView) row1.getChildAt(childnum);
spot1.setBackgroundColor(Color.WHITE);
childnum += 1;
}
int childnum2 = 0;
while(childnum2 <=4){
TextView spot2 = (TextView) row1.getChildAt(childnum2);
spot2.setBackgroundColor(Color.WHITE);
childnum2 += 1;
}
int childnum3 = 0;
while(childnum3 <=4){
TextView spot3 = (TextView) row1.getChildAt(childnum3);
spot3.setBackgroundColor(Color.WHITE);
childnum3 += 1;
}
int looper = 1;
Random initloop = new Random();
int looptime = initloop.nextInt(10-1) + 1;
while(looper <= looptime){
Random findnum = new Random();
int i = findnum.nextInt(2-0) + 0;
LinearLayout layout = (LinearLayout) toplayout.getChildAt(i);
Random spotinit = new Random();
int a = spotinit.nextInt(4-0) + 0;
TextView spot = (TextView) layout.getChildAt(a);
spot.setBackgroundColor(Color.RED);
looper += 1;
}
LinearLayout disabledrow = (LinearLayout) toplayout.getChildAt(0);
TextView disabledspot1 = (TextView) disabledrow.getChildAt(0);
TextView disabledspot2 = (TextView) disabledrow.getChildAt(1);
disabledspot1.setBackgroundColor(Color.BLUE);
disabledspot2.setBackgroundColor(Color.BLUE);
loopthing +=1;
try {
TimeUnit.SECONDS.sleep(15);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.anish.mapactivity.LotA"
android:orientation="horizontal"
android:id = "@+id/toplayout"
android:weightSum="1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/layout1"
android:orientation="vertical">
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot1"
android:layout_marginTop="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot2"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot3"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot4"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot5"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/layout2"
android:orientation="vertical">
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot6"
android:layout_marginTop="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot7"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot8"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot9"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot10"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/layout3"
android:orientation="vertical">
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot11"
android:layout_marginTop="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot12"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot13"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot14"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot15"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
</LinearLayout>
</LinearLayout>
Edit
I tried out the Handler. Here is my code. It says the app has stopped when I try to run it once. It shows up the TextViews once, and crashes after 15 seconds. Here is my code:
Java
package com.example.ani.opener;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.Random;
import java.util.concurrent.TimeUnit;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
protected void onResume() {
super.onResume();
Thread thread = new Thread() {
@Override
public void run() {
LinearLayout toplayout = (LinearLayout) findViewById(R.id.toplayout);
int loopwhile = 1;
while (loopwhile < 4) {
int childnum = 0;
LinearLayout row1 = (LinearLayout) toplayout.getChildAt(0);
LinearLayout row2 = (LinearLayout) toplayout.getChildAt(1);
LinearLayout row3 = (LinearLayout) toplayout.getChildAt(2);
while (childnum <= 4) {
TextView spot1 = (TextView) row1.getChildAt(childnum);
spot1.setBackgroundColor(Color.WHITE);
childnum += 1;
Log.d("Message", "Row 1 colored white");
}
int childnum2 = 0;
while (childnum2 <= 4) {
TextView spot2 = (TextView) row1.getChildAt(childnum2);
spot2.setBackgroundColor(Color.WHITE);
childnum2 += 1;
Log.d("Message", "Row 2 colored white");
}
int childnum3 = 0;
while (childnum3 <= 4) {
TextView spot3 = (TextView) row1.getChildAt(childnum3);
spot3.setBackgroundColor(Color.WHITE);
childnum3 += 1;
Log.d("Message", "Row 3 colored white");
}
int looper = 1;
Random initloop = new Random();
int looptime = initloop.nextInt(10 - 1) + 1;
while (looper <= looptime) {
Random findnum = new Random();
int i = findnum.nextInt(2 - 0) + 0;
LinearLayout layout = (LinearLayout) toplayout.getChildAt(i);
Random spotinit = new Random();
int a = spotinit.nextInt(4 - 0) + 0;
TextView spot = (TextView) layout.getChildAt(a);
spot.setBackgroundColor(Color.RED);
looper += 1;
}
LinearLayout disabledrow = (LinearLayout) toplayout.getChildAt(0);
TextView disabledspot1 = (TextView) disabledrow.getChildAt(0);
TextView disabledspot2 = (TextView) disabledrow.getChildAt(1);
disabledspot1.setBackgroundColor(Color.BLUE);
disabledspot2.setBackgroundColor(Color.BLUE);
loopwhile += 1;
try {
Thread.sleep(15000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
};
thread.start();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.anish.mapactivity.LotA"
android:orientation="horizontal"
android:id = "@+id/toplayout"
android:weightSum="1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/layout1"
android:orientation="vertical">
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot1"
android:layout_marginTop="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot2"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot3"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot4"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot5"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/layout2"
android:orientation="vertical">
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot6"
android:layout_marginTop="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot7"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot8"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot9"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot10"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/layout3"
android:orientation="vertical">
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot11"
android:layout_marginTop="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot12"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot13"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot14"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot15"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
</LinearLayout>
</LinearLayout>
LogCat
03-08 04:16:20.657 2655-2655/com.example.ani.opener D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
03-08 04:16:20.707 2655-2655/com.example.ani.opener I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
03-08 04:16:20.707 2655-2655/com.example.ani.opener W/dalvikvm﹕ VFY: unable to resolve virtual method 386: Landroid/content/res/TypedArray;.getType (I)I
03-08 04:16:20.707 2655-2655/com.example.ani.opener D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
03-08 04:17:06.088 2655-2655/com.example.ani.opener D/﹕ HostConnection::get() New Host Connection established 0xb7dced40, tid 2655
03-08 04:17:06.348 2655-2655/com.example.ani.opener W/EGL_emulation﹕ eglSurfaceAttrib not implemented
03-08 04:17:06.368 2655-2655/com.example.ani.opener D/OpenGLRenderer﹕ Enabling debug mode 0