0

After I add a Bottom Navigation Activity, and try to call it from another activity, on compiling, I get the error:

{project directory}\ui\dashboard\DashboardFragment.java:13: error: illegal character: '`'
import `in`.ac.somedomain.someappname.R;

And the code in DashboardFragment.java causing the error is the import line:

import `in`.ac.somedomain.someappname.R;

I only added a new Bottom Navigation Activity, and attempted to call it using:

Intent homeIntent = new Intent(getApplicationContext, HomeActivity.class);
startActivity(homeIntent);

On removing the "`" manually (in 3 activities: DashboardFragment, HomeFragment, NotificationsFragment), the following errors occur on calling the activity (logcat, the app crashes):

2020-05-24 16:29:02.711 14323-14323/in.ac.somedomain.someappname E/AndroidRuntime: FATAL EXCEPTION: main
    Process: in.ac.somedomain.someappname, PID: 14323
    java.lang.RuntimeException: Unable to start activity ComponentInfo{in.ac.somedomain.someappname/in.ac.somedomain.someappname.BaseActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2943)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3078)
        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:1816)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6854)
        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:860)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
        at androidx.navigation.ui.ActionBarOnDestinationChangedListener.setTitle(ActionBarOnDestinationChangedListener.java:48)
        at androidx.navigation.ui.AbstractAppBarOnDestinationChangedListener.onDestinationChanged(AbstractAppBarOnDestinationChangedListener.java:100)
        at androidx.navigation.NavController.addOnDestinationChangedListener(NavController.java:218)
        at androidx.navigation.ui.NavigationUI.setupActionBarWithNavController(NavigationUI.java:220)
        at in.ac.somedomain.someappname.BaseActivity.onCreate(BaseActivity.java:26)
        at android.app.Activity.performCreate(Activity.java:7151)
        at android.app.Activity.performCreate(Activity.java:7142)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2923)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3078) 
        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:1816) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6854) 
        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:860) 

The error when hovering over the "`in`" in the code says Raw string literals are not supported at language level '7', and I tried some of its suggestions (switched Source Compatibility to 1.8, which says the same thing with level 8), disabled something which didn't change anything, and now it just tells me to go to Module settings, but I doubt that the raw string thing is the problem.

What can I do to make my Bottom Navigation Activities work?

EDIT: Activity code (Bottom Navigation Activity, called Main2Activity):

package in.ac.somedomain.somename;

import android.os.Bundle;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
//the below line was causing the problem
//        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);
    }

}
Vedaant Arya
  • 475
  • 6
  • 18

1 Answers1

1

First, replace the import code:

import `in`.ac.somedomain.someappname.R;

on:

 import ac.somedomain.someappname.R;

And then the error indicates that you have a link to the action bar = null. This answer may help you: answer

If this does not help, please show your Java code and manifest.

It is also worth saying that the action bar and bottom bar are different things.

Razrab.ytka
  • 101
  • 4
  • 1
    I was actually using a theme with No Action Bar (extending it), and the code attempts to do... something with it by default (`NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);`), so removing that fixed the crashing. As for the import lines, removing \`in\` completely made `ac` an unknown symbol, but just removing the "`" from all affected activities works fine now – Vedaant Arya May 24 '20 at 12:05