I have a code module which implements viewpager with navigation drawer, however, when I run the code I get the following error
01-26 09:20:02.958: D/AndroidR
in this line in your activity:
super.onCreate(savedInstanceState);
setContentView(R.layout.Activity_Main);
use this:
super.onCreate(savedInstanceState);
setContentView(R.layout.*);
*
is your activity
If you encounter this error
"java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setTitle (java.lang.CharSequence)' on a null object reference. "
Just use
setSupportActionBar (toolbar).
Try to check here
res >> values >> styles.xml
make sure that there no code like this
<item name="windowActionBar">false</item>
if there are code like that, you can disable for a while, or erase it
You should try this one. I think it will work.
Toolbar toolbar = findViewById(R.id.toolbar1);
setSupportActionBar(toolbar);
mDrawerLayout = findViewById(R.id.drawer_layout);
mDrawerLayout = findViewById(R.id.drawer_layout);
mDrawerLayout.setDrawerShadow(R.drawable.rectagle_with_black_outer,
GravityCompat.START);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
toolbar, R.string.navigation_drawer_close,
R.string.navigation_drawer_close) {
public void onDrawerClosed(View view) {
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
invalidateOptionsMenu();
}
};
Your code is throwing on com.example.tabwithslidingdrawer.MainActivity.onCreate(MainActivity.java:95)
:
// enabling action bar app icon and behaving it as toggle button
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
The problem is pretty simple- your Activity
is inheriting from the new android.support.v7.app.ActionBarActivity. You should be using a call to getSupportActionBar() instead of getActionBar().
If you look above around line 65 of your code you'll see that you're already doing that:
actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
// TODO: Remove the redundant calls to getSupportActionBar()
// and use variable actionBar instead
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
And then lower down around line 87 it looks like you figured out the same:
getSupportActionBar().setTitle(
Html.fromHtml("<font color=\"black\">" + mTitle + " - "
+ menutitles[0] + "</font>"));
// getActionBar().setTitle(mTitle +menutitles[0]);
Notice how you commented out getActionBar()
.
Whenever such an error occurs. Try to check Following Things
Check what kind of Activity is being used, is it a simple android.app Activity or an AppCompatActivity or an ActionBarActivity and so on.
Check if your activity type which is extended falls under the compat category
example android.app based Activity/Fragment are non appCompat types, whereas android.support.v4.app.Fragment or android.support.v4.app.ActivityCompat are appCompat based
if it falls under appCompat we use getSupportActionBar() else for android.app types we can use getActionBar()
example: In the manifest file if theme applied is say android:theme="@android:style/Theme.Holo.Dialog" getActionBar() will work
but if theme applied for the activity in the manifest is as follows android:theme="@style/Theme.AppCompat.Light" then you have to use getSupportActionBar()