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
如何處理結果活動 –