java - 空指针,底部导航的活动意图中的上下文
问题描述
这个应用程序有两个活动,我想用 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();
}
}
解决方案
您不能直接实例化活动。您不能直接在它们之间传递组件。你甚至不需要 Activity 。
删除活动扩展:
public class BottomNavigationBuilder {
//...
}
然后只需将您的context
参考用于任何需要上下文(new Intent(context, PizzaActivity.class)
,context.startActivity()
等)的东西。
bottomNavigation
在将其传递给构建器后,您也不需要重新分配。bottomNavigation
这是同一个实例,因此对 Builder 内部所做的任何更改也会对bottomNavigation
外部进行。
推荐阅读
- android - 使用 apktool 更改 IAP SKU
- python - 如何在用 C 编写的程序中调用 python 脚本?
- python-3.x - 在 Pycharm 中定义一致(合法)的导入样式
- .net - Automapper:使用接口与 ITypeConverter
- javascript - 如何计算数组中元素的频率
- amazon-web-services - AWS S3 putObjectTagging 在 node.js lambda 函数中因 AccessDenied 而失败
- excel - Excel VBA更改值
- gnuplot - 点上面向 gnuplot 的箭头
- php - PHP:嵌套的foreach循环和传递值
- algorithm - 获得颜色联合的可能性以生成另一种颜色