我在代码中得到了一个 } ,但我不知道我在哪里忘记了它。
package com.example.myapplication;
import...
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Intent settingsIntent = new Intent(this, SettingsActivity.class);
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public void openSettings() {
// Open settings
startActivity(settingsIntent);
}
@Override
public void onPointerCaptureChanged(boolean hasCapture) {
super.onPointerCaptureChanged(hasCapture);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
if (item.getItemId() == R.id.action_settings) {
openSettings();
//noinspection SimplifiableIfStatement
}
return super.onOptionsItemSelected(item);
}
}
我厌倦了在第 53 行添加 } 但它给了我以下错误“class”或“interface”预期 我不知道我在哪里忘记关闭它。抱歉,如果它真的很明显。预先感谢您。
您尝试在 MainActivity 类中的方法之外声明 Intent。我想你可以将 Intent 声明移到方法内。 像这样:
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private Intent settingsIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsIntent = new Intent(this, SettingsActivity.class);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}