0
我收到不必要的跳过消息。该应用程序在其主线程中做了太多工作。在阅读了几个答案之后,我意识到要解决它,我必须使用多线程。但我的代码是非常基本的。不过,我通过使用Async Class在后台完成工作,重写了代码。不必要的跳过帧错误
我附加到活动文件的代码。首先是启动画面,另一个是启动画面的结果。即使在我手机的屏幕画面中,我也会产生不必要的延迟。
这是闪屏
package bt4u.com.pokemonbattles;
public class splash extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
System.out.println("Splash screen activity (splash.java)");
new myClass().execute();
}
class myClass extends AsyncTask<Void,Void,Void> {
@Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
Intent intent=new Intent(splash.this,gameOptions.class);
startActivity(intent);
finish();
}
}
}
的代码是这样的结果活动
public class gameOptions extends AppCompatActivity {
int gameTypeCons=0,gameFieldCons=0;
int difficulty=0; // 0-easy, 1-medium, 2-hard
int cpu=1;//0-player 1 is cpu, 1- player 2 is cpu
Button newGame;
Spinner gameType,gameField,p1,p2;
String[] options={"2 Players Local","Player vs CPU","2 Players Online","Bluetooth Play","Wi-FI Play"}, field={"3X3","4X4"},
pl={"Chikorita","Pikachu","Bulbasaur","Charmender","Mewto","Lapras","Onix","Squirtle","Dragonite","Charizard"}
,pl2={"Squirtle","Charizard","Dragonite","Onix","Chikorita","Lapras","Mewto","Charmender","Bulbasaur","Pikachu"};
String p1poke, p2poke,player1Name, player2Name;
EditText name1,name2;
@Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_options);
System.out.println("Game options activity (gameoptions.class)");
gameType=(Spinner)findViewById(R.id.gameType);
gameField=(Spinner)findViewById(R.id.gameField);
p1=(Spinner)findViewById(R.id.p1);
p2=(Spinner)findViewById(R.id.p2);
newGame = (Button)findViewById(R.id.newGame);
name1 = (EditText)findViewById(R.id.editText);
name2 = (EditText)findViewById(R.id.editText2);
ArrayAdapter optionsAdapter = new ArrayAdapter(gameOptions.this,android.R.layout.simple_dropdown_item_1line,options);
optionsAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
gameType.setAdapter(optionsAdapter);
ArrayAdapter fieldAdapter = new ArrayAdapter(gameOptions.this,android.R.layout.simple_dropdown_item_1line,field);
fieldAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
gameField.setAdapter(fieldAdapter);
ArrayAdapter player1 = new ArrayAdapter(gameOptions.this,android.R.layout.simple_dropdown_item_1line,pl);
player1.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
p1.setAdapter(player1);
ArrayAdapter player2 = new ArrayAdapter(gameOptions.this,android.R.layout.simple_dropdown_item_1line,pl2);
player2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
p2.setAdapter(player2);
gameType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
gameTypeCons=position;
if (position==1){
showDialog(1);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
gameField.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
gameFieldCons=position;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
p2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
p2poke=pl2[position];
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
p1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
p1poke=pl[position];
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
newGame.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
player1Name=name1.getText().toString();
player2Name=name2.getText().toString();
new myClass().execute();
}
});
}
class task2 extends AsyncTask<Void,Void,Void>{
@Override
protected Void doInBackground(Void... params) {
return null;
}
}
public Dialog onCreateDialog(int id){
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.cpucustomdialog);
RadioGroup radioGroup = (RadioGroup)dialog.findViewById(R.id.diff);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId==R.id.radioButton)
difficulty=0;
else if (checkedId==R.id.radioButton2)
difficulty=1;
else if (checkedId==R.id.radioButton3)
difficulty=2;
else
difficulty=0;
}
});
RadioGroup radioGroup2 = (RadioGroup)dialog.findViewById(R.id.cpu);
radioGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId==R.id.radioButton4)
cpu=0;
else
cpu=1;
}
});
dialog.show();
return dialog;
}
class myClass extends AsyncTask<Void,Void,Void>{
@Override
protected Void doInBackground(Void... params) {
SharedPreferences sp = getSharedPreferences("prefs", 0);
SharedPreferences.Editor editor = sp.edit();
editor.putString("player1Name", player1Name);
editor.putString("player2Name", player2Name);
editor.putString("player1pokemon",p1poke);
editor.putString("player2Pokemon",p2poke);
editor.putInt("difficulty",difficulty);
editor.putInt("cpu",cpu);
editor.commit();
if (gameFieldCons==0){
switch (gameTypeCons){
case 0:
Intent intent = new Intent(gameOptions.this,MainActivity.class);
startActivity(intent);
break;
case 1:
if (cpu==1){
Intent intent2 = new Intent(gameOptions.this,cpueasy.class);
startActivity(intent2);
}
else{
Intent intent2 = new Intent(gameOptions.this,demo.class);
startActivity(intent2);
}
break;
default:
break;
}
}
else {
switch (gameTypeCons){
case 0:
Intent intent = new Intent(gameOptions.this,bigmain.class);
startActivity(intent);
break;
case 1:
if (cpu==1) {
Intent intent2 = new Intent(gameOptions.this, igcpu.class);
startActivity(intent2);
}
else{
Intent intent2 = new Intent(gameOptions.this,demo2.class);
startActivity(intent2);
}
break;
default:
break;
}
}
return null;
}
}
}
后,这是闪屏启动和启动画面效果之间的logcat的。
I/System.out: Splash screen activity (splash.java)
I/Choreographer: Skipped 43 frames! The application may be doing too much work on its main thread.
I/Choreographer: Skipped 44 frames! The application may be doing too much work on its main thread.
I/Choreographer: Skipped 34 frames! The application may be doing too much work on its main thread.
I/Choreographer: Skipped 38 frames! The application may be doing too much work on its main thread.
I/Timeline: Timeline: Activity_launch_request id:bt4u.com.pokemonbattles time:415126775
I/Choreographer: Skipped 41 frames! The application may be doing too much work on its main thread.
I/Choreographer: Skipped 37 frames! The application may be doing too much work on its main thread.
I/System.out: Game options activity (gameoptions.class)
I/Choreographer: Skipped 75 frames! The application may be doing too much work on its main thread.
I/Choreographer: Skipped 54 frames! The application may be doing too much work on its main thread.
I/Choreographer: Skipped 80frames! The application may be doing too much work on its main thread.
I/Choreographer: Skipped 74 frames! The application may be doing too much work on its main thread.
I/Choreographer: Skipped 33frames! The application may be doing too much work on its main thread.
I/Choreographer: Skipped 34 frames! The application may be doing too much work on its main thread.
I/Choreographer: Skipped 32 frames! The application may be doing too much work on its main thread.
I/Choreographer: Skipped 36 frames! The application may be doing too much work on its main thread.
I/Choreographer: Skipped 32 frames! The application may be doing too much work on its main thread.
I/Choreographer: Skipped 30 frames! The application may be doing too much work on its main thread.
W/art: Suspending all threads took: 12.592ms
W/InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed.
W/InputMethodManager: startInputInner : InputBindResult == null
W/IInputConnectionWrapper: showStatusIcon on inactive InputConnection
它仍然呈现出跳跃1周的时间。但启动画面现在很流畅。你能解释为什么这样做吗? –
因为不需要异步任务,所以在需要从后台执行一些任务的情况下,需要使用异步任务,例如从服务器获取数据。在这种情况下,您只需要线程等待3秒钟,然后移动上其他班级。 – Drv
如何处理结果活动 –