【问题标题】:Android Studio throwing error when values declared in scope of Activity在 Activity 范围内声明值时 Android Studio 抛出错误
【发布时间】: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 没有右括号?
  • 在代码的最后
  • 这能回答你的问题吗? Does Java support inner / local / sub methods?
  • 肯定会关闭MainActivity 类?在任何情况下,Java 的方法中都不能有方法。
  • 否,因为我可以在 onCreate 方法中声明一些方法 (findViewById),但我无法添加其他方法。这些方法找不到相应的对象(视图和菜单),除非我在 onCreate 之前声明它们(我创建了通用私有视图和菜单对象),然后我在方法的修饰符上得到错误。

标签: java android android-studio


【解决方案1】:

试试这个,让我们知道它是否有效:


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

我已关闭该方法并删除了可能不在您的实际源代码中但出现在上方的三重反引号。

【讨论】:

  • 什么都没有改变。对三次反引号感到抱歉,但是这段代码会产生与以前相同的编译时错误,这尤其令人困惑,因为其他应用程序编译得很好,而我使用的方法可以很好地找到它们的对象。
  • 我已经关闭了一个可能丢失的括号......并在上面被其他人建议为丢失。如果这不起作用,您可以分享您遇到的堆栈跟踪|错误吗?
  • 你知道如何在 Markdown 中创建可折叠的“隐藏”消息吗? logcat 在这里很长,我想发布我的文件的全部内容,包括注释掉的部分。
猜你喜欢
  • 1970-01-01
  • 2017-02-20
  • 2012-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-10
  • 2015-06-23
相关资源
最近更新 更多