FAB 按钮在与底部导航视图一起使用时出错
FAB Button Gives Error When Use With Bottom Navigation View
我有一个底部导航视图,在底部导航视图的中心有一个浮动操作按钮。单击底部导航视图项时我启动片段,单击浮动操作按钮时我启动 activity。片段加载正确,但当我点击 fab 按钮时,有时它 运行 正确但有时它会出错。
我认为,它会出错,因为我尝试在 activity 中加载片段,然后在 activity 中加载片段时尝试打开另一个 activity。但是我无法修复它。
菜单:
https://i.stack.imgur.com/V5auA.jpg
主要Activity的相关部分:
public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, LocationListener {
private Context context;
private SharedPreference sharedPreference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = MainActivity.this;
sharedPreference = SharedPreference.getInstance(context);
if (getIntent().hasExtra(Constants.SCREEN_TAG)) {
String type = getIntent().getStringExtra(Constants.SCREEN_TAG);
}
FloatingActionButton fabMainActivity = findViewById(R.id.fabMainActivity);
BottomNavigationView bnvMainActivity = findViewById(R.id.bnvMainActivity);
bnvMainActivity.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener);
loadFragment(new DiscoverFragment());
fabMainActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, PostJobActivity.class));
}
});
}
private BottomNavigationView.OnNavigationItemSelectedListener onNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment selectedFragment = null;
switch (menuItem.getItemId()){
case R.id.navigation_discover:
selectedFragment = new DiscoverFragment();
break;
case R.id.navigation_nearby:
selectedFragment = new NearbyFragment();
break;
case R.id.empty_menu:
break;
case R.id.navigation_notification:
selectedFragment = new NotificationFragment();
break;
case R.id.navigation_profile:
selectedFragment = new ProfileFragment();
break;
}
loadFragment(selectedFragment);
return true;
}
};
private void loadFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.flMainActivity, fragment);
fragmentTransaction.commit();
}
}
主要ActivityXML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activity.MainActivity"
android:background="@color/app_background">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/rlTopBackgroundMainActivity"
android:layout_width="match_parent"
android:layout_height="75dp"
android:background="@drawable/top_background"
android:layout_gravity="top">
<ImageView
android:id="@+id/ivLogoWithTextMainActivity"
android:layout_width="142dp"
android:layout_height="41dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="23dp"
android:layout_marginEnd="247dp"
android:layout_marginRight="247dp"
android:layout_marginBottom="17dp"
android:layout_centerVertical="true"
android:contentDescription="@string/terms_activity_logo_with_text"
android:src="@drawable/logo_with_text" />
<ImageView
android:id="@+id/ivMessagesMainActivity"
android:layout_width="33dp"
android:layout_height="41dp"
android:src="@drawable/message_icon"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:layout_marginEnd="20dp"
android:contentDescription="@string/messages_icon" />
</RelativeLayout>
<FrameLayout
android:id="@+id/flMainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/rlTopBackgroundMainActivity" />
</RelativeLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bnvMainActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:layout_gravity="bottom"
app:itemIconTint="@color/bottom_menu_color"
app:menu="@menu/bottom_navigation_menu"
app:labelVisibilityMode="unlabeled" />
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bapMainActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
app:itemIconTint="@color/bottom_menu_color"
app:labelVisibilityMode="unlabeled"
app:fabAlignmentMode="center"
app:fabCradleMargin="7dp"
app:elevation="0dp"
android:layout_gravity="bottom"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fabMainActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/purple_2"
app:tint="@android:color/white"
android:src="@drawable/post_job_icon"
app:layout_anchor="@id/bapMainActivity"
app:maxImageSize="35dp" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Post 作业 Activity 的相关部分:(Post 作业 Activity 也在框架布局中。)
public class PostJobActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_job);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
context = PostJobActivity.this;
btnBackToMainActivityPostJobActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(PostJobActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
});
}
@Override
public void onBackPressed() {
Intent intent = new Intent(PostJobActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
}
错误信息:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
at androidx.fragment.app.FragmentTransaction.doAddOp(FragmentTransaction.java:161)
at androidx.fragment.app.BackStackRecord.doAddOp(BackStackRecord.java:179)
at androidx.fragment.app.FragmentTransaction.replace(FragmentTransaction.java:225)
at androidx.fragment.app.FragmentTransaction.replace(FragmentTransaction.java:200)
at dk.lejekspert.lejekspertrekrutter.Activity.MainActivity.loadFragment(MainActivity.java:133)
at dk.lejekspert.lejekspertrekrutter.Activity.MainActivity.access[=13=]0(MainActivity.java:58)
at dk.lejekspert.lejekspertrekrutter.Activity.MainActivity.onNavigationItemSelected(MainActivity.java:126)
at com.google.android.material.bottomnavigation.BottomNavigationView.onMenuItemSelected(BottomNavigationView.java:243)
at androidx.appcompat.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:840)
at androidx.appcompat.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:991)
at com.google.android.material.bottomnavigation.BottomNavigationMenuView.onClick(BottomNavigationMenuView.java:124)
at android.view.View.performClick(View.java:5723)
at android.view.View$PerformClick.run(View.java:22689)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6364)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
有解决这个问题的办法吗?
谢谢。
编辑:我在我的 bottom_navigation_menu.xml 文件中添加了一句话,以禁止启用 fab 按钮下方的菜单项。
<item
android:id="@+id/empty_menu"
app:showAsAction="always"
android:enabled="false"
android:title="@string/nearby" />
此外,此代码部分不再是必需的。
case R.id.empty_menu:
break;
当您使用 fragment
= null
调用 fragmentTransaction.replace(R.id.flMainActivity, fragment);
时会出现此问题。
检查onNavigationItemSelected
。
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment selectedFragment = null;
switch (menuItem.getItemId()){
//...
case R.id.empty_menu:
break; // <-- this case
}
loadFragment(selectedFragment);
return true;
在这种情况下 selectedFragment
是 null
。
检查问题中的屏幕,您似乎在 BottomNavigationView
中使用了一个没有图标的菜单项,正好在 FAB 后面。
我有一个底部导航视图,在底部导航视图的中心有一个浮动操作按钮。单击底部导航视图项时我启动片段,单击浮动操作按钮时我启动 activity。片段加载正确,但当我点击 fab 按钮时,有时它 运行 正确但有时它会出错。
我认为,它会出错,因为我尝试在 activity 中加载片段,然后在 activity 中加载片段时尝试打开另一个 activity。但是我无法修复它。
菜单:
https://i.stack.imgur.com/V5auA.jpg
主要Activity的相关部分:
public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, LocationListener {
private Context context;
private SharedPreference sharedPreference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = MainActivity.this;
sharedPreference = SharedPreference.getInstance(context);
if (getIntent().hasExtra(Constants.SCREEN_TAG)) {
String type = getIntent().getStringExtra(Constants.SCREEN_TAG);
}
FloatingActionButton fabMainActivity = findViewById(R.id.fabMainActivity);
BottomNavigationView bnvMainActivity = findViewById(R.id.bnvMainActivity);
bnvMainActivity.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener);
loadFragment(new DiscoverFragment());
fabMainActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, PostJobActivity.class));
}
});
}
private BottomNavigationView.OnNavigationItemSelectedListener onNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment selectedFragment = null;
switch (menuItem.getItemId()){
case R.id.navigation_discover:
selectedFragment = new DiscoverFragment();
break;
case R.id.navigation_nearby:
selectedFragment = new NearbyFragment();
break;
case R.id.empty_menu:
break;
case R.id.navigation_notification:
selectedFragment = new NotificationFragment();
break;
case R.id.navigation_profile:
selectedFragment = new ProfileFragment();
break;
}
loadFragment(selectedFragment);
return true;
}
};
private void loadFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.flMainActivity, fragment);
fragmentTransaction.commit();
}
}
主要ActivityXML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activity.MainActivity"
android:background="@color/app_background">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/rlTopBackgroundMainActivity"
android:layout_width="match_parent"
android:layout_height="75dp"
android:background="@drawable/top_background"
android:layout_gravity="top">
<ImageView
android:id="@+id/ivLogoWithTextMainActivity"
android:layout_width="142dp"
android:layout_height="41dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="23dp"
android:layout_marginEnd="247dp"
android:layout_marginRight="247dp"
android:layout_marginBottom="17dp"
android:layout_centerVertical="true"
android:contentDescription="@string/terms_activity_logo_with_text"
android:src="@drawable/logo_with_text" />
<ImageView
android:id="@+id/ivMessagesMainActivity"
android:layout_width="33dp"
android:layout_height="41dp"
android:src="@drawable/message_icon"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:layout_marginEnd="20dp"
android:contentDescription="@string/messages_icon" />
</RelativeLayout>
<FrameLayout
android:id="@+id/flMainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/rlTopBackgroundMainActivity" />
</RelativeLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bnvMainActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:layout_gravity="bottom"
app:itemIconTint="@color/bottom_menu_color"
app:menu="@menu/bottom_navigation_menu"
app:labelVisibilityMode="unlabeled" />
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bapMainActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
app:itemIconTint="@color/bottom_menu_color"
app:labelVisibilityMode="unlabeled"
app:fabAlignmentMode="center"
app:fabCradleMargin="7dp"
app:elevation="0dp"
android:layout_gravity="bottom"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fabMainActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/purple_2"
app:tint="@android:color/white"
android:src="@drawable/post_job_icon"
app:layout_anchor="@id/bapMainActivity"
app:maxImageSize="35dp" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Post 作业 Activity 的相关部分:(Post 作业 Activity 也在框架布局中。)
public class PostJobActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_job);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
context = PostJobActivity.this;
btnBackToMainActivityPostJobActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(PostJobActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
});
}
@Override
public void onBackPressed() {
Intent intent = new Intent(PostJobActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
}
错误信息:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
at androidx.fragment.app.FragmentTransaction.doAddOp(FragmentTransaction.java:161)
at androidx.fragment.app.BackStackRecord.doAddOp(BackStackRecord.java:179)
at androidx.fragment.app.FragmentTransaction.replace(FragmentTransaction.java:225)
at androidx.fragment.app.FragmentTransaction.replace(FragmentTransaction.java:200)
at dk.lejekspert.lejekspertrekrutter.Activity.MainActivity.loadFragment(MainActivity.java:133)
at dk.lejekspert.lejekspertrekrutter.Activity.MainActivity.access[=13=]0(MainActivity.java:58)
at dk.lejekspert.lejekspertrekrutter.Activity.MainActivity.onNavigationItemSelected(MainActivity.java:126)
at com.google.android.material.bottomnavigation.BottomNavigationView.onMenuItemSelected(BottomNavigationView.java:243)
at androidx.appcompat.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:840)
at androidx.appcompat.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:991)
at com.google.android.material.bottomnavigation.BottomNavigationMenuView.onClick(BottomNavigationMenuView.java:124)
at android.view.View.performClick(View.java:5723)
at android.view.View$PerformClick.run(View.java:22689)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6364)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
有解决这个问题的办法吗? 谢谢。
编辑:我在我的 bottom_navigation_menu.xml 文件中添加了一句话,以禁止启用 fab 按钮下方的菜单项。
<item
android:id="@+id/empty_menu"
app:showAsAction="always"
android:enabled="false"
android:title="@string/nearby" />
此外,此代码部分不再是必需的。
case R.id.empty_menu:
break;
当您使用 fragment
= null
调用 fragmentTransaction.replace(R.id.flMainActivity, fragment);
时会出现此问题。
检查onNavigationItemSelected
。
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment selectedFragment = null;
switch (menuItem.getItemId()){
//...
case R.id.empty_menu:
break; // <-- this case
}
loadFragment(selectedFragment);
return true;
在这种情况下 selectedFragment
是 null
。
检查问题中的屏幕,您似乎在 BottomNavigationView
中使用了一个没有图标的菜单项,正好在 FAB 后面。