[英]Try_catch block not supported android studio language level 8?
我正在 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 不支持资源引用”。
我检查了一些线程,例如此语言级别不支持的 intellij 功能(...)。 我无法编译以找出错误可能来自哪里,但我检查了 Project JDK 版本和 Android Studio JRE,它们在“java 版本 1.8”上匹配
感谢您的帮助
线
try (setValueInPrefs.equals("0")) {
无效。 在Java 8代码中括号中的代码必须是赋值语句( VariableDeclaratorId = Expression
)和
资源规范中声明的变量类型必须是 AutoCloseable 的子类型,否则会发生编译时错误。
Java 9放宽了赋值语句的部分(现在也可以是变量或字段),但还是
在资源规范中声明或引用为资源的变量的类型必须是 AutoCloseable 的子类型,否则会发生编译时错误。
您正在使用setValueInPrefs.equals("0")
作为表达式,这会导致 boolean 值和boolean
不是 AutoCloseable 的子类型。
要修复它,您应该将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.