当前位置: 首页 > 知识库问答 >
问题:

我正在尝试添加启动屏幕到我的应用程序

缑高朗
2023-03-14

我一直试图实现启动屏幕到我的应用程序的帮助下,许多代码可以在网站上使用,但没有一个为我工作。每次应用程序崩溃后显示启动屏幕3秒。我不知道哪里出错了,请更正我的代码,以正确显示闪屏!谢谢!

//这是我的主要活动

 1. public class MainActivity extends AppCompatActivity {

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

            Button one = (Button) this.findViewById(R.id.gg);
            final MediaPlayer mp1 = MediaPlayer.create(this, R.raw.gg);
            one.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "Good Game", Toast.LENGTH_SHORT).show();
                    mp1.start();
                }
            });

            Button two = (Button) this.findViewById(R.id.gm);
            final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.gm);
            two.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "Oh, They got me!", Toast.LENGTH_SHORT).show();
                    mp2.start();
                }
            });

            Button three = (Button) this.findViewById(R.id.cb);
            final MediaPlayer mp3 = MediaPlayer.create(this, R.raw.cb);
            three.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "Come on boy", Toast.LENGTH_SHORT).show();
                    mp3.start();
                }
            });

            Button four = (Button) this.findViewById(R.id.ns);
            final MediaPlayer mp4 = MediaPlayer.create(this, R.raw.ns);
            four.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "Nice shot", Toast.LENGTH_SHORT).show();
                    mp4.start();
                }
            });

            Button five = (Button) this.findViewById(R.id.wp);
            final MediaPlayer mp5 = MediaPlayer.create(this, R.raw.wp);
            five.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "You wanna piece of me!", Toast.LENGTH_SHORT).show();
                    mp5.start();
                }
            });

            Button six = (Button) this.findViewById(R.id.bi);
            final MediaPlayer mp6 = MediaPlayer.create(this, R.raw.bi);
            six.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "Bring it", Toast.LENGTH_SHORT).show();
                    mp6.start();
                }
            });

            Button seven = (Button) this.findViewById(R.id.lg);
            final MediaPlayer mp7 = MediaPlayer.create(this, R.raw.lg);
            seven.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "Let’s go, Yeah!", Toast.LENGTH_SHORT).show();
                    mp7.start();
                }
            });

            Button eight = (Button) this.findViewById(R.id.ru);
            final MediaPlayer mp8 = MediaPlayer.create(this, R.raw.ru);
            eight.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "Ready Up", Toast.LENGTH_SHORT).show();
                    mp8.start();
                }
            });

            Button nine = (Button) this.findViewById(R.id.nn);
            final MediaPlayer mp9 = MediaPlayer.create(this, R.raw.nn);
            nine.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "Nooooo", Toast.LENGTH_SHORT).show();
                    mp9.start();
                }
            });

            Button ten = (Button) this.findViewById(R.id.cm);
            final MediaPlayer mp10 = MediaPlayer.create(this, R.raw.cm);
            ten.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "Cover me", Toast.LENGTH_SHORT).show();
                    mp10.start();
                }
            });

            Button eleven = (Button) this.findViewById(R.id.hh);
            final MediaPlayer mp11 = MediaPlayer.create(this, R.raw.hh);
            eleven.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "Hoo-ya", Toast.LENGTH_SHORT).show();
                    mp11.start();
                }
            });

            Button twelve = (Button) this.findViewById(R.id.mo);
            final MediaPlayer mp12 = MediaPlayer.create(this, R.raw.mo);
            twelve.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "Move out", Toast.LENGTH_SHORT).show();
                    mp12.start();
                }
            });

            Button thirteen = (Button) this.findViewById(R.id.gs);
            final MediaPlayer mp13 = MediaPlayer.create(this, R.raw.gs);
            thirteen.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "Get Some", Toast.LENGTH_SHORT).show();
                    mp13.start();
                }
            });


            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Snackbar.make(view, "Your Messege",Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                }
            });
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    }


