Android工作室;底部导航视图 = 空
Android Studio; BottomNavigationView = null
我不知道为什么 BottomNavigationView
为空。我正在使用 Firebase API 并添加了一些代码以将数据添加到 JSON 文件。现在,在我这样做之后,我的一项活动就停止了。它甚至不是我正在研究的 Activity。我的导航等于 null,我检查了 XML 文件。名称相同,调试也无济于事,因为它向我展示了设置 BottomNavigationView
并为我提供 NullPointer 的一行代码。
这是造成此问题的导航:
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener()
{
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_to_dos:
return true;
case R.id.navigation_categories:
intent = new Intent(ToDos.this, Categories.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
startActivity(intent);
return true;
case R.id.navigation_account:
intent = new Intent(ToDos.this, Account.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
startActivity(intent);
return true;
}
return false;
}
};
这是我在 onCreate() 函数中初始化与导航有关的所有内容的部分:
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
navigation.setSelectedItemId(R.id.navigation_to_dos);
我收到这个错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: lb335.bbbaden.ch.pendenzenverwaltung, PID: 25288
java.lang.RuntimeException: Unable to start activity ComponentInfo{lb335.bbbaden.ch.pendenzenverwaltung/lb335.bbbaden.ch.pendenzenverwaltung.ToDos}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.BottomNavigationView.setOnNavigationItemSelectedListener(android.support.design.widget.BottomNavigationView$OnNavigationItemSelectedListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3279)
at android.app.ActivityThread.-wrap12(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1881)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7406)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:926)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.BottomNavigationView.setOnNavigationItemSelectedListener(android.support.design.widget.BottomNavigationView$OnNavigationItemSelectedListener)' on a null object reference
at lb335.bbbaden.ch.pendenzenverwaltung.ToDos.onCreate(ToDos.java:76)
at android.app.Activity.performCreate(Activity.java:7365)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3137)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3279)
at android.app.ActivityThread.-wrap12(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1881)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7406)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:926)
我的ActivityXML:
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />
我对此一头雾水,非常感谢更有经验的编码人员提供一些帮助。
编辑:
感谢 snachmsm,我发现我的函数顺序全乱了。我在实际设置内容之前执行了findViewById()
。我不得不将其关闭,现在可以使用了。
您的导航 returning false
因为您将其设置为 false
请在导航结束时检查并将 return 类型从 return false;
更改为 return true;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener()
{
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_to_dos:
return true;
case R.id.navigation_categories:
intent = new Intent(ToDos.this, Categories.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
startActivity(intent);
return true;
case R.id.navigation_account:
intent = new Intent(ToDos.this, Account.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
startActivity(intent);
return true;
}
return false; // Here is the problem. Change it from false to true
}};
此外,BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.BottomNavigationView.setOnNavigationItemSelectedListener(android.support.design.widget.BottomNavigationView$OnNavigationItemSelectedListener)' on a null object reference
你的BottomNavigationView
是null
,这是事实。此行未获取您的 View
:
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
您确定您的 BottomNavigationView
属于膨胀布局吗? R.id.navigation
是否为 BottomNavigationView
设置正确?显示 XML 和 onCreate
方法中的更多代码。也许顺序不正确? (例如 findViewById
在 setContentView
之前调用)
编辑:好的,id
在 XML 中为 View
设置正确,但你只发布了它的一小部分,还显示了你如何膨胀这个 XML里面Activity
在我的案例中我也遇到了这个问题我通过在
中替换正确的家 activity 解决了这个问题
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seller_home);//here was problem,i was inflating wrong home activity
我遇到了同样的问题。我通过添加 setContentView 方法解决了它。
setContentView(R.layout.activity_manage);
我不知道为什么 BottomNavigationView
为空。我正在使用 Firebase API 并添加了一些代码以将数据添加到 JSON 文件。现在,在我这样做之后,我的一项活动就停止了。它甚至不是我正在研究的 Activity。我的导航等于 null,我检查了 XML 文件。名称相同,调试也无济于事,因为它向我展示了设置 BottomNavigationView
并为我提供 NullPointer 的一行代码。
这是造成此问题的导航:
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener()
{
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_to_dos:
return true;
case R.id.navigation_categories:
intent = new Intent(ToDos.this, Categories.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
startActivity(intent);
return true;
case R.id.navigation_account:
intent = new Intent(ToDos.this, Account.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
startActivity(intent);
return true;
}
return false;
}
};
这是我在 onCreate() 函数中初始化与导航有关的所有内容的部分:
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
navigation.setSelectedItemId(R.id.navigation_to_dos);
我收到这个错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: lb335.bbbaden.ch.pendenzenverwaltung, PID: 25288
java.lang.RuntimeException: Unable to start activity ComponentInfo{lb335.bbbaden.ch.pendenzenverwaltung/lb335.bbbaden.ch.pendenzenverwaltung.ToDos}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.BottomNavigationView.setOnNavigationItemSelectedListener(android.support.design.widget.BottomNavigationView$OnNavigationItemSelectedListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3279)
at android.app.ActivityThread.-wrap12(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1881)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7406)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:926)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.BottomNavigationView.setOnNavigationItemSelectedListener(android.support.design.widget.BottomNavigationView$OnNavigationItemSelectedListener)' on a null object reference
at lb335.bbbaden.ch.pendenzenverwaltung.ToDos.onCreate(ToDos.java:76)
at android.app.Activity.performCreate(Activity.java:7365)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3137)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3279)
at android.app.ActivityThread.-wrap12(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1881)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7406)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:926)
我的ActivityXML:
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />
我对此一头雾水,非常感谢更有经验的编码人员提供一些帮助。
编辑:
感谢 snachmsm,我发现我的函数顺序全乱了。我在实际设置内容之前执行了findViewById()
。我不得不将其关闭,现在可以使用了。
您的导航 returning false
因为您将其设置为 false
请在导航结束时检查并将 return 类型从 return false;
更改为 return true;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener()
{
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_to_dos:
return true;
case R.id.navigation_categories:
intent = new Intent(ToDos.this, Categories.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
startActivity(intent);
return true;
case R.id.navigation_account:
intent = new Intent(ToDos.this, Account.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
startActivity(intent);
return true;
}
return false; // Here is the problem. Change it from false to true
}};
此外,BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.BottomNavigationView.setOnNavigationItemSelectedListener(android.support.design.widget.BottomNavigationView$OnNavigationItemSelectedListener)' on a null object reference
你的BottomNavigationView
是null
,这是事实。此行未获取您的 View
:
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
您确定您的 BottomNavigationView
属于膨胀布局吗? R.id.navigation
是否为 BottomNavigationView
设置正确?显示 XML 和 onCreate
方法中的更多代码。也许顺序不正确? (例如 findViewById
在 setContentView
之前调用)
编辑:好的,id
在 XML 中为 View
设置正确,但你只发布了它的一小部分,还显示了你如何膨胀这个 XML里面Activity
在我的案例中我也遇到了这个问题我通过在
中替换正确的家 activity 解决了这个问题 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seller_home);//here was problem,i was inflating wrong home activity
我遇到了同样的问题。我通过添加 setContentView 方法解决了它。
setContentView(R.layout.activity_manage);