导航抽屉方法接收NullPointer异常

时间:2017-04-26 14:05:43

标签: java android android-activity nullpointerexception navigation-drawer

我正在创建一个使用导航抽屉的Android应用程序。由于我在抽屉中有多个选项,我必须创建一个方法来传递我想要实现导航和活动工具栏的活动。

问题是,当我在活动中声明NavigatioDrawer方法时,我收到一个空指针。让我告诉你:

这是我的导航抽屉方法(我将参加活动):

        package studio.brunocasamassa.ajudaaqui.helper;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.mikepenz.materialdrawer.AccountHeader;
import com.mikepenz.materialdrawer.AccountHeaderBuilder;
import com.mikepenz.materialdrawer.Drawer;
import com.mikepenz.materialdrawer.DrawerBuilder;
import com.mikepenz.materialdrawer.model.DividerDrawerItem;
import com.mikepenz.materialdrawer.model.PrimaryDrawerItem;
import com.mikepenz.materialdrawer.model.ProfileDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IProfile;

import studio.brunocasamassa.ajudaaqui.MainActivity;
import studio.brunocasamassa.ajudaaqui.PerfilActivity;
import studio.brunocasamassa.ajudaaqui.R;
import studio.brunocasamassa.ajudaaqui.ConfiguracoesActivity;
import studio.brunocasamassa.ajudaaqui.ChatActivity;
import studio.brunocasamassa.ajudaaqui.GruposActivity;
import studio.brunocasamassa.ajudaaqui.PedidosActivity;
import studio.brunocasamassa.ajudaaqui.SobreActivity;

/**
 * Created by bruno on 24/04/2017.
 */

public class NavigationDrawer {


    //NAVIGATION DRAWER
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    //private static Activity setClasse = new Activity();
    private static MainActivity main;
    private static User usuario = main.user;

    public void createDrawer(final Activity classe, Toolbar toolbar) {
        //setClasse = classe;
        //Itens do Drawer
        PrimaryDrawerItem item1 = new PrimaryDrawerItem().withIdentifier(1).withName(R.string.menu_pedidos);
        PrimaryDrawerItem item2 = new PrimaryDrawerItem().withIdentifier(2).withName(R.string.menu_chats);
        PrimaryDrawerItem item3 = new PrimaryDrawerItem().withIdentifier(3).withName(R.string.menu_grupos);
        PrimaryDrawerItem item4 = new PrimaryDrawerItem().withIdentifier(4).withName(R.string.menu_perfil);
        PrimaryDrawerItem item5 = new PrimaryDrawerItem().withIdentifier(5).withName(R.string.menu_configuracoes);
        PrimaryDrawerItem item6 = new PrimaryDrawerItem().withIdentifier(6).withName(R.string.menu_sobre);

        // Create the Navigation Drawer AccountHeader
        AccountHeader headerResult = new AccountHeaderBuilder()
                .withActivity(classe)
                .withHeaderBackground(R.color.colorPrimary)
                .addProfiles(
                        new ProfileDrawerItem().withName("User").withEmail("user@example.com").withIcon(usuario.getProfileImageURL())
                )
                .withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
                    @Override
                    public boolean onProfileChanged(View view, IProfile profile, boolean currentProfile) {
                        return false;
                    }
                })
                .build();

        //Definition Drawer
        Drawer drawer = new DrawerBuilder()
                .withActivity(classe)
                .withToolbar(toolbar)
                .withAccountHeader(headerResult)
                .addDrawerItems(
                        item1,
                        new DividerDrawerItem(),//Divisor
                        item2,
                        new DividerDrawerItem(),//Divisor
                        /*DIVISAO COM MENSAGEM new SectionDrawerItem().withName(R.string.section),//Seção*/
                        item3,
                        new DividerDrawerItem(),//Divisor
                        item4,
                        new DividerDrawerItem(),//Divisor
                        item5,
                        new DividerDrawerItem(),//Divisor
                        item6
                        //Divisor
                )
                .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                    @Override
                    public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {

                        StartActivity a = new StartActivity()   ;
                        a.verifyActivity(classe, position);
                        return false;
                    }
                })
                .withSelectedItemByPosition(0)
                .build();
    }


}