//This is my activity_main.xml

 2. <android.support.design.widget.CoordinatorLayout
    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"
        android:fitsSystemWindows="true"
        tools:context="com.example.android.mmm.MainActivity">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/app_bar_height"
            android:fitsSystemWindows="true"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/toolbar_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/AppTheme.PopupOverlay" />

            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>

        <include layout="@layout/content_main" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/fab_margin"
            app:layout_anchor="@id/app_bar"
            app:layout_anchorGravity="bottom|end"
            app:srcCompat="@android:drawable/ic_dialog_email" />

    </android.support.design.widget.CoordinatorLayout>


//This is content_main.xml

 3. <android.support.v4.widget.NestedScrollView
        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"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.example.android.mmm.MainActivity"
        tools:showIn="@layout/activity_main">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="#000000">

            <Button
                android:layout_width="100dp"
                android:layout_height="48dp"
                android:text="GG"
                android:id="@+id/gg"
                android:textSize="18sp"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:textColor="#000000"
                tools:ignore="HardcodedText"
                android:layout_marginTop="16dp"
                android:layout_gravity="center_horizontal"/>

            <Button
                android:id="@+id/gm"
                android:layout_width="100dp"
                android:layout_height="48dp"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="16dp"
                android:text="GM"
                android:textAllCaps="true"
                android:textColor="#000000"
                android:textSize="18sp"
                android:textStyle="bold"
                tools:ignore="HardcodedText" />

            <Button
                android:layout_width="100dp"
                android:layout_height="48dp"
                android:text="CB"
                android:id="@+id/cb"
                android:textSize="18sp"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:textColor="#000000"
                tools:ignore="HardcodedText"
                android:layout_marginTop="16dp"
                android:layout_gravity="center_horizontal"/>

            <Button
                android:layout_width="100dp"
                android:layout_height="48dp"
                android:text="NS"
                android:id="@+id/ns"
                android:textSize="18sp"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:textColor="#000000"
                tools:ignore="HardcodedText"
                android:layout_marginTop="16dp"
                android:layout_gravity="center_horizontal"/>

            <Button
                android:layout_width="100dp"
                android:layout_height="48dp"
                android:text="WP"
                android:id="@+id/wp"
                android:textSize="18sp"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:textColor="#000000"
                tools:ignore="HardcodedText"
                android:layout_marginTop="16dp"
                android:layout_gravity="center_horizontal"/>

            <Button
                android:layout_width="100dp"
                android:layout_height="48dp"
                android:text="BI"
                android:id="@+id/bi"
                android:textSize="18sp"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:textColor="#000000"
                tools:ignore="HardcodedText"
                android:layout_marginTop="16dp"
                android:layout_gravity="center_horizontal"/>

            <Button
                android:layout_width="100dp"
                android:layout_height="48dp"
                android:text="LG"
                android:id="@+id/lg"
                android:textSize="18sp"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:textColor="#000000"
                tools:ignore="HardcodedText"
                android:layout_marginTop="16dp"
                android:layout_gravity="center_horizontal"/>

            <Button
                android:layout_width="100dp"
                android:layout_height="48dp"
                android:text="RU"
                android:id="@+id/ru"
                android:textSize="18sp"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:textColor="#000000"
                tools:ignore="HardcodedText"
                android:layout_marginTop="16dp"
                android:layout_gravity="center_horizontal"/>

            <Button
                android:layout_width="100dp"
                android:layout_height="48dp"
                android:text="NN"
                android:id="@+id/nn"
                android:textSize="18sp"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:textColor="#000000"
                tools:ignore="HardcodedText"
                android:layout_marginTop="16dp"
                android:layout_gravity="center_horizontal"/>

            <Button
                android:layout_width="100dp"
                android:layout_height="48dp"
                android:text="CM"
                android:id="@+id/cm"
                android:textSize="18sp"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:textColor="#000000"
                tools:ignore="HardcodedText"
                android:layout_marginTop="16dp"
                android:layout_gravity="center_horizontal"/>

            <Button
                android:layout_width="100dp"
                android:layout_height="48dp"
                android:text="HH"
                android:id="@+id/hh"
                android:textSize="18sp"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:textColor="#000000"
                tools:ignore="HardcodedText"
                android:layout_marginTop="16dp"
                android:layout_gravity="center_horizontal"/>

            <Button
                android:layout_width="100dp"
                android:layout_height="48dp"
                android:text="MO"
                android:id="@+id/mo"
                android:textSize="18sp"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:textColor="#000000"
                tools:ignore="HardcodedText"
                android:layout_marginTop="16dp"
                android:layout_gravity="center_horizontal"/>

            <Button
                android:layout_width="100dp"
                android:layout_height="48dp"
                android:text="GS"
                android:id="@+id/gs"
                android:textSize="18sp"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:textColor="#000000"
                tools:ignore="HardcodedText"
                android:layout_marginTop="16dp"
                android:layout_marginBottom="16dp"
                android:layout_gravity="center_horizontal"/>

        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>


