在 Android Gradle 插件版本 8.0 中,资源 ID 默认为非最终的,避免在 switch case 语句中使用它们
Resource IDs will be non-final by default in Android Gradle Plugin version 8.0, avoid using them in switch case statements
我在 Android Studio 中收到关于我的导航抽屉资源的警告。警告是 Resource IDs will be non-final by default in Android Gradle Plugin version 8.0, avoid using them in switch case statements
。我尝试使用方法 if
来更新我的代码,但我不会 'converted right'。我在互联网上发现这个 article 可以帮助我转换我的代码,但它似乎对我不起作用。我想知道我是否遗漏了什么。
下面是之前和之后的想法,here是我的完整activity因为我看到很多人用过后都有这个public void onClick...
而我没有使用它。我有 navigationView.setNavigationItemSelectedListener(menuItem -> {
.
Before:
navigationView.setNavigationItemSelectedListener(menuItem -> {
switch (menuItem.getItemId())
{
case R.id.nav_drawer_settings:
Intent intent = new Intent (MainActivity.this, SettingsActivity.class);
startActivity(intent);
break;
case R.id.nav_drawer_whitelist:
intent = new Intent (MainActivity.this, WhitelistActivity.class);
startActivity(intent);
break;
case R.id.nav_drawer_clipboard_cleaner:
intent = new Intent (MainActivity.this, ClipboardActivity.class);
startActivity(intent);
break;
case R.id.nav_drawer_invalid_media_cleaner:
intent = new Intent (MainActivity.this, InvalidActivity.class);
startActivity(intent);
break;
case R.id.nav_drawer_about:
intent = new Intent (MainActivity.this, AboutActivity.class);
startActivity(intent);
break;
case R.id.nav_drawer_support:
Intent newIntent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("https://www.paypal.me/d4rkmichaeltutorials"));
startActivity(newIntent);
break;
case R.id.nav_drawer_share:{
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "https://play.google.com/store/apps/details?id=com.d4rk.cleaner";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Try right now!");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share using..."));
}
break;
}
return false;
});
}
After:
navigationView.setNavigationItemSelectedListener(menuItem -> {
switch (view.getId()) {
if (item.getItemId() == R.id.nav_drawer_settings) {
Intent intent = new Intent (MainActivity.this, SettingsActivity.class);
startActivity(intent);
}
if (item.getItemId() == R.id.nav_drawer_whitelist) {
Intent intent = new Intent (MainActivity.this, WhitelistActivity.class);
startActivity(intent);
}
if (item.getItemId() == R.id.nav_drawer_clipboard_cleaner) {
Intent intent = new Intent (MainActivity.this, ClipboardActivity.class);
startActivity(intent);
}
if (item.getItemId() == R.id.nav_drawer_invalid_media_cleaner) {
Intent intent = new Intent (MainActivity.this, InvalidActivity.class);
startActivity(intent);
}
if (item.getItemId() == R.id.nav_drawer_about) {
Intent intent = new Intent (MainActivity.this, AboutActivity.class);
startActivity(intent);
}
if (item.getItemId() == R.id.nav_drawer_support) {
Intent openURL = new Intent(Intent.ACTION_VIEW);
openURL.setData(Uri.parse("https://www.paypal.me/d4rkmichaeltutorials"));
startActivity(openURL);
}
if (item.getItemId() == R.id.nav_drawer_share) {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "https://play.google.com/store/apps/details?id=com.d4rk.cleaner";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Try right now!");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share using..."));
}
}
return false;
});
}
提前致谢! :D
在您发布的后续代码中,不再需要开关。
它应该只通过编写以下内容工作:
if (item.getItemId() == R.id.nav_drawer_settings) {
Intent intent = new Intent (MainActivity.this, SettingsActivity.class);
startActivity(intent);
}
if (item.getItemId() == R.id.nav_drawer_whitelist) {
Intent intent = new Intent (MainActivity.this, WhitelistActivity.class);
startActivity(intent);
}
if (item.getItemId() == R.id.nav_drawer_clipboard_cleaner) {
Intent intent = new Intent (MainActivity.this, ClipboardActivity.class);
startActivity(intent);
}
if (item.getItemId() == R.id.nav_drawer_invalid_media_cleaner) {
Intent intent = new Intent (MainActivity.this, InvalidActivity.class);
startActivity(intent);
}
if (item.getItemId() == R.id.nav_drawer_about) {
Intent intent = new Intent (MainActivity.this, AboutActivity.class);
startActivity(intent);
}
if (item.getItemId() == R.id.nav_drawer_support) {
Intent openURL = new Intent(Intent.ACTION_VIEW);
openURL.setData(Uri.parse("https://www.paypal.me/d4rkmichaeltutorials"));
startActivity(openURL);
}
if (item.getItemId() == R.id.nav_drawer_share) {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "https://play.google.com/store/apps/details?id=com.d4rk.cleaner";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Try right now!");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share using..."));
}
我在 Android Studio 中收到关于我的导航抽屉资源的警告。警告是 Resource IDs will be non-final by default in Android Gradle Plugin version 8.0, avoid using them in switch case statements
。我尝试使用方法 if
来更新我的代码,但我不会 'converted right'。我在互联网上发现这个 article 可以帮助我转换我的代码,但它似乎对我不起作用。我想知道我是否遗漏了什么。
下面是之前和之后的想法,here是我的完整activity因为我看到很多人用过后都有这个public void onClick...
而我没有使用它。我有 navigationView.setNavigationItemSelectedListener(menuItem -> {
.
Before:
navigationView.setNavigationItemSelectedListener(menuItem -> {
switch (menuItem.getItemId())
{
case R.id.nav_drawer_settings:
Intent intent = new Intent (MainActivity.this, SettingsActivity.class);
startActivity(intent);
break;
case R.id.nav_drawer_whitelist:
intent = new Intent (MainActivity.this, WhitelistActivity.class);
startActivity(intent);
break;
case R.id.nav_drawer_clipboard_cleaner:
intent = new Intent (MainActivity.this, ClipboardActivity.class);
startActivity(intent);
break;
case R.id.nav_drawer_invalid_media_cleaner:
intent = new Intent (MainActivity.this, InvalidActivity.class);
startActivity(intent);
break;
case R.id.nav_drawer_about:
intent = new Intent (MainActivity.this, AboutActivity.class);
startActivity(intent);
break;
case R.id.nav_drawer_support:
Intent newIntent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("https://www.paypal.me/d4rkmichaeltutorials"));
startActivity(newIntent);
break;
case R.id.nav_drawer_share:{
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "https://play.google.com/store/apps/details?id=com.d4rk.cleaner";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Try right now!");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share using..."));
}
break;
}
return false;
});
}
After:
navigationView.setNavigationItemSelectedListener(menuItem -> {
switch (view.getId()) {
if (item.getItemId() == R.id.nav_drawer_settings) {
Intent intent = new Intent (MainActivity.this, SettingsActivity.class);
startActivity(intent);
}
if (item.getItemId() == R.id.nav_drawer_whitelist) {
Intent intent = new Intent (MainActivity.this, WhitelistActivity.class);
startActivity(intent);
}
if (item.getItemId() == R.id.nav_drawer_clipboard_cleaner) {
Intent intent = new Intent (MainActivity.this, ClipboardActivity.class);
startActivity(intent);
}
if (item.getItemId() == R.id.nav_drawer_invalid_media_cleaner) {
Intent intent = new Intent (MainActivity.this, InvalidActivity.class);
startActivity(intent);
}
if (item.getItemId() == R.id.nav_drawer_about) {
Intent intent = new Intent (MainActivity.this, AboutActivity.class);
startActivity(intent);
}
if (item.getItemId() == R.id.nav_drawer_support) {
Intent openURL = new Intent(Intent.ACTION_VIEW);
openURL.setData(Uri.parse("https://www.paypal.me/d4rkmichaeltutorials"));
startActivity(openURL);
}
if (item.getItemId() == R.id.nav_drawer_share) {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "https://play.google.com/store/apps/details?id=com.d4rk.cleaner";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Try right now!");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share using..."));
}
}
return false;
});
}
提前致谢! :D
在您发布的后续代码中,不再需要开关。
它应该只通过编写以下内容工作:
if (item.getItemId() == R.id.nav_drawer_settings) {
Intent intent = new Intent (MainActivity.this, SettingsActivity.class);
startActivity(intent);
}
if (item.getItemId() == R.id.nav_drawer_whitelist) {
Intent intent = new Intent (MainActivity.this, WhitelistActivity.class);
startActivity(intent);
}
if (item.getItemId() == R.id.nav_drawer_clipboard_cleaner) {
Intent intent = new Intent (MainActivity.this, ClipboardActivity.class);
startActivity(intent);
}
if (item.getItemId() == R.id.nav_drawer_invalid_media_cleaner) {
Intent intent = new Intent (MainActivity.this, InvalidActivity.class);
startActivity(intent);
}
if (item.getItemId() == R.id.nav_drawer_about) {
Intent intent = new Intent (MainActivity.this, AboutActivity.class);
startActivity(intent);
}
if (item.getItemId() == R.id.nav_drawer_support) {
Intent openURL = new Intent(Intent.ACTION_VIEW);
openURL.setData(Uri.parse("https://www.paypal.me/d4rkmichaeltutorials"));
startActivity(openURL);
}
if (item.getItemId() == R.id.nav_drawer_share) {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "https://play.google.com/store/apps/details?id=com.d4rk.cleaner";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Try right now!");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share using..."));
}