【发布时间】:2020-10-14 21:05:33
【问题描述】:
我正在尝试构建这个应用程序,但 Android Studio 在文件上向我抛出了一个编译时错误,告诉我语句的结构不正确。当我尝试使用 public 修饰符声明方法时,第一个错误是“表达式的非法开始”错误。然后,无论对象(菜单、视图、工具栏等)如何,都找不到我使用的每个对象。
我可以在声明 AppBarConfiguration 后很好地声明对象,但我不能调用方法。
我曾尝试在调用 onCreate 之前添加私有对象并使用它们,但波浪形的红线只是在将参数添加到方法上的修饰符之前移动。
这一切都发生在我活动的 onCreate 方法中。我机器上的其他应用程序发现在 onCreate 中声明的值和方法很好并且可以正常构建。 这是代码;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.View;
import android.view.Menu;
import android.widget.Button;
import com.google.android.material.navigation.NavigationView;
import androidx.appcompat.widget.PopupMenu;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button sdstorage = (Button) findViewById(R.id.goSDCardBtn);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
//this is where the errors begin.
public void onPop3 (View view){ //illegal start of expression on the blank space right before the View is passed to the method and the view object cannot be found
PopupMenu popThree = new PopupMenu(this, view);
MenuInflater inflater = popThree.getMenuInflater();
inflater.inflate(R.menu.topmenu, popThree.getMenu());
popThree.show();
}
public void goSdCard (View view){illegal start of expression on the blank space right before the View is passed to the method and the view object cannot be found
startActivity(new Intent(MainActivity.this, DirectoryView.class));
}
public void goSettings (View view){ //here too
Navigation.findNavController(view).navigate(R.id.settingsview);
Intent intent = new Intent(this, settings.class);
startActivity(new Intent(MainActivity.this, settings.class));
}
@Override // the Override annotation is throwing an error
public boolean onCreateOptionsMenu (Menu menu){ // a semi-colon is expected before the Menu object is declared
getMenuInflater().inflate(R.menu.main, menu); //
return true;
}
@Override
public boolean onSupportNavigateUp () { // a semi-colon is expected here before and after the parenthesis
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();// everything after the return statement is throwing an error
}
} // this curly brace should signal the end of the onCreate method
}
【问题讨论】:
-
onCreate没有右括号? -
在代码的最后
-
肯定会关闭
MainActivity类?在任何情况下,Java 的方法中都不能有方法。 -
否,因为我可以在 onCreate 方法中声明一些方法 (findViewById),但我无法添加其他方法。这些方法找不到相应的对象(视图和菜单),除非我在 onCreate 之前声明它们(我创建了通用私有视图和菜单对象),然后我在方法的修饰符上得到错误。
标签: java android android-studio