Navigation Drawer 方法收到 NullPointer 异常
Posted
技术标签:
【中文标题】Navigation Drawer 方法收到 NullPointer 异常【英文标题】:Navigation Drawer Method receives NullPointer Exception 【发布时间】:2017-09-23 23:52:21 【问题描述】:我正在创建一个使用导航抽屉的 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);
如果有人能帮忙,将不胜感激
【问题讨论】:
你在哪一行得到了 NPE?请写在答案中。 能否粘贴异常信息。 What is a NullPointerException, and how do I fix it?的可能重复 如果NavigationDrawer
没有被用作Activity
,它不应该扩展AppCompatActivity
。
@AlexanderPodkutin 我在“ navigator.createDrawer(PedidosActivity.this, toolbar);” 行中获得了 NPE
【参考方案1】:
它的工作,tks @MikeM,就是你所说的。这是答案
这个方法是我在 Drawer onClickListener 内部调用的;
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));
这里我称之为,对于每个活动:
NavigationDrawer navigator = new NavigationDrawer();
navigator.createDrawer(PerfilActivity.this, toolbar);
【讨论】:
以上是关于Navigation Drawer 方法收到 NullPointer 异常的主要内容,如果未能解决你的问题,请参考以下文章
drawer navigation, tabhostFragment 默认导向
向@react-navigation/drawer 组件发送道具
有条件的 Drawer.Navigator 的 openByDefault 属性无法正常工作(“@react-navigation/drawer”:“^5.12.5”)