在活动范围

时间:2020-10-14 21:05:33

标签: java android android-studio

我正在尝试构建此应用,但是Android Studio在文件上抛出了编译时错误,告诉我语句的结构不正确。当我尝试使用公共修饰符声明方法时,第一个错误是“表达式的非法开始”错误。然后,无论对象(菜单,视图,工具栏等)如何,都找不到我使用的每个对象。

在声明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
}

1 个答案:

答案 0 :(得分:0)

尝试一下,让我们知道它是否有效:


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

我已经关闭了该方法,并删除了三个反引号,该反引号可能不在您的实际源代码中,而是显示在上方。