I am trying to incorporate material design into an existing project that uses NavigationFragment. So I'm trying to use a toolbar instead of an ActionBar. I followed this tutorial and I replaced all getActionBar() with getSupportActionBar() , but my application always crashes on startup.
activity_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.andreapivetta.mypckg.MainActivity"> <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" /> <fragment android:id="@+id/navigation_drawer" android:name="com.andreapivetta.mypckg.NavigationDrawerFragment" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" /> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimaryDark"/> </android.support.v4.widget.DrawerLayout>
Mainactivity
public class MainActivity extends ActionBarActivity implements NavigationDrawerFragment.NavigationDrawerCallbacks { private NavigationDrawerFragment mNavigationDrawerFragment; private CharSequence mTitle; private static SharedPreferences mSharedPreferences; static final String PREF_KEY_TWITTER_LOGIN = "isTwitterLogedIn"; static final String PREF_SELECTED_INDEX = "SELECTED_POSITION"; private ConnectionDetector connectionDetector; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); if (toolbar != null) { setSupportActionBar(toolbar); } mSharedPreferences = getSharedPreferences("MyPref", 0); setContentView(R.layout.activity_main); connectionDetector = new ConnectionDetector(this); startService(new Intent(getApplicationContext(), StartupService.class)); } @Override public void onResume() { super.onResume(); if (isTwitterLoggedInAlready()) { mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.navigation_drawer); mTitle = getTitle(); DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); setDrawerLeftEdgeSize(this, mDrawerLayout, 0.3f); mNavigationDrawerFragment.setUp(R.id.navigation_drawer, mDrawerLayout); if (mSharedPreferences.getBoolean("FIRST_LAUNCH", true)) { mSharedPreferences.edit().putBoolean("FIRST_LAUNCH", false).apply(); } } else { if (connectionDetector.isConnectingToInternet()) { Fragment fragment = new LoginFragment(); FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.container, fragment).commit(); } else { Toast.makeText(this, "internet connection required", Toast.LENGTH_LONG).show(); } } } @Override public void onNavigationDrawerItemSelected(int position) { Fragment fragment; FragmentManager fragmentManager = getSupportFragmentManager(); mSharedPreferences.edit().putInt(PREF_SELECTED_INDEX, position).apply(); switch (position) { case 0: fragment = new MainFragment(); fragmentManager.beginTransaction() .replace(R.id.container, fragment).commit(); setTitle(getResources().getString(R.string.app_name)); break; case 1: ... case 2: ... case 3: ... } } @Override public void setTitle(CharSequence title) { mTitle = title; getSupportActionBar().setTitle(mTitle); } public void restoreActionBar() { android.support.v7.app.ActionBar actionBar = getSupportActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); actionBar.setDisplayShowTitleEnabled(true); actionBar.setTitle(mTitle); } }
Journal
10-28 14:20:29.085 32367-32367/com.andreapivetta.mypckg E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.andreapivetta.mypckg, PID: 32367 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.andreapivetta.mypckg/com.andreapivetta.mypckg.MainActivity}: android.view.InflateException: Binary XML file line
What am I doing wrong?
source share