我得到Choreographer错误应用程序可能在其主线程上做了太多工作。 在我的主要活动中,每次我启动应用程序或返回主要活动。
根据我的理解,在一个核心上完成了许多工作(因为我没有使用线程),所以我必须找到一个可能会发展线程和AsyncTask的解决方案。
我试图弄清楚cpu究竟是什么原因造成他的努力,但我自己无法解决这个问题。
这里是包含导航抽屉的主要活动的代码:
public class MainActivity extends ActionBarActivity
implements View.OnClickListener {
private Animation animAlpha;
private Animation animRotate;
private MediaPlayer background_music;
static int is_login = 0;
private boolean isOpen = false;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private ViewFlipper flipper;
ImageButton messages,weekly_day_times,shabat_times,events;
private ViewGroup mContainerView1,mContainerView2,mContainerView3,mContainerView4;
@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);
background_music = MediaPlayer.create(this, R.raw.yedid_nefesh);
flipper = (ViewFlipper) findViewById(R.id.viewFlipper);
startFlipper();
background_music.start();
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);
messages.setOnClickListener(this);
weekly_day_times.setOnClickListener(this);
shabat_times.setOnClickListener(this);
events.setOnClickListener(this);
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);
findViewById(R.id.b_shabat_add).setOnClickListener(this);
findViewById(R.id.b_weekly_add).setOnClickListener(this);
findViewById(R.id.b_messages_add).setOnClickListener(this);
findViewById(R.id.b_events_add).setOnClickListener(this);
IntializeNavDrawer();
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
setButtonsAnimation(messages);
setButtonsAnimation(weekly_day_times);
setButtonsAnimation(shabat_times);
setButtonsAnimation(events);
}
private void setButtonsAnimation(ImageButton button) {
int rand = (int)(Math.random()*700);
Animation hyperspaceJump = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
hyperspaceJump.setStartOffset(rand);
button.startAnimation(hyperspaceJump);
}
/*
Initialize all list views in navigation drawer.
Handles actions of open or close drawer.
*/
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);
}
/*
Initialize properties and start the background flipper
*/
private void startFlipper() {
flipper.setFlipInterval(5000);
flipper.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
flipper.startFlipping();
}
@Override
protected void onResume() {
super.onResume();
flipper.startFlipping();
background_music.start();
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle(mTitle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onClick(View v) {
Intent i;
switch(v.getId())
{
case R.id.messages_button: {
i = new Intent(com.example.neotavraham.MainActivity.this, Messages.class);
startActivity(i);
return;
}
case R.id.sat_times_button: {
i = new Intent(com.example.neotavraham.MainActivity.this, ShabatPrays.class);
startActivity(i);
return;
}
case R.id.weekly_times_button: {
i = new Intent(com.example.neotavraham.MainActivity.this, WeeklyPrays.class);
startActivity(i);
return;
}
case R.id.events_button: {
i = new Intent(com.example.neotavraham.MainActivity.this, Events.class);
startActivity(i);
return;
}
case R.id.b_shabat_add:
if(isOpen){
removeItem(mContainerView1);
isOpen = false;
}else {
addItem(mContainerView1,R.layout.nav_drawer_l_changes_item_1);
isOpen =true;
}
return;
case R.id.b_weekly_add:
if(isOpen){
removeItem(mContainerView2);
isOpen = false;
}else {
addItem(mContainerView2,R.layout.nav_drawer_l_changes_item_1);
isOpen =true;
}
return;
case R.id.b_messages_add:
if(isOpen){
removeItem(mContainerView3);
isOpen = false;
}else {
addItem(mContainerView3,R.layout.nav_drawer_l_changes_item_2);
isOpen =true;
}
return;
case R.id.b_events_add:
if(isOpen){
removeItem(mContainerView4);
isOpen = false;
}else {
addItem(mContainerView4,R.layout.nav_drawer_l_changes_item_2);
isOpen =true;
}
return;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
@SuppressLint("NewApi")
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return false;
}
@Override
protected void onStop() {
super.onStop();
background_music.pause();
flipper.stopFlipping();
}
/**** Method for Setting the Height of the ListView dynamically.
**** Hack to fix the issue of not showing all the items of the ListView
**** when placed inside a ScrollView ****/
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
if (i == 0) {
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, AbsListView.LayoutParams.WRAP_CONTENT));
}
view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
private void addItem(ViewGroup mContainerView,int layout) {
// Instantiate a new "row" view.
final ViewGroup newView = (ViewGroup) LayoutInflater.from(this).inflate(
layout, mContainerView, false);
// Because mContainerView has android:animateLayoutChanges set to true,
// adding this view is automatically animated.
mContainerView.addView(newView, 2);
}
public void removeItem(ViewGroup mContainerView){
mContainerView.removeViewAt(2);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
此处还有以下导航栏的说明:
我希望你能告诉我这个错误是什么,以及我应该如何克服它的一个例子。
谢谢
答案 0 :(得分:0)