I am making an app in 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".
I checked some threads like this one intellij feature (...) not supported at this language level. 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"
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
) and
The type of a variable declared in a resource specification must be a subtype of AutoCloseable, or a compile-time error occurs.
Java 9 relaxed the part with the assignment statement (now it can also be a variable or a field), but still
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.
You are using setValueInPrefs.equals("0")
as expression, which results in a boolean value and boolean
is not a subtype of AutoCloseable.
To fix it you should replace the try
block with
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.