java代码如下
package com.example.changecolors;import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;public class FrameLayoutTest extends Activity {
private int current = 0;
final int[] colors = new int[] { R.color.color1, R.color.color2,
R.color.color3, R.color.color4, R.color.color5, R.color.color6,
R.color.color7 };
final int[] names = new int[] { R.id.view1, R.id.view2, R.id.view3,
R.id.view4, R.id.view5, R.id.view6, R.id.view7 };
TextView view[] = new TextView[7]; public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
for (int i = 0; i < 7; i++) {
view[i] = (TextView) findViewById(names[i]);
}
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == 0x1122) {
for (int i = 0; i < 7 - current; i++) {
view[i].setBackgroundResource(colors[i + current]);
}
for (int i = 7 - current, j = 0; j < 7; i++, j++) {
view[i].setBackgroundResource(colors[j]);
}
}
super.handleMessage(msg);
}
};
new Timer().schedule(new TimerTask() { @Override
public void run() {
// TODO Auto-generated method stub
current++;
if (current >= 6) {
current = 0;
}
Message m = new Message();
m.what = 0x1122;
handler.sendMessage(m);
}
}, 0, 100);
}
}
xml配置如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >    <TextView
        android:id="@+id/view1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        android:height="50px"
        android:width="210px" />    <TextView
        android:id="@+id/view2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#bb0000"
        android:height="50px"
        android:width="180px" />    <TextView
        android:id="@+id/view3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#dd0000"
        android:height="50px"
        android:width="150px" />    <TextView
        android:id="@+id/view4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#990000"
        android:height="50px"
        android:width="120px" />    <TextView
        android:id="@+id/view5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#770000"
        android:height="50px"
        android:width="90px" />    <TextView
        android:id="@+id/view6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#550000"
        android:height="50px"
        android:width="60px" />    <TextView
        android:id="@+id/view7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#330000"
        android:height="50px"
        android:width="30px" /></FrameLayout>
Manifest的如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.changecolors"
    android:versionCode="1"
    android:versionName="1.0" >    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="14" />    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.changecolors.FrameLayoutTest"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application></manifest>Android报错

解决方案 »

  1.   

    同上,35行数组越界了。
    for (int i = 7 - current, j = 0; j < 7; i++, j++) {
    可能是这个current是0.
      

  2.   

    current最小是1 不行吗?试了,没用
      

  3.   

    修改代码
    @Override
    public void run() {
    // TODO Auto-generated method stub
    if (current >= 6) {
    current = 0;
    }
    current++;
    Message m = new Message();
    m.what = 0x1122;
    handler.sendMessage(m);
    }
    }, 0, 100);
    }
    }
      

  4.   

    还是报错for (int i = 7 - current, j = 0; j < 7 && i<7 ; i++, j++) {
    view[i].setBackgroundResource(colors[j]);
    }
      

  5.   

    for (int i = 7 - current, j = 0; j < 7; i++, j++) {
    view[i].setBackgroundResource(colors[j]);
    }
    是i--吧?for (int i = 7 - current, j = 0; j < 7; i--, j++) {
    view[i].setBackgroundResource(colors[j]);
    }
      

  6.   

    还是报错for (int i = 7 - current, j = 0; j < 7 && i<7 ; i++, j++) {
    view[i].setBackgroundResource(colors[j]);
    }

    可以了,3Q