There is a code:
MainActivity.java:
package asus.example.com.fitnessapp; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.widget.TextView; public class MainActivity extends AppCompatActivity { final TextView textView = findViewById(R.id.textView); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu){ MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.nav_items,menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item){ switch (item.getItemId()){ case R.id.home: textView.setText("Home"); return true; case R.id.notification: textView.setText("Notification"); return true; case R.id.profile: textView.setText("Profile"); return true; default: return super.onOptionsItemSelected(item); } } } activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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=".MainActivity"> <android.support.design.widget.BottomNavigationView android:layout_width="match_parent" android:layout_height="52dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:menu="@menu/nav_items"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView"/> </android.support.constraint.ConstraintLayout> nav_items.xml:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/home" android:title="@string/home" android:icon="@drawable/home" /> <item android:id="@+id/notification" android:title="@string/notifications" android:icon="@drawable/notification" /> <item android:id="@+id/profile" android:icon="@drawable/person" android:title="@string/profile" /> </menu> UPDATE
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="asus.example.com.fitnessapp"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> When executed, it gives an error:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo {asus.example.com.fitnessapp / asus.example.com.fitnessapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method android.view.Window $ Callback android.view.Window.getCallback () 'on a null object reference at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2843) at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3048) at android.app. servertransaction.LaunchActivityItem.execute (LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks (TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.r.apprn.servicetransaction.TransactionExecutor.execute (TransactionExecutor.ologa.appaserser.java:108) .ActivityThread $ H.handleMessage (ActivityThread.java:1808) at android.os.Handler.dispatchMessage (Handler.java:106) at android.os.Looper.loop (Looper.java:193) at android.app.ActivityThread. main (ActivityThread.java:6669) at java.lang.reflect.Method.invoke (Native Method) at com.android. .NullPointerException: Attempt to invoke virtual method "android.view.Window $ Callback android.view.Window.getCallback ()" android.support. AppCompatActivity.java:191) at asus.example.com.fitnessapp.MainActivity. (MainActivity.java:11) at java.lang. ) at android.support.v4.app.CoreComponentFactory.instantiateActivity (CoreComponentFactory.java:43) at android.app.Instrumentation.newActivity (I nstrumentation.java:1215) at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2831)
What is the problem and how to solve?