class StartActivity extends AppCompatActivity {


    public void verifyActivity(Activity classe, int position) {
        if (position == 1) {
            // HERE I AM TRYING USING DIFFERENT FORMS TO START THE ACTITIVIES
            startActivity(new Intent(classe, PedidosActivity.class));
        }
        if (position == 3) {
            startActivity(new Intent(classe, ChatActivity.class));
        }
        if (position == 5) {
            startActivity(new Intent(classe, GruposActivity.class));
        }
        if (position == 7) {
            startActivity(new Intent(classe, PerfilActivity.class));
        }
        if (position == 9) {
            startActivity(new Intent(classe, SobreActivity.class));
        }
    }

}

这是我的PedidosActivity(我在这里称之为导航器):

    package studio.brunocasamassa.ajudaaqui;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ListView;

import com.facebook.login.LoginResult;

import studio.brunocasamassa.ajudaaqui.helper.NavigationDrawer;
import studio.brunocasamassa.ajudaaqui.helper.PedidosTabAdapter;
import studio.brunocasamassa.ajudaaqui.helper.SlidingTabLayout;
import studio.brunocasamassa.ajudaaqui.helper.User;

/**
 * Created by bruno on 24/04/2017.
 */

public class PedidosActivity extends AppCompatActivity {
    private Toolbar toolbar;
    private ListView listview_nomes;
    private ViewPager viewPager;
    private SlidingTabLayout slidingTabLayout;
    public int posicao;
    private LoginResult loginResult;
    private MainActivity main;
    private User usuario;
    private static NavigationDrawer navigator = new NavigationDrawer();


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hello);

        toolbar = (Toolbar) findViewById(R.id.toolbar_principal);
        toolbar.setTitle(getResources().getString(R.string.menu_pedidos));
        //toolbar.setTitleTextColor(getResources().getColor(R.color.colorPrimaryDark));
        setSupportActionBar(toolbar);

        /*
        loginResult = MainActivity.lr;
        Profile profile = Profile.getCurrentProfile();
        System.out.println("PROFILE: "+ profile);

        String userid = loginResult.getAccessToken().getUserId();
        String name = message(profile);
        user.setName(name);*/

        usuario = main.user;
        System.out.println("USER: " + usuario.getName());
        String username = usuario.getName();
        loginResult=main.lr;
        String userImg = usuario.getProfileImg();

        listview_nomes = (ListView) findViewById(R.id.ListContatos);
        viewPager = (ViewPager) findViewById(R.id.vp_pagina);
        slidingTabLayout = (SlidingTabLayout) findViewById(R.id.stl_tabs);
        slidingTabLayout.setDistributeEvenly(true);
        slidingTabLayout.setSelectedIndicatorColors(ContextCompat.getColor(this, R.color.colorAccent));

        PedidosTabAdapter pedidosTabAdapter = new PedidosTabAdapter(getSupportFragmentManager());
        viewPager.setAdapter(pedidosTabAdapter);

        slidingTabLayout.setViewPager(viewPager);


        navigator.createDrawer(PedidosActivity.this, toolbar);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.action_exit:
                //logoutUser();
                return true;
            case R.id.action_settings:
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }

    }


}

如果有人可以提供帮助,那将非常感激

1 个答案:

答案 0 :(得分:0)

ITS工作,请@MikeM,就是你所说的。这是答案

我在Drawer onClickListener中调用此方法;

<div id="btn">Click Me to Toggle Classes</div>

这里我称之为每个活动:

   private void verifyActivity(Activity classe, int position) {
            if (position == 1) {
                // HERE I AM TRYING USING DIFFERENT FORMS TO START THE ACTITIVIES
                classe.startActivity(new Intent(classe, PedidosActivity.class));
            }
            if (position == 3) {
                classe.startActivity(new Intent(classe, ChatActivity.class));
            }
            if (position == 5) {
                classe.startActivity(new Intent(classe, GruposActivity.class));
            }
            if (position == 7) {
                classe.startActivity(new Intent(classe, PerfilActivity.class));
            }
            if (position == 9) {
                classe.startActivity(new Intent(classe, SobreActivity.class));
            }