简体   繁体   English

不支持 Try_catch 块 android 工作室语言级别 8?

[英]Try_catch block not supported android studio language level 8?

I am making an app in Android Studio:我正在 Android Studio 中制作应用程序:

public class MainActivity extends AppCompatActivity {

    SharedPreferences sharedPreferences;
    EditText noteTitleField, noteContentField;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sharedPreferences = getSharedPreferences("NoteAppPrefs", Context.MODE_PRIVATE);
        String setValueInPrefs = sharedPreferences.getString("alreadyLaunched", null);
        try (setValueInPrefs.equals("0")) {
            Log.d("debugging", "App running for first time... launching setup");
            setupForFirstTime();
        } catch (NullPointerException e) {
            noteContentField = findViewById(R.id.noteContentTextBox);
            noteTitleField = findViewById(R.id.noteTitleTextBox);
            Toast.makeText(MainActivity.this, R.string.welcomeMessage, Toast.LENGTH_LONG);
        }
    }
}

However, I am getting "Resource References are not supported at language level 8".但是,我收到“语言级别 8 不支持资源引用”。

I checked some threads like this one intellij feature (...) not supported at this language level.我检查了一些线程,例如此语言级别不支持的 intellij 功能(...)。 I can't compile to figure out where the error could be coming from but I checked both Project JDK versions and Android Studio JRE and they match on "java version 1.8" 我无法编译以找出错误可能来自哪里,但我检查了 Project JDK 版本和 Android Studio JRE,它们在“java 版本 1.8”上匹配

Thank you for helping感谢您的帮助

The line线

try (setValueInPrefs.equals("0")) {

is not valid.无效。 In Java 8 code code in the parentheses must be an assignment statement ( VariableDeclaratorId = Expression ) andJava 8代码中括号中的代码必须是赋值语句( VariableDeclaratorId = Expression )和

The type of a variable declared in a resource specification must be a subtype of AutoCloseable, or a compile-time error occurs.资源规范中声明的变量类型必须是 AutoCloseable 的子类型,否则会发生编译时错误。

Java 9 relaxed the part with the assignment statement (now it can also be a variable or a field), but still Java 9放宽了赋值语句的部分(现在也可以是变量或字段),但还是

The type of a variable declared or referred to as a resource in a resource specification must be a subtype of AutoCloseable, or a compile-time error occurs.在资源规范中声明或引用为资源的变量的类型必须是 AutoCloseable 的子类型,否则会发生编译时错误。

You are using setValueInPrefs.equals("0") as expression, which results in a boolean value and boolean is not a subtype of AutoCloseable.您正在使用setValueInPrefs.equals("0")作为表达式,这会导致 boolean 值和boolean不是 AutoCloseable 的子类型。

To fix it you should replace the try block with要修复它,您应该将try块替换为

    if (setValueInPrefs != null) {
        if (setValueInPrefs.equals("0")) {
            Log.d("debugging", "App running for first time... launching setup");
            setupForFirstTime();
        }
    } else {
        noteContentField = findViewById(R.id.noteContentTextBox);
        noteTitleField = findViewById(R.id.noteTitleTextBox);
        Toast.makeText(MainActivity.this, R.string.welcomeMessage, Toast.LENGTH_LONG);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM