gpt4 book ai didi

java - 应用程序在主线程上做了太多工作(Firebase 和 OnLongClickListener)

转载 作者:行者123 更新时间:2023-12-02 02:13:23 32 4
gpt4 key购买 nike

我试图让我的应用程序向 firebase 发送信息,首先它允许它向 firebase 发送信息,但在第二次时它显示一条消息“跳过了 n# 帧!应用程序可能在其主线程上做了太多工作。”和“后台并发复制GC释放了185001(8MB)个AllocSpace对象,0(0B)个LOS对象,31%空闲,12MB/18MB...”。

其主要功能是如果长时间点击某个游戏的按钮,则发送有关游戏的信息(OnLongClickListener)。

该函数的java文件是这样的:

HomePageActivity.java

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class HomePageActivity extends AppCompatActivity {

private FirebaseAuth firebaseAuth;
private Firebase FavDBRef;
private FirebaseUser firebaseUser;
private Button LeagueButton, OWButton, CSGOButton, DOTAButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
setupUIViews();
gameButtons();

}

public void setupUIViews(){
LeagueButton = findViewById(R.id.leagueButton);
OWButton = findViewById(R.id.owButton);
CSGOButton = findViewById(R.id.csgoButton);
DOTAButton = findViewById(R.id.dotaButton);
}

public void onStart() {
super.onStart();
FirebaseUser currentUser = firebaseAuth.getCurrentUser();
}

public void gameButtons(){

Firebase.setAndroidContext(this);

firebaseAuth = FirebaseAuth.getInstance();
firebaseUser = firebaseAuth.getCurrentUser();

FavDBRef = new Firebase("https://mwt-esportsinfo-userfav.firebaseio.com/" + firebaseUser.getUid()).child("FavEsport");

LeagueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(HomePageActivity.this, LeagueOfLegends.class));
}
});

LeagueButton.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {

FavDBRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String FavEsport = "League of Legends";
String FavEsportDev = "Riot Games";
String FavEsportRel = "27 October 2009";
String FavEsportGenre = "Multiplayer Online Battle Arena";
String FavEsportImg = "https://vignette.wikia.nocookie.net/leagueoflegends/images/8/86/League_of_legends_logo_transparent.png/revision/latest/scale-to-width-down/640?cb=20131023175853";

UserFavESport userFavESport = new UserFavESport(FavEsport, FavEsportDev, FavEsportRel, FavEsportGenre, FavEsportImg);

FavDBRef.setValue(userFavESport);

Toast.makeText(HomePageActivity.this, FavEsport + " has been added as Favourite eSport", Toast.LENGTH_SHORT).show();
}

@Override
public void onCancelled(FirebaseError firebaseError) {

}
});

return false;
}
});

OWButton.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
FavDBRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String FavEsport = "Overwatch";
String FavEsportDev = "Blizzard Entertainment";
String FavEsportRel = "24 May 2016";
String FavEsportGenre = "First-Person Shooter";
String FavEsportImg = "https://vignette.wikia.nocookie.net/dbxfanon/images/8/84/Overwatch_logo_by_feeerieke-da4xuzp.png/revision/latest?cb=20170316184119";

UserFavESport userFavESport = new UserFavESport(FavEsport, FavEsportDev, FavEsportRel, FavEsportGenre, FavEsportImg);

FavDBRef.setValue(userFavESport);

Toast.makeText(HomePageActivity.this, FavEsport + " has been added as Favourite eSport", Toast.LENGTH_SHORT).show();
}

@Override
public void onCancelled(FirebaseError firebaseError) {

}
});

return false;
}
});

CSGOButton.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
FavDBRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String FavEsport = "Counter-Strike: Global Offensive";
String FavEsportDev = "Valve Corporation";
String FavEsportRel = "21 August 2012";
String FavEsportGenre = "First-Person Shooter";
String FavEsportImg = "https://vignette.wikia.nocookie.net/logopedia/images/c/c8/CSGO.png/revision/latest?cb=20150828062634";

