我有一个将我的活动与导航抽屉连接起来的应用程序。问题是某些活动的加载比其他活动更困难(当我按抽屉上的图标时,而不是立即打开活动,它会停留1-2秒然后加载活动)。这可能是一个虚拟的问题,但是是否可以给我一些有关如何解决它的建议?这是一项工作难度更大的活动
public class ImportAPI extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
public TextView fullnameside, emailside;
public static String stravaToken;
public ImageButton btnStrava;
public ImageView tickStrava;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.importapi);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
toolbar.setTitle("");
toolbar.setSubtitle("");
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View headerView = navigationView.getHeaderView(0);
fullnameside = (TextView) headerView.findViewById(R.id.fullnameside);
emailside = (TextView) headerView.findViewById(R.id.emailside);
fullnameside.setText(""+GetInfo.fullname);
emailside.setText(""+GetInfo.email);
navigationView.setNavigationItemSelectedListener(this);
navigationView.getMenu().getItem(3).setChecked(true);
//STRAVA
tickStrava=(ImageView) findViewById(R.id.tickStrava);
btnStrava=(ImageButton) findViewById(R.id.stravaBtn);
connectStrava();
}
public void connectStrava(){
btnStrava.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent getStravaApi= new Intent(ImportAPI.this,StravaSetupApi.class);
startActivity(getStravaApi);
}
});
//GET ACCESS TOKEN FROM STRAVAS AUTHORIZE ACCOUNT
String accessToken = StravaAuthenticateActivity.getStravaAccessToken(this);
stravaToken=accessToken; //make static var so i can use it anywhere i want
//Get athletes activities from GetStravaAthleteActivities.java
new GetStravaAthleteActivities.AthleteActivities();
//check if token is null so i can display the tick and also disable the button press
if(stravaToken!=null)
{
btnStrava.setEnabled(false);
tickStrava.setVisibility(View.VISIBLE);
}
else {
tickStrava.setVisibility(View.INVISIBLE);
}
}
答案 0 :(得分:1)
尝试检查是否是活动的错误,而不是抽屉菜单的错误。您可能会计时onnectStrava()方法,这可能会导致延迟。在方法的开头和结尾处添加日志消息。
public void connectStrava(){
Log.d(“TAG”, “STRAVA Entry point);
…
Log.d(“TAG”, “STRAVA Exit point);
}
然后检查IDE Logcat上的时间戳,并查看该方法运行需要多长时间。如果确实是1-2秒,那么您知道造成延迟的原因。如果是这种情况,您可以尝试在单独的线程上或作为AsyncTask启动该方法,以免该方法阻塞您的UI