1

i am trying to use this answer but i don't get why am i getting this error:

error: class, interface, or enum expected

the code:

package com.example.moviereview3;

import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class HomeActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
}
}
BottomNavigationView bottomNavigationView = BottomNavigationView)findViewById(R.id.bottom_navigation);
BottomNavigationViewHelper.removeShiftMode(bottomNavigationView);

sorry for asking so many dumb questions and thanks for answering

Dima Kozhevin
  • 3,602
  • 9
  • 39
  • 52
wolnavi
  • 183
  • 1
  • 14
  • 1
    Last two lines need to be part of some function you are going to call. They can't below HomeActivity. I would suggest you learn a little bit more about Java before learning Android. – TheKarlo95 Jul 06 '18 at 21:15
  • The '}' before the first BottomNavigattionView is the end of the class. It should come after the last statement of the file. Indent the code properly, and it would be easy to see such errors. – Susmit Agrawal Jul 06 '18 at 21:15

2 Answers2

2

Do like this:-

public class HomeActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    BottomNavigationView bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottom_navigation);
    BottomNavigationViewHelper.removeShiftMode(bottomNavigationView);

}
}
Raj
  • 2,997
  • 2
  • 12
  • 30
1

You are missing a "(" right before casting the view, but even more importantly you're lacking to understand that the reference should be called inside the onCreate() method of the class. Placing stuff outside of the class would compromise proper form and prevent the compiler from recognizing and/or synthesizing the code you wrote, as such, the example you provided. As an alternative, you could create a class member variable and then access it with the this operator, but most importantly make sure the code is inside the class scope.

private ...View bnv; ... this.bnv = instantiate

This would be your modified code, it's up to you if you want member variables vs local variables.

public class HomeActivity extends AppCompatActivity {
 @Override protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_home);
  BottomNavigationView bottomNavigationView = 
  (BottomNavigationView)findViewById(R.id.bottom_navigation);
  BottomNavigationViewHelper.removeShiftMode(bottomNavigationView);
  } //END_ON_CREATE_METHOD
 }/END_HOMEACTIVITY_CLASS
EvOlaNdLuPiZ
  • 600
  • 1
  • 4
  • 21