Click here to Skip to main content
14,739,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Currently i am trying to create a news app by following the link here Create News App in Android Studio || Design Layout And Retrieve Data From News Api - Part 1 - YouTube[^] , at the same time i have also created a bottom navigation to try and make my app different, however though when i have runned it this error came out
java.lang.RuntimeException: Unable to start activity ComponentInfo{sg.edu.rp.c346.a4thpdappv2/sg.edu.rp.c346.a4thpdappv2.MainActivity}: java.lang.IllegalArgumentException: ID does not reference a View inside this Activity
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
       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.java:68)
       at android.app.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.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
    Caused by: java.lang.IllegalArgumentException: ID does not reference a View inside this Activity
       at android.app.Activity.requireViewById(Activity.java:2678)
       at androidx.core.app.ActivityCompat.requireViewById(ActivityCompat.java:363)
       at androidx.navigation.Navigation.findNavController(Navigation.java:58)
       at sg.edu.rp.c346.a4thpdappv2.MainActivity.onCreate(MainActivity.java:101)
       at android.app.Activity.performCreate(Activity.java:7136)
       at android.app.Activity.performCreate(Activity.java:7127)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
       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.java:68) 
       at android.app.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.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 


What I have tried:

so what i have tried is to of course try and figure out where is the error within the MainActivity file and it pointed me towards here
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
BottomNavigationView bottomNav = findViewById(R.id.bottom_nav);
bottomNav.setOnNavigationItemSelectedListener(navListener);
NavigationUI.setupWithNavController(bottomNav, navController);

getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment, new HomeFragment());

yet however though i have viewed my activity_main file and i already have the id there as seen here
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:orientation="vertical"
    tools:context=".MainActivity">



    <fragment
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:id="@+id/nav_host_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph"
        />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        app:menu="@menu/home"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        app:itemTextColor="@color/whiteColor"
        app:itemIconTint="@color/whiteColor"
        android:background="@color/colorPrimary"/>
</RelativeLayout>

thus as such i am confused as to why does the error there states that the Id does not reference a view in the activity as i clearly have it already in the first place and i do not know what else im missing. Can anyone kindly help me out please?
Posted
Updated 5-May-20 7:37am
Comments
David Crow 6-Apr-20 10:14am
   
What is at line 101 of MainActivity.java?
MervinWee 7-Apr-20 2:37am
   
Hi, its this line here:

NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900