在恢复和创建



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

为了详细说明,我需要一种方法来打开应用程序,显示一些文本视图并为其着色,等待 15 秒,显示更多文本视图并为其着色,

等待 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.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>

编辑

我尝试了处理程序。这是我的代码。它说当我尝试运行一次应用程序时,该应用程序已停止。它显示一次文本视图,并在 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>

日志猫

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
您需要

在处理程序中编写代码,该代码将在 15 秒后运行

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

不能在 15 秒内阻止 onResume 方法。它在主线程上运行。

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

例:

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

您可以在此处找到更多信息。

示例 2:

 ViewTreeObserver vto=mActionCustomView.getViewTreeObserver();
  vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
    @Override public void onGlobalLayout(){
       mActionCustomView.getViewTreeObserver()
             .removeGlobalOnLayoutListener(this);
       //....
       runnable.run();
    }
  }

更多信息在这里。

相关内容

  • 没有找到相关文章

最新更新