我试图每秒更改背景,这是我的代码: package com.example.splasher;
import java.lang.reflect.Field;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Typeface;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
public class Views extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.view);
final ScrollTextView scrolltext=(ScrollTextView) findViewById(R.id.scrolltext);
if(MainActivity.bold.isChecked())
{
scrolltext.setTypeface(null, Typeface.BOLD);
};
if(MainActivity.italic.isChecked())
{
scrolltext.setTypeface(null, Typeface.ITALIC);
};
if((MainActivity.italic.isChecked())&&(MainActivity.bold.isChecked()))
{
scrolltext.setTypeface(null, Typeface.BOLD_ITALIC);
};
scrolltext.setTextSize(Integer.parseInt(MainActivity.tSize[MainActivity.Size.getSelectedItemPosition()]));
scrolltext.setText(MainActivity.text);
scrolltext.setTextColor(Integer.parseInt(MainActivity.colour[MainActivity.TextColour.getSelectedItemPosition()]));
scrolltext.startScroll();
scrolltext.setBackgroundColor(Integer.parseInt(MainActivity.colour[MainActivity.BackgroundColour.getSelectedItemPosition()]));
Thread thread = new Thread()
{
public void run()
{
if(MainActivity.random.isChecked())
{
int delay = 0; // delay for 5 sec.
int period = 1000; // repeat every sec.
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
int n=1;
public void run() {if (n==9)n=1;
scrolltext.setBackgroundColor(Integer.parseInt(MainActivity.colour[n]));
}
}, delay, period);
/*int n=1;
boolean constant=true;
while (constant==true){
if (n==10) n=1;
// int randInt = new Random().nextInt(2);
// scrolltext.setBackgroundColor(Integer.parseInt(MainActivity.colour[randInt]));
scrolltext.setBackgroundColor(Integer.parseInt(MainActivity.colour[n]));
n=n+1;
try
{
Thread.sleep(2000); // 1 second
} catch (Exception e)
{
e.printStackTrace();
}
}*/
}
}
};
thread.start();
// TextView textview=(TextView) findViewById (R.id.textView1);
// textview.setText(MainActivity.text);
// textview.setTextColor(Integer.parseInt(MainActivity.colour[MainActivity.TextColour.getSelectedItemPosition()]));
// textview.setBackgroundColor(Integer.parseInt(MainActivity.colour[MainActivity.BackgroundColour.getSelectedItemPosition()]));
// textview.setTextSize(Integer.parseInt(MainActivity.tSize[MainActivity.Size.getSelectedItemPosition()]));
// textview.setSelected(true);
}
}
但是力量关闭了。 Logcat显示:FATAL EXCEPTION:Timer-0; android.view.ViewRootŲcalledfromwrongThreadException:只有创建视图层次结构的原始威胁才能触及其视图。 这段代码有什么问题?
答案 0 :(得分:2)
您不能在与所谓的scrolltext.setBackgroundColor()
分开的线程中操作UI元素(如调用UI thread
)(基本上是相同的线程onCreate()
。< / p>
要解决您的问题,请使用Handler。创建一个类字段:
Handler handler = new Handler();
在你的线程中,将所有UI操作放在Handler调用中:
handler.post(new Runnable() {
@Override
public void run () {
// All UI operations, like scrolltext.setBackgroundColor()
}
});
答案 1 :(得分:1)
你的问题是
scrolltext.setBackgroundColor(Integer.parseInt(MainActivity.colour[n]));
您正在从工作线程更新UI
(它不在UI线程上运行),这是不允许的。只有主(UI)线程可以使用UI进行操作。
因此您需要将其更改为runOnUiThread()
或Handler
。
runOnUiThread(new Runnable(){
public void run() {
// do your stuff
}
});
答案 2 :(得分:0)
异常消息似乎表明视图只能在主线程上更改。当您运行的代码改变了新线程上的视图时,它不喜欢它。
您是否了解Android中使用的主要Event-Dispatcher线程?类似于在EDT上更新SWINGs GUI的方式。如果是这样,请寻找告诉主绘图线程进行最终更新的方法。
在摇摆中,它看起来像是:
EventQueue.invokeLater(new Runnable(){
public void run(){
}
}
希望这有一定意义。