0

I am trying to build this app, but Android Studio is throwing me a compile-time error on the file, telling me that statements aren't correctly structured. The first error is an "illegal start of expression" error when I'm trying to declare a method with a public modifier. Then, each object that I use cannot be found, no matter the object (menu, view, toolbar etc).

I can declare objects just fine after I declare my AppBarConfiguration, but I cannot call methods.

I have tried adding private objects before onCreate is called and using those, but the squiggly red line just moves from before the parameters are added to the modifier on the methods.

This is all happening within the onCreate method of my activity. Other apps on my machine find the values and methods declared within onCreate to be just fine and build normally. Here is the code;


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
}
  • 1
    No closing brace for `onCreate`? – Den-Jason Oct 14 '20 at 21:11
  • at the very end of the code – Code_Student09 Oct 14 '20 at 21:16
  • Does this answer your question? [Does Java support inner / local / sub methods?](https://stackoverflow.com/questions/5388584/does-java-support-inner-local-sub-methods) – Den-Jason Oct 14 '20 at 21:18
  • Surely that closes the `MainActivity ` class? In any case you cannot have methods within methods in Java. – Den-Jason Oct 14 '20 at 21:20
  • no because I am able to declare some methods (findViewById) within the onCreate method, but I am unable to add others. The methods are not finding the corresponding objects (View and Menu) unless I declare them before onCreate (I created generic private View and Menu objects), and then I get errors on the modifiers for the methods. – Code_Student09 Oct 14 '20 at 21:25
  • The `findViewById` items are function calls, they are not methods. – Den-Jason Oct 14 '20 at 21:28
  • Last time I trust Google. Well, Builder works just fine with the AppBarConfiguration object. – Code_Student09 Oct 14 '20 at 21:33
  • It might be worth you consulting a book such as this https://www.amazon.co.uk/dp/178829629X – Den-Jason Oct 14 '20 at 21:51
  • do you know how to create a collapsible 'hidden' message in markdown? The logcat is pretty lengthy here, and I want to post the entire contents of my file including the commented out portions. – Code_Student09 Oct 14 '20 at 22:05

1 Answers1

0

Try this and let us know if it works:


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

} // this curly brace should signal the end of the onCreate method

I've closed the method and removed the triple backtick which may not be in your actual source code but appears above.

Rob Evans
  • 2,822
  • 1
  • 9
  • 15
  • nothing has changed. sorry about the triple backtick, but this code produces the same compil-time errors as before, which is particularly baffling because other apps compile just fine and the methods I use can find their objects just fine. – Code_Student09 Oct 14 '20 at 21:30
  • I've closed a bracket that was potentially missing ... and suggested above by others as missing. If that didn't work can you share the stacktrace|error you're getting? – Rob Evans Oct 14 '20 at 21:51
  • do you know how to create a collapsible 'hidden' message in markdown? The logcat is pretty lengthy here, and I want to post the entire contents of my file including the commented out portions. – Code_Student09 Oct 14 '20 at 22:05