//This is AndroidManifest.xml

 4. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.mmm">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity
        android:name=".SplashActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
    </activity>
</application>
//This is styles.xml

 5. <resources>

        <!-- Base application theme. -->
        <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
            <!-- Customize your theme here. -->
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
        </style>

        <style name="AppTheme.NoActionBar">
            <item name="windowActionBar">false</item>
            <item name="windowNoTitle">true</item>
        </style>

        <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

        <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

    </resources>


//This is my SplashActivity.Java


public class SplashScreen 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(SplashScreen.this, MainActivity.class);
                startActivity(i);

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

 6. //This is activity_splash.xml

        <RelativeLayout 
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:src="@drawable/wwe_logo" />

        </RelativeLayout>

共有1个答案

齐永昌
2023-03-14

appcompatactivity有它自己的工具栏,所以您需要在清单中使用Android:theme=“@style/apptheme.noactionbar”删除它

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" 
        android:theme="@style/AppTheme.NoActionBar">
    </activity>

虽然如果在工具栏中没有进行太多的自定义,那么也可以通过从XML中删除工具栏并从Java代码中删除工具栏的初始化来跳过创建自己的工具栏,但是这样做将失去折叠工具栏的功能

 类似资料:
  • 因此,我一直试图在我的android应用程序中添加启动屏幕,但每当我试图运行它时,应用程序总是崩溃。这里是代码`` activity_splash.xml

  • 建造Gradle。应用程序部分。 项目部分。 我的文件中没有所有项目部分。这就是为什么我自己添加了这个地方。但是那些在所有项目中定义的 它必须成形。当我尝试引入google()和mavenCentral时,它给出了buildScript中已经定义的错误。当我试图只定义没有定义google()和mavenCentral;Build被配置为首选设置存储库而不是项目存储库,但存储库“maven”是由Bu

  • 错误:任务“:app:ProcessDebugResources”执行失败。 com.android.ide.common.Process.processException:org.gradle.Process.internal.exeception:Process“command”c:\users\mohamed sallam\appdata\local\android\sdk\build-to

  • 问题内容: 我正在编写一个应用程序,当用户指示我的应用程序这样做时,该应用程序应该能够将小部件(仅文本框)添加到用户电话的主屏幕。我该怎么做? 我知道我可以添加一个应用小部件,但是如何添加更多呢? 问题答案: 从应用程序无法将小部件放置在主屏幕中。只有主屏幕可以将应用程序小部件添加到主屏幕。 相似链接link 但是您可以提供用户从widgetpicker中选择widget。

  • 我正在尝试将本地Spring Boot应用程序连接到我的Heroku Postgres数据库。当我使用Heroku上可用的creds尝试此操作时,我得到以下错误 错误:SEVERITY_LOCALIZED=FATAL,SEVERITY_NON_LOCALIZED=FATAL, CODE=28000, MESSAGE=没有主机"myhost",用户"myuser",数据库"mydb"的pg_hba.

  • 我制作了一个简单的Android应用程序,其中有一个登录活动和一个片段(BottomSheetDialogFragment)。我正试图通过片段(有多个按钮)向主活动(登录活动)添加多种语言,但它在代码下面不起作用。有人能帮我吗? 登录活动 碎片 登录活动片段