James1234
James1234

Reputation: 111

Activity intent to Navigation Drawer Activity (App Crashes)

My application crashes after the activity is being intent on the navigation drawer which is the main activity...Could someone help me please...

Below is my code for Introduction.java

public class Introduction extends AppCompatActivity{

    //POP UPS
    ImageView getstart;

    //image button
    MediaPlayer background_music;
    int length;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.introduction);
        bmusic();
        getstart=(ImageView)findViewById(R.id.playnow);
        final MediaPlayer button= MediaPlayer.create(this,R.raw.intro_button);
        getstart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                button.start();
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {

                        Intent goMain= new Intent(Introduction.this,MainActivity.class);
                        startActivity(goMain);
                        finish();
                    }
                }, 1000);
            }


        });


    }

    public void bmusic() {
        background_music = MediaPlayer.create(this, R.raw.intro_adventure);
        background_music.setLooping(true);
        background_music.start();
    }


    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        background_music.pause();
        length = background_music.getCurrentPosition();
    }

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
       background_music.seekTo(length);
        background_music.start();
    }


}

And here is my code in MainActivity.java

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    private DrawerLayout drawer;


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


        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open
                , R.string.navigation_drawer_close);

        drawer.addDrawerListener(toggle);
        toggle.syncState();

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new fragment_home()).commit();
            navigationView.setCheckedItem(R.id.nav_home);
        }

    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {

        switch (item.getItemId()) {

            case R.id.nav_home:

                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new fragment_home()).commit();
                break;
            case R.id.nav_tips:

                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new fragment_tips()).commit();
                break;

            case R.id.nav_about:

                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new fragment_about()).commit();
                break;


            case R.id.share:

                Toast.makeText(this, "Share", Toast.LENGTH_SHORT).show();
                break;

            case R.id.rate:

                Toast.makeText(this, "Rate this app", Toast.LENGTH_SHORT).show();
                break;

        }
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public void onBackPressed() {
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }
}

Here is the error on my logcat

E/HW-JPEG-DEC: [HME_JPEG_DEC_Delete](3321): HME_JPEG_DEC_Delete: decoder_ctx=null
E/HW-JPEG-DEC: [HME_JPEG_DEC_Delete](3321): HME_JPEG_DEC_Delete: decoder_ctx=null
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.james.navigate, PID: 4947
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.james.navigate/com.james.navigate.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:2793)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864)
                      at android.app.ActivityThread.-wrap12(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
                      at android.os.Handler.dispatchMessage(Handler.java:105)
                      at android.os.Looper.loop(Looper.java:156)
                      at android.app.ActivityThread.main(ActivityThread.java:6523)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
                   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:201)
                      at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:129)
                      at com.james.navigate.MainActivity.onCreate(MainActivity.java:26)
                      at android.app.Activity.performCreate(Activity.java:6915)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2746)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864) 
                      at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567) 
                      at android.os.Handler.dispatchMessage(Handler.java:105) 
                      at android.os.Looper.loop(Looper.java:156) 
                      at android.app.ActivityThread.main(ActivityThread.java:6523) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832) 

Upvotes: 1

Views: 663

Answers (1)

ʍѳђઽ૯ท
ʍѳђઽ૯ท

Reputation: 16976

This happens because your styles has an ActionBar and in your java codes, you declared the Toolbar.

So, add this to your styles.xml:

<item name="windowActionBar">false</item> 

This will tell that this theme doesn't have ActionBar but you can declare Toolbar in java side

Or just use a parent without ActionBar:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

The second one is the easiest way to do that.

Update: for AndroidX or up to 28 API:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">

Upvotes: 2

Related Questions