gpt4 book ai didi

java - 在 Android Gradle 插件版本 8.0 中,资源 ID 默认不是最终的,请避免在 switch case 语句中使用它们

转载 作者:行者123 更新时间:2023-12-04 23:39:37 26 4
gpt4 key购买 nike

我在 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更新我的代码,但我不会“正确转换”。我在网上找到了这个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..."));
}

关于java - 在 Android Gradle 插件版本 8.0 中,资源 ID 默认不是最终的,请避免在 switch case 语句中使用它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69120822/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com