当我按下搜索按钮时,它会产生意外停止的问题。
public class beautiful extends Activity {
ImageView radar;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.beautiful);
radar = (ImageView) findViewById(R.id.radar);
Button search = (Button) findViewById(R.id.magnifier);
ImageView text = (ImageView) findViewById(R.id.text);
MediaPlayer siren = MediaPlayer.create(this, R.raw.siren);
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Thread counter = new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
try{
Thread.sleep(2000);
radar.setImageResource(R.drawable.radar_new_full);
Thread.sleep(3000);
radar.setImageResource(R.drawable.radar_new_50);
Thread.sleep(2000);
radar.setImageResource(R.drawable.radar_new_found);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
}
}
});
counter.start();
}
});
}
以下是LogCat警告:
07-07 17:36:17.298:WARN / dalvikvm(274):threadid = 15:线程退出,未捕获异常(group = 0x4001b188) 07-07 17:36:17.306:ERROR / AndroidRuntime(274):未捕获的处理程序:由于未捕获的异常而导致线程-8退出 07-07 17:36:17.306:ERROR / AndroidRuntime(274):android.view.ViewRoot $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触及其视图。 07-07 17:36:17.306:ERROR / AndroidRuntime(274):在android.view.ViewRoot.checkThread(ViewRoot.java:2683) 07-07 17:36:17.306:ERROR / AndroidRuntime(274):在android.view.ViewRoot.requestLayout(ViewRoot.java:557) 07-07 17:36:17.306:ERROR / AndroidRuntime(274):在android.view.View.requestLayout(View.java:7918) 07-07 17:36:17.306:ERROR / AndroidRuntime(274):在android.view.View.requestLayout(View.java:7918) 07-07 17:36:17.306:ERROR / AndroidRuntime(274):在android.view.View.requestLayout(View.java:7918) 07-07 17:36:17.306:ERROR / AndroidRuntime(274):在android.view.View.requestLayout(View.java:7918) 07-07 17:36:17.306:ERROR / AndroidRuntime(274):在android.widget.ImageView.setImageResource(ImageView.java:271) 07-07 17:36:17.306:ERROR / AndroidRuntime(274):at example.beautiful $ 1 $ 1.run(beautiful.java:46) 07-07 17:36:17.306:ERROR / AndroidRuntime(274):at java.lang.Thread.run(Thread.java:1096)
答案 0 :(得分:1)
尝试删除 public void run()上方的 @Override 。
答案 1 :(得分:1)
阅读ANR对话link以及如何从其他帖子Painless Threading更新用户界面
答案 2 :(得分:1)
在这里或那里放置一些打印语句或断点,无论你喜欢哪个。 追踪导致错误的那一行,尝试在其周围放置try-catch语句以清楚地了解错误是什么 - 然后更容易解决错误(Shortly debug)
答案 3 :(得分:1)
你可以试试这个:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
//do sth...
}