0

I am trying to build a navigation drawer activity and this error would show up whenever the main activity is to be displayed and crashes the app.

xml code:

<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="?attr/colorPrimaryDark"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

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

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

<android.support.design.widget.FloatingActionButton
    android:id="@+id/Button_sign_out"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@drawable/ic_arrow_forward_black_24dp"
    />

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

this is my java code:

package com.android.example.ithelpdesk;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
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;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

public DrawerLayout drawer;
public Toolbar supportActionBar;
private boolean mToolBarNavigationListenerIsRegistered  = false;
ActionBarDrawerToggle toggle;

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

    populateTicketList();

   Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    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.addDrawerListener(toggle);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) 
     findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    ListView listView = (ListView) findViewById(R.id.lvSubcategory);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

  @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int 
       i, long l) {
       Intent intent = new Intent(MainActivity.this, ComplaintDetail.class);
                startActivity(intent);
        }
    });

}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

/*private void signOut() {
    mGoogleSignInClient.signOut()
            .addOnCompleteListener(this, new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    // ...
                }
            });
}*/

@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.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public void setSupportActionBar(Toolbar supportActionBar) {
    this.supportActionBar = supportActionBar;
}


private void populateTicketList() {
    ArrayList<Ticket> arrayOfTicket = Ticket.getTicket();

    TicketAdapter adapter = new TicketAdapter(this, arrayOfTicket);
    ListView listView = (ListView) findViewById(R.id.lvTicket);
    listView.setAdapter(adapter);

}

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

    if (id == R.id.nav_complaint) {
        Intent intent = new Intent(MainActivity.this, TicketCategory.class);
        startActivity(intent);
    }
    else if (id == R.id.nav_profile) {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }// else if (id == R.id.nav_slideshow) {

     //  } else if (id == R.id.nav_manage) {

   // }
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

}

the crash from the log cat:

pls help. I am a beginner so I would appreciate an explanation regarding this problem too as to what is the issue and how can i avoid it in the future.

Here is the logcat:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.example.ithelpdesk/com.android.example.ithelpdesk.MainActivity}: java.lang.ClassCastException: android.support.design.widget.CoordinatorLayout cannot be cast to android.support.v7.widget.Toolbar at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2455) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2515) at android.app.ActivityThread.access$1000(ActivityThread.java:154) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1379) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:157) at android.app.ActivityThread.main(ActivityThread.java:5571) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:745) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635) Caused by: java.lang.ClassCastException: android.support.design.widget.CoordinatorLayout cannot be cast to android.support.v7.widget.Toolbar at com.android.example.ithelpdesk.MainActivity.onCreate(MainActivity.java:35) at android.app.Activity.performCreate(Activity.java:6357) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2408) ... 9 more

2
  • 1
    Post the crash from logcat, thanks
    – jantursky
    Jun 7, 2018 at 10:58
  • 1
    clean and rebuild your app Jun 7, 2018 at 14:20

1 Answer 1

0

You have not implemented DrawerLayout and NavigationView Layout in xml code yet. If you are not known to this layout, then try below link

How to create a custom navigation drawer in android

1
  • If im not wrong, arent those supposed to be implemented in other xml files? i just posted the app_bar_main xml file. Also, I am using the template available in the android studio. Appreciate your help tho... Jun 7, 2018 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.