Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Try\u捕获块不支持android studio语言级别8?_Java_Android - Fatal编程技术网

Java Try\u捕获块不支持android studio语言级别8?

Java Try\u捕获块不支持android studio语言级别8?,java,android,Java,Android,我正在Android Studio中制作一个应用程序: public class MainActivity extends AppCompatActivity { SharedPreferences sharedPreferences; EditText noteTitleField, noteContentField; @Override protected void onCreate(Bundle savedInstanceState) {

我正在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);
        }
    }
}
然而,我得到“资源引用在语言级别8不受支持”

我检查了一些类似这样的线程以找出错误的来源,但我检查了Project JDK版本和Android Studio JRE,它们在“java 1.8版”上匹配

谢谢你帮我打电话

try (setValueInPrefs.equals("0")) {
无效。在代码中,括号中的代码必须是赋值语句(
VariableDeclaratorId=Expression
)和

资源规范中声明的变量类型必须是AutoCloseable的子类型,否则会发生编译时错误。

使用赋值语句放松该部分(现在它也可以是变量或字段),但仍然

在资源规范中声明或引用为资源的变量类型必须是AutoCloseable的子类型,否则会发生编译时错误。

您正在使用
setValueInPrefs.equals(“0”)
作为表达式,这会产生一个布尔值,
boolean
不是自动关闭的子类型

要修复它,您应该将
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);
    }

从不捕获nullpointerexceptions,而是检查null。