在Splash Screen Android应用程序之后强行关闭

时间:2017-07-18 19:00:26

标签: java android android-intent splash-screen

这是Logcat。请求帮助:(



07-19 09:39:48.815 9001-9001/pens.pensnews E/AndroidRuntime: FATAL EXCEPTION: main
                                                             Process: pens.pensnews, PID: 9001
                                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{pens.pensnews/pens.pensnews.MainActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319)
                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
                                                                 at android.app.ActivityThread.access$1100(ActivityThread.java:229)
                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                 at android.os.Looper.loop(Looper.java:148)
                                                                 at android.app.ActivityThread.main(ActivityThread.java:7325)
                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                              Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
                                                                 at android.support.v7.app.AppCompatDelegateImplV9.setSupportActionBar(AppCompatDelegateImplV9.java:204)
                                                                 at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:129)
                                                                 at pens.pensnews.MainActivity.onCreate(MainActivity.java:25)
                                                                 at android.app.Activity.performCreate(Activity.java:6904)
                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415) 
                                                                 at android.app.ActivityThread.access$1100(ActivityThread.java:229) 
                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) 
                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                 at android.os.Looper.loop(Looper.java:148) 
                                                                 at android.app.ActivityThread.main(ActivityThread.java:7325) 
                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 




我实施了一个启动画面,以便在我的主要活动之前启动。在我使用Splash Screen之前,它确实有效。我不确定我错在哪里或错过了什么。

这是我的代码



Splashscreen.java :

    package pens.pensnews;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.os.Handler;import android.view.Window;import android.view.WindowManager;
    public class Splashscreen extends Activity {

    //Set waktu lama splashscreen
    private static int splashInterval = 2000;
     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_splashscreen);
    new Handler().postDelayed(new Runnable(){
            @Override
            public void run() {
                // TODO Auto-generated method stub
    Intent i = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(i); // menghubungkan activity splashscreen ke main activity dengan intent

                //jeda selesai Splashscreen
    finish();
            }
        }, splashInterval);
    }}

MainActivity.java

    package pens.pensnews;import android.os.Bundle;import android.support.design.widget.FloatingActionButton;import android.support.design.widget.Snackbar;import android.support.v4.app.FragmentManager;import android.support.design.widget.NavigationView;import android.support.v4.view.GravityCompat;import android.support.v4.widget.DrawerLayout;import android.support.v7.app.ActionBarDrawerToggle;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.Toolbar;import android.view.Menu;import android.view.MenuItem;import android.view.View;
    public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
        toggle.syncState();
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    FragmentManager manager = getSupportFragmentManager();

        Kampus kampus = new Kampus();
        manager.beginTransaction().replace(R.id.MainLayout, kampus).commit();
    Akademik akademik = new Akademik();
        manager.beginTransaction().replace(R.id.MainLayout, akademik).commit();
    Nonak nonak = new Nonak();
        manager.beginTransaction().replace(R.id.MainLayout, nonak).commit();
    Beranda beranda = new Beranda();
        manager.beginTransaction().replace(R.id.MainLayout, beranda).commit();
    }
    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
    drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }}
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.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.nav_help) {
    return true;
        }

        return super.onOptionsItemSelected(item);}
    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
    int id = item.getItemId();

        if (id == R.id.nav_beranda) {
            Beranda beranda = new Beranda();
    FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.MainLayout, beranda).commit();
        } else if (id == R.id.nav_kampus) {
            Kampus kampus = new Kampus();
    FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.MainLayout, kampus).commit();
        } else if (id == R.id.nav_akademik) {
            Akademik akademik = new Akademik();
    FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.MainLayout, akademik).commit();
        }else if (id == R.id.nav_non) {
            Nonak nonak = new Nonak();
    FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.MainLayout, nonak).commit();
        } else if (id == R.id.nav_about) {
            About about = new About();
    FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.MainLayout, about).commit();
        }
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }}

AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="pens.pensnews">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
        android:theme="@style/AppTheme">
        <activity
            android:name="pens.pensnews.Splashscreen"
    android:label="@string/app_name"
            android:theme="@style/Splashscreen"
            android:noHistory="true">
            <intent-filter>
    <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
    </activity>
        <activity
            android:name="pens.pensnews.MainActivity"
     android:label="@string/app_name" >
        </activity>
        <activity android:name=".Mawapres" />
        <activity android:name=".Akademik1" />
    <activity android:name=".Akademik2" />
        <activity android:name=".Kampus1" />
        <activity android:name=".Kampus2" />
        <activity android:name=".Kampus3" />
    <activity android:name=".Nonak1" />
        <activity android:name=".Nonak2" />
        <activity android:name=".Nonak3" />
        <activity android:name=".Nonak4"></activity>
    </application>

</manifest>
&#13;
&#13;
&#13;

&#13;
&#13;
MainActivity.java

    package pens.pensnews;import android.os.Bundle;import android.support.design.widget.FloatingActionButton;import android.support.design.widget.Snackbar;import android.support.v4.app.FragmentManager;import android.support.design.widget.NavigationView;import android.support.v4.view.GravityCompat;import android.support.v4.widget.DrawerLayout;import android.support.v7.app.ActionBarDrawerToggle;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.Toolbar;import android.view.Menu;import android.view.MenuItem;import android.view.View;
    public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
        toggle.syncState();
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    FragmentManager manager = getSupportFragmentManager();

        Kampus kampus = new Kampus();
        manager.beginTransaction().replace(R.id.MainLayout, kampus).commit();
    Akademik akademik = new Akademik();
        manager.beginTransaction().replace(R.id.MainLayout, akademik).commit();
    Nonak nonak = new Nonak();
        manager.beginTransaction().replace(R.id.MainLayout, nonak).commit();
    Beranda beranda = new Beranda();
        manager.beginTransaction().replace(R.id.MainLayout, beranda).commit();
    }
    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
    drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }}
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.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.nav_help) {
    return true;
        }

        return super.onOptionsItemSelected(item);}
    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
    int id = item.getItemId();

        if (id == R.id.nav_beranda) {
            Beranda beranda = new Beranda();
    FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.MainLayout, beranda).commit();
        } else if (id == R.id.nav_kampus) {
            Kampus kampus = new Kampus();
    FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.MainLayout, kampus).commit();
        } else if (id == R.id.nav_akademik) {
            Akademik akademik = new Akademik();
    FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.MainLayout, akademik).commit();
        }else if (id == R.id.nav_non) {
            Nonak nonak = new Nonak();
    FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.MainLayout, nonak).commit();
        } else if (id == R.id.nav_about) {
            About about = new About();
    FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.MainLayout, about).commit();
        }
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }}
&#13;
&#13;
&#13;

&#13;
&#13;
AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="pens.pensnews">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
        android:theme="@style/AppTheme">
        <activity
            android:name="pens.pensnews.Splashscreen"
    android:label="@string/app_name"
            android:theme="@style/Splashscreen"
            android:noHistory="true">
            <intent-filter>
    <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
    </activity>
        <activity
            android:name="pens.pensnews.MainActivity"
     android:label="@string/app_name" >
        </activity>
        <activity android:name=".Mawapres" />
        <activity android:name=".Akademik1" />
    <activity android:name=".Akademik2" />
        <activity android:name=".Kampus1" />
        <activity android:name=".Kampus2" />
        <activity android:name=".Kampus3" />
    <activity android:name=".Nonak1" />
        <activity android:name=".Nonak2" />
        <activity android:name=".Nonak3" />
        <activity android:name=".Nonak4"></activity>
    </application>

</manifest>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

在AndroidManifest.xml中添加此行

机器人:主题= “@风格/ AppTheme.NoActionBar”

添加此行后,您的文件将如下所示..

 <activity
            android:name="pens.pensnews.Splashscreen"
    android:label="@string/app_name"
            android:theme="@style/Splashscreen"
    android:theme="@style/AppTheme.NoActionBar"
            android:noHistory="true">
            <intent-filter>
    <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
    </activity>

Shiena如果此代码适用于您,请将此问题标记为您的答案。