I am new in android. I am making quiz app and I want to use navigation view to Show current score of player by changing color of textview of points. I am using navigation drawer activity and home fragment for running the quiz.
this is code of home fragment
binding = FragmentHomeBinding.inflate(inflater, container, false);
View root = binding.getRoot();
NavigationView navigationView = (NavigationView) root.findViewById(R.id.nav_header);
View headerView = navigationView.getHeaderView(0);
TextView tv1=(TextView)headerView.findViewById(R.id.textView1);
TextView tv2=(TextView)headerView.findViewById(R.id.textView2);
TextView tv3=(TextView)headerView.findViewById(R.id.textView3);
TextView tv4=(TextView)headerView.findViewById(R.id.textView4);
TextView tv5=(TextView)headerView.findViewById(R.id.textView5);
TextView tv6=(TextView)headerView.findViewById(R.id.textView6);
TextView tv7=(TextView)headerView.findViewById(R.id.textView7);
TextView tv8=(TextView)headerView.findViewById(R.id.textView8);
TextView tv9=(TextView)headerView.findViewById(R.id.textView9);
TextView tv10=(TextView)headerView.findViewById(R.id.textView10);
TextView tv11=(TextView)headerView.findViewById(R.id.textView11);
TextView tv12=(TextView)headerView.findViewById(R.id.textView12);
TextView tv13=(TextView)headerView.findViewById(R.id.textView13);
TextView tv14=(TextView)headerView.findViewById(R.id.textView14);
TextView tv15=(TextView)headerView.findViewById(R.id.textView15);
TextView tv16=(TextView)headerView.findViewById(R.id.textView16);
switch (correctCountScore+1){
case 16: tv16.setBackgroundColor(Color.rgb(128,255,128));
case 15: tv15.setBackgroundColor(Color.rgb(128,255,128));
case 14: tv14.setBackgroundColor(Color.rgb(128,255,128));
case 13: tv13.setBackgroundColor(Color.rgb(128,255,128));
case 12: tv12.setBackgroundColor(Color.rgb(128,255,128));
case 11: tv11.setBackgroundColor(Color.rgb(128,255,128));
case 10: tv10.setBackgroundColor(Color.rgb(128,255,128));
case 9: tv9.setBackgroundColor(Color.rgb(128,255,128));
case 8: tv8.setBackgroundColor(Color.rgb(128,255,128));
case 7: tv7.setBackgroundColor(Color.rgb(128,255,128));
case 6: tv6.setBackgroundColor(Color.rgb(128,255,128));
case 5: tv5.setBackgroundColor(Color.rgb(128,255,128));
case 4: tv4.setBackgroundColor(Color.rgb(128,255,128));
case 3: tv3.setBackgroundColor(Color.rgb(128,255,128));
case 2: tv2.setBackgroundColor(Color.rgb(128,255,128));
default: tv1.setBackgroundColor(Color.rgb(128,255,128));
}
Below is nav header xml file :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
android:background="@color/black"
android:id="@+id/nav_header">
<ImageView
android:id="@+id/ivAudiencePoll"
android:layout_width="170dp"
android:layout_height="70dp"
android:layout_marginStart="0dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="50dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<ImageView
android:id="@+id/ivFiftyFifty"
android:layout_width="170dp"
android:layout_height="70dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="50dp"
android:layout_marginRight="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/ivAudiencePoll"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/ivChangeQuestion"
android:layout_width="170dp"
android:layout_height="70dp"
android:layout_marginStart="60dp"
android:layout_marginLeft="60dp"
app:layout_constraintEnd_toStartOf="@+id/ivAskTheExpert"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ivAudiencePoll" />
<ImageView
android:id="@+id/ivAskTheExpert"
android:layout_width="170dp"
android:layout_height="70dp"
android:layout_marginTop="120dp"
android:layout_marginEnd="0dp"
android:layout_marginRight="80dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="250dp"
android:gravity="center_horizontal"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/textView16"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text="16 Points" />
<TextView
android:id="@+id/textView15"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text="15 Points" />
<TextView
android:id="@+id/textView14"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text="14 Points" />
<TextView
android:id="@+id/textView13"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text="13 Points" />
<TextView
android:id="@+id/textView12"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text="12 Points" />
<TextView
android:id="@+id/textView11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text="11 Points" />
<TextView
android:id="@+id/textView10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text="10 Points" />
<TextView
android:id="@+id/textView9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text=" 9 " />
<TextView
android:id="@+id/textView8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text=" 8 Points" />
<TextView
android:id="@+id/textView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text=" 7 Points" />
<TextView
android:id="@+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text=" 6 Points" />
<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text=" 5 Points" />
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text=" 4 Points" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text=" 3 Points" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text=" 2 Points" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text=" 1 Points" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
But I am getting these error
2022-01-08 15:20:55.033 687-687/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.viky.homequiztest, PID: 687
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.viky.homequiztest/com.viky.homequiztest.MainActivity}: android.view.InflateException: Binary XML file line #15: Binary XML file line #19: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2781)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2859)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1592)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6518)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: android.view.InflateException: Binary XML file line #15: Binary XML file line #19: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #19: Error inflating class fragment
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View com.google.android.material.navigation.NavigationView.getHeaderView(int)' on a null object reference
at com.viky.homequiztest.ui.home.HomeFragment.onCreateView(HomeFragment.java:72)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:112)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1647)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3128)
at androidx.fragment.app.FragmentManager.dispatchViewCreated(FragmentManager.java:3065)
at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:2988)
at androidx.fragment.app.FragmentStateManager.ensureInflatedView(FragmentStateManager.java:392)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:281)
at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:140)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:319)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:298)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:995)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:859)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:995)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:859)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at com.viky.homequiztest.databinding.ActivityMainBinding.inflate(ActivityMainBinding.java:53)
at com.viky.homequiztest.databinding.ActivityMainBinding.inflate(ActivityMainBinding.java:47)
at com.viky.homequiztest.MainActivity.onCreate(MainActivity.java:28)
at android.app.Activity.performCreate(Activity.java:7023)
at android.app.Activity.performCreate(Activity.java:7014)
2022-01-08 15:20:55.036 687-687/? E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2734)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2859)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1592)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6518)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)