2017-07-18 58 views
1

我編寫了一個測驗應用程序,現在我正在爲我的應用程序制定最後一步。我認爲一個閃屏將是一個不錯的主意。所以我爲我的程序添加了一個Splash Screen活動,並且我設置了它,現在,當我啓動我的應用程序時,啓動畫面不會出現。錯誤在哪裏?Splashscreen沒有出現,主要活動是

主要活動(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); 
    } 

} 

閃屏(飛濺)

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:

 <?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> 

菜單2的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); 




} 

}

菜單2 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> 

+1

請出示您的Manifest.xml – 0X0nosugar

+0

我會添加它現在的先生 – ProjectX

+0

[開機畫面將不會顯示(可能的重複https://stackoverflow.com/questions/14722035/splash-screen-will -not-display) – 0X0nosugar

回答

2
<activity 
      android:name=".Splash" 
      android:screenOrientation="portrait"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

+0

爲什麼OP應該設置screenOrientation?是否可以保留MainActivity的意圖過濾器? – 0X0nosugar

+0

這將避免在應用程序中自動旋轉。自動旋轉將重新開始您的活動。我只在我的應用程序中定位肖像模式。 –

+0

刪除MainActivity。只保留'Splash' –

2

從主要活動招intent-filter這樣做是你的Manifest.xml到飛濺活動在manifest.xml文件

<activity android:name=".QuizActivity"></activity> 

<activity android:name=".Splash"> 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 
     <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</activity>