在选项卡式活动中实现导航抽屉

时间:2016-05-14 13:31:50

标签: android navigation-drawer

我想在选项卡式活动中实现导航抽屉,但每当我运行应用程序时,它都会显示

Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.setDrawerListener(android.support.v4.widget.DrawerLayout$DrawerListener)' on a null object reference

这是我的TabHome活动
有人可以帮帮我 此外,当我带上 SetSupportActionBar(toolbar); 时,它会显示红色,表示我们无法解决此问题。
有人可以帮帮我

package com.example.user.zersey;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.Toast;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

/**
 * Created by USER on 26-02-2016.
 */
public class TabHome extends TabActivity implements     NavigationView.OnNavigationItemSelectedListener{
Intent intent;
int counter =0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabs);
    Resources ressources = getResources();
    TabHost tabHost = getTabHost();
    TabSpec spec;
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    //setSupportActionBar(toolbar);


    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);




    intent = new Intent().setClass(this, TabInHome.class);
    spec = tabHost.newTabSpec("1").setIndicator("",ressources.getDrawable(R.drawable.home))
            .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this,TabInSearch.class );
    spec = tabHost.newTabSpec("2").setIndicator("",ressources.getDrawable(R.drawable.category))
            .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, TabInCategory.class);
    spec = tabHost.newTabSpec("3").setIndicator("",ressources.getDrawable(R.drawable.search))
            .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, Posts.class);
    spec = tabHost.newTabSpec("4").setIndicator("",ressources.getDrawable(R.drawable.notify))
            .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, UserAccount.class);
    spec = tabHost.newTabSpec("2").setIndicator("",ressources.getDrawable(R.drawable.user))
            .setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(0);


}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    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.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_home) {

    } else if (id == R.id.nav_profile) {

    } else if (id == R.id.nav_bookmarks) {

    } else if (id == R.id.nav_faq) {

    } else if (id == R.id.nav_appshare) {

    } else if (id == R.id.nav_feedback) {

    } else if (id == R.id.nav_settings) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
   }
}

0 个答案:

没有答案