J'ai codé un Quiz App, maintenant je suis en train de préparer les dernières étapes pour mon application.J'ai pensé qu'un écran Splash serait une bonne idée. J'ai donc ajouté une activité Splash Screen à mon programme et je l'ai mis en place, maintenant quand je commence à démarrer mon application, l'écran de démarrage n'apparaît pas. Où est l'erreur?Splashscreen n'apparaît pas, L'activité principale est
Activité principale (QuizActivity):
public class QuizActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
private Toolbar mToolbar;
private MenuItem menuItem;
private Intent in;
private QuestionLibrary mQuestionLibrary = new QuestionLibrary();
private TextView mScoreView;
private TextView mQuestionView;
private Button mButtonChoice1;
private Button mButtonChoice2;
private Button mButtonChoice3;
private String mAnswer;
private int mScore = 0;
private int mQuestionNumber = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
//Randromize the row of the questions
QuestionLibrary q = new QuestionLibrary();
System.out.printf("Question:0 Choice:(%s, %s, %s) Answer:%s%n",
q.getChoice1(0), q.getChoice2(0), q.getChoice3(0), q.getCorrectAnswer(0));
q.shuffle();
System.out.printf("Question:0 Choice:(%s, %s, %s) Answer:%s%n",
q.getChoice1(0), q.getChoice2(0), q.getChoice3(0), q.getCorrectAnswer(0));
mQuestionLibrary.shuffle();
//End randomizer
mToolbar = (Toolbar) findViewById(R.id.nav_action);
setSupportActionBar(mToolbar);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true); //Able to see the Navigation Burger "Button"
NavigationView mNavigationView = (NavigationView) findViewById(R.id.nv1);
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){
@Override
public boolean onNavigationItemSelected(MenuItem menuItem){
switch (menuItem.getItemId()){
case(R.id.nav_stats):
Intent accountActivity = new Intent(getApplicationContext(),Menu2.class);
startActivity(accountActivity);
}
return true;
}
});
mScoreView = (TextView) findViewById(R.id.score_score);
mQuestionView = (TextView) findViewById(R.id.question);
mButtonChoice1 = (Button) findViewById(R.id.choice1);
mButtonChoice2 = (Button) findViewById(R.id.choice2);
mButtonChoice3 = (Button) findViewById(R.id.choice3);
updateQuestion();
//Start of Button Listener1
mButtonChoice1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//My logic for Button goes in here
if (mButtonChoice1.getText() == mAnswer) {
mScore = mScore + 1;
updateScore(mScore);
updateQuestion();
//This line of code is optional...
Toast.makeText(QuizActivity.this, "Correct", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(QuizActivity.this, "Wrong... Try again!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(QuizActivity.this, Menu2.class);
intent.putExtra("score",mScore); //pass score to Menu2
startActivity(intent);
}
}
});
//End of Button Listener1
//Start of Button Listener2
mButtonChoice2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//My logic for Button goes in here
if (mButtonChoice2.getText() == mAnswer) {
mScore = mScore + 1;
updateScore(mScore);
updateQuestion();
//This line of code is optional...
Toast.makeText(QuizActivity.this, "Correct", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(QuizActivity.this, "Oh... wrong your score is 0", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(QuizActivity.this, Menu2.class);
intent.putExtra("score",mScore); //pass score to Menu2
startActivity(intent);
}
}
});
//End of Button Listener2
//Start of Button Listener3
mButtonChoice3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//My logic for Button goes in here
if (mButtonChoice3.getText() == mAnswer) {
mScore = mScore + 1;
updateScore(mScore);
updateQuestion();
//This line of code is optional...
Toast.makeText(QuizActivity.this, "Correct", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(QuizActivity.this, "Come on, that was not so hard...", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(QuizActivity.this, Menu2.class);
intent.putExtra("score",mScore); //pass score to Menu2
startActivity(intent);
}
}
});
//End of Button Listener3
}
private void updateQuestion() {
if (mQuestionNumber < mQuestionLibrary.getLength()) {
mQuestionView.setText(mQuestionLibrary.getQuestion(mQuestionNumber));
mButtonChoice1.setText(mQuestionLibrary.getChoice1(mQuestionNumber));
mButtonChoice2.setText(mQuestionLibrary.getChoice2(mQuestionNumber));
mButtonChoice3.setText(mQuestionLibrary.getChoice3(mQuestionNumber));
mAnswer = mQuestionLibrary.getCorrectAnswer(mQuestionNumber);
mQuestionNumber++;
}
else {
Toast.makeText(QuizActivity.this, "Last Question! You are very intelligent!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(QuizActivity.this, Menu2.class);
intent.putExtra("score",mScore); //pass score to Menu2
startActivity(intent);
}
}
private void updateScore (int point){
mScoreView.setText("" + mScore);
}
@Override //Makes that the "Burger" Item, shows the Drawer if someone clicks on the simbol
public boolean onOptionsItemSelected (MenuItem item){
if (mToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
écran Splash (Splash)
public class Splash extends AppCompatActivity {
//Splash screen start
private static int SPLASH_TIME_OUT = 4000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent homeIntent = new Intent(Splash.this, QuizActivity.class);
startActivity(homeIntent);
finish();
}
}, SPLASH_TIME_OUT);
}
}
XML Splash:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="amapps.impossiblequiz.Splash">
</android.support.constraint.ConstraintLayout>
Menu2 Java:
public class Menu2 extends AppCompatActivity {
private DrawerLayout mDrawerLayout2;
private ActionBarDrawerToggle mToggle;
private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu2);
TextView txtScore = (TextView) findViewById(R.id.textScore2);
TextView txtHighScore = (TextView)findViewById(R.id.textHighScore);
ImageView imgTrophyView1 = (ImageView)findViewById(R.id.trophy1);
ImageView imgTrophyView2 = (ImageView) findViewById(R.id.trophy2);
Intent intent = getIntent();
int mScore = intent.getIntExtra ("score",0);
txtScore.setText("Your score is: " + mScore);
SharedPreferences sharedpreferences = getSharedPreferences("mypref", Context.MODE_PRIVATE);
int applyView =sharedpreferences.getInt("currentscore",0);
SharedPreferences mypref =getPreferences(MODE_PRIVATE);
int highScore = mypref.getInt("highScore", 0);
if (highScore>= mScore)
txtHighScore.setText("High score: " + highScore);
else{
txtHighScore.setText("New highscore: " + mScore);
SharedPreferences.Editor editor = mypref.edit();
editor.putInt("highScore",mScore);
editor.commit();
}
if (applyView >=10 && applyView<20)
imgTrophyView1.setVisibility(View.VISIBLE);
if (applyView >= 20 && applyView<30){
imgTrophyView2.setVisibility(View.VISIBLE);}}
public void onClick(View view) {
Intent intent = new Intent(Menu2.this, QuizActivity.class);
startActivity(intent);
mToolbar = (Toolbar) findViewById(R.id.nav_action);
setSupportActionBar(mToolbar);
mDrawerLayout2 = (DrawerLayout) findViewById(R.id.drawerLayout2);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout2, R.string.open, R.string.close);
mDrawerLayout2.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
NavigationView mNavigationView = (NavigationView) findViewById(nv2);
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case (R.id.nav_home2):
Intent accountActivity2 = new Intent(getApplicationContext(), QuizActivity.class);
startActivity(accountActivity2);
}
return true;
}
});
}
@Override //Makes that the "Burger" Item, shows the Drawer if someone clicks on the simbol
public boolean onOptionsItemSelected(MenuItem item) {
if (mToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Menu2 XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="amapps.impossiblequiz.Menu2"
android:id="@+id/drawerLayout2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="@layout/navigation_action"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:id="@+id/textScore2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="71dp"
android:background="#f60"
android:paddingBottom="20dp"
android:paddingLeft="50dp"
android:paddingRight="100dp"
android:paddingTop="20dp"
android:text="Your score:"
android:textAppearance="@style/TextAppearance.AppCompat"
android:textColor="#ffffff"
android:textSize="20dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="@+id/textHighScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/textScore2"
android:layout_marginTop="20dp"
android:background="#1abc9c"
android:paddingBottom="20dp"
android:paddingLeft="50dp"
android:paddingRight="100dp"
android:paddingTop="20dp"
android:text="Highest score:"
android:textAppearance="@style/TextAppearance.AppCompat"
android:textColor="#ffffff"
android:textSize="20dp" />
<Button
android:id="@+id/tryAgain_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/textHighScore"
android:layout_marginTop="26dp"
android:background="#000"
android:onClick="onClick"
android:text="Restart Quiz!"
android:textColor="#ffffff" />
<ImageView
android:id="@+id/trophy1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/tryAgain_button"
android:layout_marginLeft="33dp"
android:layout_marginStart="33dp"
android:layout_marginTop="37dp"
app:srcCompat="@mipmap/ic_person_black_24dp"
android:visibility="invisible"/>
<ImageView
android:id="@+id/trophy2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/trophy1"
android:layout_marginLeft="39dp"
android:layout_marginStart="39dp"
android:layout_toEndOf="@+id/trophy1"
android:layout_toRightOf="@+id/trophy1"
app:srcCompat="@mipmap/ic_launcher_round"
android:visibility="invisible"/>
</RelativeLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="@menu/navigation_menu2"
android:layout_gravity="start"
android:id="@+id/nv2"
app:headerLayout="@layout/navigation_header"
app:itemIconTint="@drawable/tint_color_selector2">
</android.support.design.widget.NavigationView>
s'il vous plaît montrer votre Manifest.xml – 0X0nosugar
Je vais ajouter maintenant monsieur – ProjectX
double possible de [Screen Splash ne sera pas affiché] (https://stackoverflow.com/questions/14722035/splash-screen-will -not-display) – 0X0nosugar