我试图设置一个显示3个片段的BottomNavigation。
问题是app在Android 5.0上完全有效,但在Android 6.0及更高版本上抛出ERROR ......
- compileSdkVersion 26 - minSdkVersion 21 - targetSdkVersion 26 -
为了确保我一直在使用example来构建这个应用程序,并且在我的代码中实现此方法之后仍然会抛出异常。
提前感谢您的帮助。
错误:
AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.samsung.auto_temperatura, PID: 8129
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.samsung.auto_temperatura/com.example.samsung.auto_temperatura.MainActivity}: 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
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
MainActivity
public class MainActivity extends AppCompatActivity {
public static final String FRAGMENT_USTAWIENIA = "FRAGMENT_USTAWIENIA";
public static final String FRAGMENT_CZUJNIKI = "FRAGMENT_CZUJNIKI";
public static final String FRAGMENT_ROZLACZ = "FRAGMENT_ROZLACZ";
private static final String TAG = MainActivity.class.getSimpleName();
// I think here goes something wrong...
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_ustawienia:
replaceFragment(UstawieniaFragment.newInstance(),FRAGMENT_USTAWIENIA);
return true;
case R.id.action_czujniki:
replaceFragment(CzujnikiFragment.newInstance(),FRAGMENT_CZUJNIKI);
return true;
case R.id.action_rozlacz:
replaceFragment(RozlaczFragment.newInstance(),FRAGMENT_ROZLACZ);
return true;
}
return false;
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navigation = findViewById(R.id.bottom_navigation);
navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_ustawienia:
replaceFragment(UstawieniaFragment.newInstance(),FRAGMENT_USTAWIENIA);
return true;
case R.id.action_czujniki:
replaceFragment(CzujnikiFragment.newInstance(),FRAGMENT_CZUJNIKI);
return true;
case R.id.action_rozlacz:
replaceFragment(RozlaczFragment.newInstance(),FRAGMENT_ROZLACZ);
return true;
}
return false;
}
});
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragment_container, CzujnikiFragment.newInstance(), FRAGMENT_CZUJNIKI)
.commit();
}
private void replaceFragment(Fragment newFragment, String tag) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container, newFragment, tag)
.commit();
}
}
MainActivity XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.example.samsung.auto_temperatura.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentInsetLeft="0dp"
android:elevation="4dp"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:titleTextColor="@color/bialy"
app:contentInsetRight="0dp">
<ImageView
android:layout_width="100dp"
android:layout_height="40dp"
android:src="@drawable/imkapknapis"
android:layout_marginRight="12dp"/>
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="4dp"
android:text="STAN CZUJNIKÓW:"
android:visibility="visible"
android:textStyle="bold"/>
<TextView
android:id="@+id/stan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="DOBRY"
android:textStyle="bold"/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottom_navigation" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_gravity="bottom"
app:itemBackground="@color/bialy"
app:itemIconTint="@color/imkNiebieski"
app:itemTextColor="@color/czarny"
app:menu="@menu/nav_items"
/>
片段示例XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerviewID"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_marginTop="24dp"
android:clickable="true"
android:clipChildren="false"
android:clipToPadding="false" />
答案 0 :(得分:0)
BottomNavigationView navigation = findViewById(R.id.bottom_navigation);
findViewById在代码中返回null。你能附上活动和片段的布局XML文件吗?确保您的视图的布局ID为R.id.bottom_navigation。