The application may be doing too much work on its main thread
处于活动状态时,偶尔会出现此错误MainActivity
...我正在使用线程,asyncTasks和服务但仍出现错误..
这是MainActivity
代码(其中包含NavigationDrawer
):
/*
* Declerations are here
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//animAlpha = AnimationUtils.loadAnimation(this, R.anim.anim_alpha);
//animRotate = AnimationUtils.loadAnimation(this, R.anim.anim_rotate);
i_music = new Intent(this,PlayMusicService.class);
i_music.putExtra("action", "com.example.neotavraham.PLAY");
startService(i_music);
music_flag = 1;
//background_music = MediaPlayer.create(this, R.raw.yedid_nefesh);
flipper = (ViewFlipper) findViewById(R.id.viewFlipper);
flipper_thread = new Flipper(flipper);
flipper_thread.execute();
//background_music.start();
Thread process_buttons = new Thread(){
@Override
public void run(){
messages = (ImageButton) findViewById(R.id.messages_button);
weekly_day_times = (ImageButton) findViewById(R.id.weekly_times_button);
shabat_times = (ImageButton) findViewById(R.id.sat_times_button);
events = (ImageButton) findViewById(R.id.events_button);
}
};
Thread process_containers = new Thread(){
@Override
public void run(){
mContainerView1 = (ViewGroup) findViewById(R.id.add_shabat_container);
mContainerView2 = (ViewGroup) findViewById(R.id.add_weekly_container);
mContainerView3 = (ViewGroup) findViewById(R.id.add_messages_container);
mContainerView4 = (ViewGroup) findViewById(R.id.add_events_container);
mContainerView5 = (ViewGroup) findViewById(R.id.add_friday_night_container);
mContainerView6 = (ViewGroup) findViewById(R.id.add_friday_morning_container);
}
};
Thread process_buttons_click_listener = new Thread(){
@Override
public void run(){
messages.setOnClickListener(MainActivity.this);
weekly_day_times.setOnClickListener(MainActivity.this);
shabat_times.setOnClickListener(MainActivity.this);
events.setOnClickListener(MainActivity.this);
}
};
Thread process_nav_click_listener = new Thread(){
@Override
public void run(){
findViewById(R.id.b_shabat_add).setOnClickListener(MainActivity.this);
findViewById(R.id.b_weekly_add).setOnClickListener(MainActivity.this);
findViewById(R.id.b_messages_add).setOnClickListener(MainActivity.this);
findViewById(R.id.b_events_add).setOnClickListener(MainActivity.this);
findViewById(R.id.b_friday_night_add).setOnClickListener(MainActivity.this);
findViewById(R.id.b_friday_morning_add).setOnClickListener(MainActivity.this);
findViewById(R.id.b_shabat_delete_all).setOnClickListener(MainActivity.this);
findViewById(R.id.b_friday_night_delete_all).setOnClickListener(MainActivity.this);
findViewById(R.id.b_friday_morning_delete_all).setOnClickListener(MainActivity.this);
findViewById(R.id.b_weekly_delete_all).setOnClickListener(MainActivity.this);
findViewById(R.id.b_messages_delete_all).setOnClickListener(MainActivity.this);
findViewById(R.id.b_events_delete_all).setOnClickListener(MainActivity.this);
findViewById(R.id.soundToggleButton).setOnClickListener(MainActivity.this);
}
};
process_buttons.start();
process_containers.start();
process_buttons_click_listener.start();
process_nav_click_listener.start();
IntializeNavDrawer();
}
private void IntializeNavDrawer() {
mTitle = mDrawerTitle = getTitle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
null, R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
/*
*more irrelevant funcs here..
*/
这也是背景音乐的服务:
public class PlayMusicService extends Service {
public static final String ACTION_PLAY = "com.example.neotavraham.PLAY";
MediaPlayer mMediaPlayer = null;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.getStringExtra("action").equals(ACTION_PLAY)) {
mMediaPlayer = MediaPlayer.create(this, R.raw.yedid_nefesh);
mMediaPlayer.setLooping(true); // Set looping
mMediaPlayer.start();
}
return flags;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
mMediaPlayer.stop();
mMediaPlayer.release();
}
}
这是鳍状肢的类:
public class Flipper extends AsyncTask<String,String,String> {
private final ViewFlipper vFlipper;
public Flipper(ViewFlipper vFlipper){
this.vFlipper = vFlipper;
}
@Override
protected String doInBackground(String... params) {
startFlipper();
return null;
}
private void startFlipper() {
vFlipper.setFlipInterval(5000);
//flipper.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
//flipper.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
vFlipper.startFlipping();
}
}
我在我自己的手机上测试了这个代码,我的Galaxy s4,所以这不是一个慢模拟器的问题......
任何想法我应该做些什么?
答案 0 :(得分:1)
Android Service
正在UI thread
上运行,所以如果你想做太多工作或使用默认情况下在工作线程上运行的IntentService
,你必须创建自己的线程但它是一次做一份工作,所以它只有一个线程。如果你想拥有多个线程,你必须使用普通服务并创建自己的线程。阅读:Services
首先,像活动一样,服务中的所有工作都是在一个 默认情况下是单线程 - 事实上,如果你正在运行一个活动和一个 来自同一个应用程序的服务,他们使用相同的线程(“主要 线程“)默认情况下。因此,服务需要处理传入 快速意图并且在响应时从不执行冗长的计算 给他们。如果需要任何繁重的工作或阻止呼叫,您必须这样做 这些任务是异步的:要么是你实现的另一个线程 你自己,或者使用框架的许多异步工具 处理