第22行底部导航助手的空指针异常

时间:2018-01-07 18:06:28

标签: android

bottomnavigationHelper类,其中存在nullpointer异常 当我尝试加载配置文件activyt页面时,我的应用程序崩溃,并且错误在bottomNavigationHelper中显示空指针异常。

public class bottomNavigationHelper {
    public  void enableNavigation(final Context context, BottomNavigationView obj)
    {
        obj.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId())
                {
                    case R.id.bbn_item1:
                        Intent intent1=new Intent(context, HomeActivity.class);
                        context.startActivity(intent1);
                        break;
                    case R.id.bbn_item2:
                        Intent intent2=new Intent(context, SearchActivity.class);
                        context.startActivity(intent2);
                        break;
                    case R.id.bbn_item3:
                        Intent intent3=new Intent(context, LikesandGroupActivity.class);
                        context.startActivity(intent3);


              break;
                        case R.id.bbn_item4:
                            Intent intent4=new Intent(context, ProfileActivity.class);
                            context.startActivity(intent4);
                            break;



                    }
                    return false;
                }
            });
        }
    }

Profile Activity是bottomnavigation的主要活动 当我尝试加载配置文件activyt页面时,我的应用程序崩溃,并且错误在bottomNavigationHelper中显示空指针异常。

public class ProfileActivity extends AppCompatActivity {
    private Integer Activity_num=3;
    FirebaseAuth mAuth=FirebaseAuth.getInstance();
    FirebaseUser currentuser=mAuth.getCurrentUser();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);
        Button SignoutFacebook=(Button)findViewById(R.id.signout);
        SignoutFacebook.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAuth.signOut();
                LoginManager.getInstance().logOut();
                updateUI();

            }
        });
        setupBottomNavigationView();


    }
    private void setupBottomNavigationView(){
        Toast.makeText(this,"setupBottomNavigationView starts",Toast.LENGTH_SHORT).show();
        BottomNavigationView bottom=(BottomNavigationView)findViewById(R.id.bottom_nav_view);
        bottomNavigationHelper bottomNavigationHelper=new bottomNavigationHelper();
        bottomNavigationHelper.enableNavigation(ProfileActivity.this,bottom);
        Menu menu=bottom.getMenu();
        MenuItem menuitem=menu.getItem(Activity_num);
        menuitem.setChecked(true);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if(currentuser==null)
        {
            updateUI();
        }
    }

    private void updateUI() {
        startActivity(new Intent(ProfileActivity.this,LoginActivity.class));
        finish();
    }

}

0 个答案:

没有答案