【发布时间】:2021-02-13 20:02:34
【问题描述】:
我正在 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 feature (...) not supported at this language level. I can't compile 的线程以找出错误可能来自哪里,但我检查了 Project JDK 版本和 Android Studio JRE,它们在“java 版本 1.8”上匹配
【问题讨论】:
-
永远不要捕获 nullpointerexceptions 而是检查 null。