onResume和onCreate

时间:2015-03-08 08:27:29

标签: java android onresume

我的应用程序在模拟器上加载但不执行我要求它执行的操作。我把代码放在onResume方法中,它应该循环3次,中间间隔15秒。它没有这样做。有没有什么方法可以做到这一点?

详细说明,我需要一个方法来打开应用程序,显示一些文本视图和颜色,等待15秒,显示更多的文本视图和颜色,等待15秒,并显示文本视图和颜色。< / p>

这是我的代码:

爪哇

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>

修改

我试过了Handler。这是我的代码。它说,当我尝试运行一次时,应用已停止。它显示TextViews一次,并在15秒后崩溃。这是我的代码:

爪哇

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

2 个答案:

答案 0 :(得分:0)

您无法阻止onResume方法15秒。 它在MainThread上运行。

例如,您可以使用AsyncTask更新ui,也可以将方法用作ViewTreeObserver.OnGlobalLayoutListenerHandler

示例:

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
       //Update UI 
    }
}, 15000);

您可以找到更多信息here

例2:

 ViewTreeObserver vto=mActionCustomView.getViewTreeObserver();
  vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
    @Override public void onGlobalLayout(){

       mActionCustomView.getViewTreeObserver()
             .removeGlobalOnLayoutListener(this);
       //....
       runnable.run();
    }
  }

更多信息here

答案 1 :(得分:0)

您需要在15秒后运行的处理程序中编写代码

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
       //Update UI here
    }
}, 15000);