我正在Android中制作一个应用程序:
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版本和Android,它们在“JavaVersion1.8”上匹配。
发布于 2021-02-14 08:24:40
线
try (setValueInPrefs.equals("0")) {是无效的。在Java 8中,括号中的代码必须是赋值语句(VariableDeclaratorId = Expression)和
在资源规范中声明的变量的类型必须是AutoCloseable的子类型,否则会发生编译时错误。
Java 9使用赋值语句放松了部分(现在它也可以是变量或字段),但仍然
在资源规范中声明或称为资源的变量的类型必须是AutoCloseable的子类型,否则会发生编译时错误。
您使用setValueInPrefs.equals("0")作为表达式,这将导致布尔值,而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);
}https://stackoverflow.com/questions/66189328
复制相似问题