gpt4 book ai didi

java - 空指针,底部导航的 Activity Intent 中的上下文

转载 作者:行者123 更新时间:2023-12-02 10:23:19 32 4
gpt4 key购买 nike

这个应用程序有两个 Activity ,我想使用 BottomNavigationView 在它们之间切换。在 switch 语句内声明 Intent 会引发空指针异常。 MainActivity.java 更改为 SpecialsActivity.java。第二个 Activity 是 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。您不能直接在它们之间传递组件。为此,您甚至不需要 Activity。

删除 Activity 扩展:

public class BottomNavigationBuilder {
//...
}

然后只需使用您的 context任何需要 Context 的内容的引用( new Intent(context, PizzaActivity.class)context.startActivity() 等)。

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

关于java - 空指针,底部导航的 Activity Intent 中的上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54164407/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com