i want to create a Base Navigation Drawer. So that i´m able to choose if a Activity needs a Navigation Drawer or not. There must be something going very wrong in my class, when im not able to open a Activity with this NavDrawer and i already tried other posted solutions here on stackoverflow to fix it but it didnt worked out.
Well, what i get is a Null Pointer Exception at Attempting to invoke virtual method android.support.v4.widget.DrawerLayout.addDrawerListener(android.support.v4.widget.DrawerLayout$DrawerListener) on a null object reference
triggered by addDrawListener in the Base Class and super.onCreate in the Child Class
Thank you in advance for any input.
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
public class NavDrawerActivity extends LoggingHelper implements
NavigationView.OnNavigationItemSelectedListener {
public DrawerLayout m_drawerLayout;
private ActionBarDrawerToggle m_drawerToggle;
private TextView m_userName;;
UserObjectDB m_DBuser = new UserObjectDB();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bnav_drawer);
// R.id.drawer_layout should be in every activity with exactly the same id.
m_drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
m_drawerToggle = new ActionBarDrawerToggle(this, m_drawerLayout, R.string.drawer_open, R.string.drawer_close){};
m_drawerLayout.addDrawerListener(m_drawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.nav_chooseRegion:
Intent intent = new Intent(this, ChooseRegion.class);
startActivity(intent);
finish();
break;
case R.id.nav_chooseRoute:
break;
case R.id.nav_logout:
//DialogMainClass.dialogLogout();
//Todo: logout in dialogLogout(); via setLogout(true);
finish();
default:
break;
}
m_drawerLayout.closeDrawer(GravityCompat.START);
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.
if (m_drawerToggle.onOptionsItemSelected(item))
{
m_userName = (TextView) findViewById(R.id.usernameViewNav); // Global?
m_userName.setText(m_DBuser.getAllUserObjects().getUsernameAtIndex(0));
return true;
}
return super.onOptionsItemSelected(item);
}
}
R.layout.activity_bnav_drawer is empty, which only has tools:contex set to the Base Nav Drawer class and uses a constraint layout
drawer_layout:
<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.graphhopper.android.cleanedActivities.CleanedMainActivity">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Nav_View is based of Androids official Template and is split into header and menu layout and worked before.
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/Base.V22.Theme.AppCompat"
tools:openDrawer="start">
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
Here is how i inherit the Nav Drawer Class:
public class MainActivity extends NavDrawerActivity implements ICallbackManager {
ListView listView;
Auth m_Auth;
Intent intent;
private int m_RowID;
public MainActivity() throws JSONException {}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
m_Auth = new Auth(MainActivity.this);
listView = (ListView) findViewById(R.id.list_items);
DataBaseHelper.init(MainActivity.this);
try {
m_Auth.performAsyncHttpRoutesPostRequest();
} catch (JSONException e) {
e.printStackTrace();
}
// TODO: Move it maybe out of onCreate
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("MainActivity", listView.getItemAtPosition(position).toString());
Log.i("MainAcitivty:pos", String.valueOf(position));
}
});
}