I'm trying to include material design on an existing project which use a NavigationFragment.
So I'm trying to use Toolbar instead of ActionBar.
I followed this guide, and I replaced all getActionBar()
to getSupportActionBar()
, but my App always crash at 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);
}
}
Log
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 #20: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
Caused by: android.view.InflateException: Binary XML file line #20: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
at com.andreapivetta.mypckg.MainActivity.onCreate(MainActivity.java:56)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
at com.andreapivetta.mypckg.MainActivity.setTitle(MainActivity.java:155)
at com.andreapivetta.mypckg.MainActivity.onNavigationDrawerItemSelected(MainActivity.java:128)
at com.andreapivetta.mypckg.NavigationDrawerFragment.selectItem(NavigationDrawerFragment.java:247)
at com.andreapivetta.mypckg.NavigationDrawerFragment.onCreate(NavigationDrawerFragment.java:93)
at android.app.Fragment.performCreate(Fragment.java:1678)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:859)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040)
at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1142)
at android.app.Activity.onCreateView(Activity.java:4786)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
at com.andreapivetta.mypckg.MainActivity.onCreate(MainActivity.java:56)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
What am I doing wrong?
in your onCreate
you never set the content before you try to access the toolbar view
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
mSharedPreferences = getSharedPreferences("MyPref", 0);
setContentView(R.layout.activity_main);
you need to set the content first for your activity before you can access it
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
mSharedPreferences = getSharedPreferences("MyPref", 0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With