UserFavESport userFavESport = new UserFavESport(FavEsport, FavEsportDev, FavEsportRel, FavEsportGenre, FavEsportImg);

FavDBRef.setValue(userFavESport);

Toast.makeText(HomePageActivity.this, FavEsport + " has been added as Favourite eSport", Toast.LENGTH_SHORT).show();
}

@Override
public void onCancelled(FirebaseError firebaseError) {

}
});

return false;
}
});

DOTAButton.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
FavDBRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String FavEsport = "DOTA 2";
String FavEsportDev = "Valve Corporation";
String FavEsportRel = "9 July 2013";
String FavEsportGenre = "Multiplayer Online Battle Arena";
String FavEsportImg = "http://www.userlogos.org/files/logos/Kravlenissen/Dota-2%284x3%291-1.png";

UserFavESport userFavESport = new UserFavESport(FavEsport, FavEsportDev, FavEsportRel, FavEsportGenre, FavEsportImg);

FavDBRef.setValue(userFavESport);

Toast.makeText(HomePageActivity.this, FavEsport + " has been added as Favourite eSport", Toast.LENGTH_SHORT).show();
}

@Override
public void onCancelled(FirebaseError firebaseError) {

}
});

return false;
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.profileMenu){
startActivity(new Intent(HomePageActivity.this, UserProfileActivity.class));
}
if (item.getItemId() == R.id.logoutMenu){
Logout();
}
else{
return super.onOptionsItemSelected(item);
}
return true;
}

private void Logout(){
firebaseAuth.signOut();
finish();
startActivity(new Intent(HomePageActivity.this, LoginActivity.class));
}
}

以及检索这些信息。

UserFavESport.java

public class UserFavESport {
String favEsports, favEsportsDev, favEsportsRel, favEsportsGenre, favEsportsImg ;

public UserFavESport(){

}

public UserFavESport(String favEsports, String favEsportsDev, String favEsportsRel, String favEsportsGenre, String favEsportsImg) {
this.favEsports = favEsports;
this.favEsportsDev = favEsportsDev;
this.favEsportsRel = favEsportsRel;
this.favEsportsGenre = favEsportsGenre;
this.favEsportsImg = favEsportsImg;
}

public String getFavEsports() {
return favEsports;
}

public void setFavEsports(String favEsports) {
this.favEsports = favEsports;
}

public String getFavEsportsDev() {
return favEsportsDev;
}

public void setFavEsportsDev(String favEsportsDev) {
this.favEsportsDev = favEsportsDev;
}

public String getFavEsportsRel() {
return favEsportsRel;
}

public void setFavEsportsRel(String favEsportsRel) {
this.favEsportsRel = favEsportsRel;
}

public String getFavEsportsGenre() {
return favEsportsGenre;
}

public void setFavEsportsGenre(String favEsportsGenre) {
this.favEsportsGenre = favEsportsGenre;
}

public String getFavEsportsImg() {
return favEsportsImg;
}

public void setFavEsportsImg(String favEsportsImg) {
this.favEsportsImg = favEsportsImg;
}
}

对此的任何帮助将不胜感激。

谢谢

最佳答案

错误“应用程序可能在其主线程上做了太多工作”表明您正在主线程上进行长时间操作。我们应该始终避免在主线程上执行长时间操作,而应在后台线程上执行。除了在按钮的长按监听器中显示 toast 之外,所有代码都应该在新线程而不是主线程上运行。只需在单击监听器方法中创建一个新线程并将此工作委托(delegate)给新线程即可。

您可以使用 Thread 和 Runnable 来处理主线程之外的数据,或者如果您想将结果传达回 UI 线程,则可以使用异步任务。

关于java - 应用程序在主线程上做了太多工作(Firebase 和 OnLongClickListener),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49630097/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com