基本导航抽屉活动在addDrawerListener()

时间:2017-09-04 09:55:14

标签: android android-layout navigation navigation-drawer drawerlayout

我想创建一个基本导航抽屉。因此,我可以选择活动是否需要导航抽屉。 我的课程中肯定会出现一些问题,当我无法使用此NavDrawer打开一个活动时,我已经在stackoverflow上尝试了其他已发布的解决方案来修复它,但它没有成功。

嗯,我得到的是在尝试调用虚方法android.support.v4.widget.DrawerLayout.addDrawerListener(android.support.v4.widget.DrawerLayout$DrawerListener) on a null object reference

时出现空指针异常

由基类中的addDrawListener和子类中的super.onCreate触发

提前感谢您的任何输入。

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为空,只有工具:contex设置为Base Nav Drawer类并使用约束布局

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基于Androids官方模板 并分为标题和菜单布局,并在之前工作。

<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" />

以下是我如何继承Nav抽屉类:

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));             
        }
    });
}

0 个答案:

没有答案