[英]The application may be doing too much work on its main thread
Hi guys i'm trying to get separate activities by clicking menus from the navigation drawer, every thing seems fine but im getting 嗨,大家好,我正在尝试通过单击导航抽屉中的菜单来进行单独的活动,一切似乎都很好,但即时通讯
06-10 12:26:57.569 3457-3457/? 06-10 12:26:57.569 3457-3457 /? I/Choreographer: Skipped 68 frames! I /编舞:跳过68帧! The application may be doing too much work on its main thread. 该应用程序可能在其主线程上做太多工作。
error. 错误。 and also app is very slow. 而且应用程序非常慢。 please help me to solve the issue. 请帮助我解决问题。
MainActivity.java MainActivity.java
package gira.cdap.com.giira;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import gira.cdap.com.giira.activity.EventPlanningFragment;
import gira.cdap.com.giira.activity.HomeFragment;
import gira.cdap.com.giira.activity.LoginActivity;
import gira.cdap.com.giira.activity.ProfileFragment;
import gira.cdap.com.giira.activity.TourFragment;
import gira.cdap.com.giira.helper.SQLiteHandler;
import gira.cdap.com.giira.helper.SessionManager;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
NavigationView navigationview = null;
Toolbar toolbar=null;
private SQLiteHandler db;
private SessionManager session;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//initNavigationDrawer();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.drawer_open, R.string.drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
navigationview = (NavigationView) findViewById(R.id.navigation_view);
// SqLite database handler
db = new SQLiteHandler(getApplicationContext());
// session manager
session = new SessionManager(getApplicationContext());
View headerView = navigationview.getHeaderView(0);
navigationview.setNavigationItemSelectedListener(this);
if (!session.isLoggedIn()) {
logoutUser();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.logout) {
//Set the fragment initially
logoutUser();
Toast.makeText(getApplicationContext(), "Successfully Logout", Toast.LENGTH_SHORT).show();
}
else if (id ==R.id.home_menu){
home_activity();
}
else if (id ==R.id.profile_menu){
profile_activity();
}
else if (id ==R.id.tour_menu){
tour_activity();
}
else if (id ==R.id.event_plan_menu){
event_activity();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private void logoutUser() {
session.setLogin(false);
db.deleteUsers();
// Launching the login activity
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
private void profile_activity(){
Intent intent = new Intent(this, ProfileFragment.class);
startActivity(intent);
finish();
}
private void tour_activity(){
Intent intent = new Intent(this, TourFragment.class);
startActivity(intent);
finish();
}
private void event_activity(){
Intent intent = new Intent(this, EventPlanningFragment.class);
startActivity(intent);
finish();
}
private void home_activity(){
Intent intent = new Intent(this, HomeFragment.class);
startActivity(intent);
finish();
}
}
activity_main activity_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout"
android:layout_width="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" tools:openDrawer="start">
<include layout="@layout/app_bar_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView android:id="@+id/navigation_view"
android:layout_width="wrap_content" android:layout_height="match_parent"
android:layout_gravity="start" android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header" app:menu="@menu/menu_navigation" />
</android.support.v4.widget.DrawerLayout>
that's because your launching your activity in onNavigationItemSelected during the menu closing. 那是因为您在菜单关闭期间在onNavigationItemSelected中启动了活动。 I suggest you to launch your activity after the menu is closed. 建议您在菜单关闭后启动活动。
private static final int NAVDRAWER_ANIM_DURATION = 250;
public void goToActivity(Class<?> mClass){
closeNavigationView(); // in this method you can close your drawer
Intent intent = new Intent(this, mClass);
new Handler().postDelayed(new ActivityTransitionRunnable(intent), NAVDRAWER_ANIM_DURATION);
}//goToActivity
private class ActivityTransitionRunnable implements Runnable {
Intent mIntent;
private ActivityTransitionRunnable(Intent intent) {
mIntent = intent;
}
public void run() {
startActivity(mIntent);
}
}//ActivityTransitionRunnable
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.