首页 文章

应用程序可能在其主线程上做了太多工作

提问于
浏览
1

嗨伙计们,我试图通过点击导航抽屉里的菜单来获得单独的活动,每件事似乎都很好,但即时通讯

06-10 12:26:57.569 3457-3457 /? I / Choreographer:跳过68帧!应用程序可能在其主线程上做了太多工作 .

错误 . 而app也很慢 . 请帮我解决这个问题 .

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

<?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>

1 回答

  • 2

    那是因为你在菜单关闭期间在onNavigationItemSelected中启动了你的活动 . 我建议您在菜单关闭后启动您的活动 .

    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
    

相关问题