简体   繁体   English

我在Android工作室错误地打电话了吗?

[英]did i call wrongly in android studio?

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 很抱歉提出这么多愚蠢的问题并感谢您的回答

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);

}
}

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. 你在投射视图之前错过了一个“(”,但更重要的是你不能理解应该在类的onCreate()方法中调用引用。在类之外放置东西会损害正确的形式和阻止编译器识别和/或合成您编写的代码,例如您提供的示例。作为替代方法,您可以创建一个类成员变量,然后使用this运算符访问它,但最重要的是确保代码是在类范围内。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM