i get a } expected in the code i dont know where i forgot it though.
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);
}
}
I tired adding a } at line 53 but it gives me the following error ‘class’ or ‘interface’ expected
I cant figure out where i forgot to close it. Sorry if it is really obivous. Thank you in advance.
>Solution :
You try to declare an Intent outside of a method in your MainActivity class. I guess you can move the Intent declaration inside a method.
Like this:
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;
}