dimanche 8 mars 2015

onResume and onCreate


Vote count:

0




It's me again with another question. 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://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4" 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>


Please do not get mad at me or anything like that. Please try and respond ASAP. I need the fix code in another 12 hours. Thank you in advance.


Regards, Ani



asked 17 secs ago







onResume and onCreate

Aucun commentaire:

Enregistrer un commentaire