空指针,活动意图中的上下文用于底部导航

时间:2019-01-12 22:21:44

标签: java android

此应用程序有两个活动,我想使用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();
    }

}

2 个答案:

答案 0 :(得分:1)

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

删除“活动”扩展程序:

public class BottomNavigationBuilder {
    //...
}

然后仅将context引用用于需要上下文的任何内容(new Intent(context, PizzaActivity.class)context.startActivity()等)。

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

答案 1 :(得分:0)

出现此错误的原因可能是由于您放置此语句的位置。目前,它就像:

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    //.....
    Intent PizzaIntent = new Intent(this, PizzaActivity.class);

尝试通过以下操作将this构造函数中的Intent关键字更改为ApplicationContext

//Enter your Application's context below.
Intent PizzaIntent = new Intent(context, PizzaActivity.class);

现在,IntentonNavigationItemSelected内部应该可以正常工作。 context来自类的构造函数。但是,由于您的班级扩展了AppCompatActivity,因此您也可以执行getApplicationContext()。如果您有任何疑问或问题,可以在下面发布。