2016-07-05 4 views
0

Ich bekomme unnötige Nachricht des Überspringens. Die Anwendung macht zu viel Arbeit in ihrem Hauptthread. Nachdem ich ein paar Antworten hier gelesen hatte, erkannte ich, dass ich Multithreading verwenden muss, um es zu lösen. Aber mein Code ist sehr einfach. Dennoch habe ich den Code mithilfe der Async-Klasse neu geschrieben, um die Arbeit im Hintergrund auszuführen.Unnötiger Überspringrahmenfehler

Ich füge den Code der Aktivitätsdatei an. Der erste ist der Begrüßungsbildschirm und der andere ist das Ergebnis des Begrüßungsbildschirms. Ich bekomme unnötige Verzögerung sogar während des Bildschirms in meinem Telefon.

Dies ist der Code von Splash-Screen

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(); 
    } 
} 

} 

Und das ist der nach dem Ergebnis Aktivität

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; 
    } 
} 
} 

Dies ist der logcat zwischen Splash-Screen Start- und Splash-Screen Ergebnis.

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 

Antwort

1

Just do unten Code für Ihren Begrüßungsbildschirm, ersetzen Sie Ihre splash.java mit folgenden Datei:

public class splash extends Activity { 

// Splash screen timer 
private static int SPLASH_TIME_OUT = 3000; 

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

    new Handler().postDelayed(new Runnable() { 

     /* 
     * Showing splash screen with a timer. This will be useful when you 
     * want to show case your app logo/company 
     */ 

     @Override 
     public void run() { 
      // This method will be executed once the timer is over 
      // Start your app main activity 
      Intent i = new Intent(splash.this, gameOptions.class); 
      startActivity(i); 

      // close this activity 
      finish(); 
     } 
    }, SPLASH_TIME_OUT); 
} 

} 
+0

Sein noch angezeigt, das Überspringen Rahmen 1mal. Aber der Begrüßungsbildschirm ist jetzt glatt. Kannst du eine Erklärung geben, warum es gemacht wird? –

+0

Da keine asynchrone Task benötigt wird, müssen Sie eine asynchrone Task in Situationen verwenden, in denen Sie einige Aufgaben im Hintergrund ausführen möchten, wie das Abrufen von Daten vom Server. In diesem Fall mussten Sie nur 3 Sekunden warten und dann verschieben auf andere Klasse. – Drv

+0

Und was zu tun ist, wenn Ergebnis Aktivität –