java - 启动画面不出现,主要 Activity 出现

标签 java android splash-screen

我编写了一个测验应用程序,现在我正在为我的应用程序编写最后的步骤。我认为启动画面是个不错的主意。所以我在我的程序中添加了一个启动画面 Activity 并进行了设置,现在当我启动我的应用程序时启动画面不会出现。哪里出错了?

主要 Activity (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>

最佳答案

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

在您的 Manifest.xml 中执行此操作

关于java - 启动画面不出现,主要 Activity 出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45173824/

相关文章:

java - 为什么我不能使用 Override 注释?

java - Java 中(未)压缩的 base64 编码字符串 (Android)

android - 在 Android Studio 中添加库 make "UNEXPECTED TOP-LEVEL EXCEPTION"

objective-c - 如何在 objective-c 的启动图像上方显示 "Loading..."gif 图像?

java - 如何在 Eclipse IDE for Java 中启用自动完成(智能感知)

java - 计算十六进制颜色相反的公式 ("Difference")

java - Kryonet RMI 抛出异常 => 循环 (StackOverflowError)

android - 在显示拆分操作栏时隐藏操作栏

java - 应用 SplashScreen 而不创建 Thread 和 run()

php - 使用 PHP(而非 JS)检查 cookie 以用于初始页面重定向