首页 > 解决方案 > 空指针,底部导航的活动意图中的上下文

问题描述

这个应用程序有两个活动,我想用 BottomNavigationView 在它们之间切换。在 switch 语句中声明意图会引发空指针异常。MainActivity.java 更改为 SpecialsActivity.java。第二个活动是 PizzaActivity。底部导航由 BottomNavigationBuilder.java 控制。

示例堆栈跟踪:

W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference

我试过的:

Intent PizzaIntent = new Intent(this, PizzaActivity.class);
                                    //Intent PizzaIntent = new Intent(getCallingActivity(), PizzaActivity.class);
                                    //Intent PizzaIntent = new Intent( getBaseContext(),PizzaActivity.class);
                                    //Intent PizzaIntent = new Intent(BottomNavigationBuilder.this, PizzaActivity.class);
                                    //Intent PizzaIntent = new Intent(getApplicationContext(), PizzaActivity.class);
                                    startActivity(PizzaIntent);

BottomNavigationBuilder.java

public class BottomNavigationBuilder extends AppCompatActivity {

    private Context context;
    private BottomNavigationView bottomNavigation;

    public BottomNavigationBuilder(Context context, BottomNavigationView findViewById) {
        this.context = context;
        this.bottomNavigation = findViewById;
    }

    public BottomNavigationBuilder setBaseConfig() {
        setTextVisible();
        setSelectedListener();
        return this;
    }

    private void setTextVisible() {
        bottomNavigation.setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_LABELED);
    }

    public void displayToast(String message) {
        Toast.makeText(this.context, message,
                Toast.LENGTH_SHORT).show();
    }

    private void setSelectedListener() {
        BottomNavigationView.OnNavigationItemSelectedListener navigationItemSelectedListener =
                new BottomNavigationView.OnNavigationItemSelectedListener() {

                    String url;

                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                        switch (item.getItemId()) {

                            case R.id.action_specials:
                                displayToast("Specials clicked");
                                break;

                            case R.id.action_pizza:
                                displayToast("Pizza clicked");

                                try {
                                    Intent PizzaIntent = new Intent(this, PizzaActivity.class);
                                    //Intent PizzaIntent = new Intent(getCallingActivity(), PizzaActivity.class);
                                    //Intent PizzaIntent = new Intent( getBaseContext(),PizzaActivity.class);
                                    //Intent PizzaIntent = new Intent(BottomNavigationBuilder.this, PizzaActivity.class);
                                    //Intent PizzaIntent = new Intent(getApplicationContext(), PizzaActivity.class);
                                    startActivity(PizzaIntent);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }


                                break;

                            case R.id.action_stromboli:
                                displayToast("Stromboli clicked");
                                break;

                            case R.id.action_salad:
                                displayToast("Salad clicked");
                                break;

                            case R.id.action_drinks:
                                displayToast("Drinks clicked");
                                break;

                            default:
                                // none
                        }
                        return true;
                    }
                };
        bottomNavigation.setOnNavigationItemSelectedListener(navigationItemSelectedListener);
    }

    public BottomNavigationView getBottomNavigation() {
        return bottomNavigation;
    }
}

SpecialsActivity.java

public class SpecialsActivity extends AppCompatActivity {

    private BottomNavigationView bottomNavigation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_specials);
        setItemId();
        setView();
    }

    private void setItemId() {
        bottomNavigation = findViewById(R.id.bottom_nav);
    }

    private void setView() {
        initNavigationView();
    }

    private void initNavigationView() {
        bottomNavigation = new BottomNavigationBuilder(this, bottomNavigation)
                .setBaseConfig()
                .getBottomNavigation();
    }

}

标签: javaandroid

解决方案


您不能直接实例化活动。您不能直接在它们之间传递组件。你甚至不需要 Activity 。

删除活动扩展:

public class BottomNavigationBuilder {
    //...
}

然后只需将您的context参考用于任何需要上下文(new Intent(context, PizzaActivity.class)context.startActivity()等)的东西。

bottomNavigation在将其传递给构建器后,您也不需要重新分配。bottomNavigation这是同一个实例,因此对 Builder 内部所做的任何更改也会对bottomNavigation外部进行。


推荐阅读