首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >默认情况下,在Android插件版本8.0中,资源ID将是非最终的,避免在开关case语句中使用它们。

默认情况下,在Android插件版本8.0中,资源ID将是非最终的,避免在开关case语句中使用它们。
EN

Stack Overflow用户
提问于 2021-09-09 15:19:28
回答 3查看 3.2K关注 0票数 8

我在Android中有一个关于导航抽屉资源的警告。警告是Resource IDs will be non-final by default in Android Gradle Plugin version 8.0, avoid using them in switch case statements。我试图使用if方法来更新我的代码,但我不会“正确转换”。我在互联网上找到了这个文章来帮助我转换代码,但它似乎不适合我。我想知道我是不是错过了什么。

下面是创建一个想法的前后,这里是我的全部活动,因为我看到很多人在使用public void onClick...之后都有这个,而我不使用它。我有navigationView.setNavigationItemSelectedListener(menuItem -> {

Before:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-09-09 15:28:04

在您发布的后代码中,不再需要切换。

它只应通过编写以下内容来工作:

代码语言:javascript
复制
            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..."));
            }
票数 8
EN

Stack Overflow用户

发布于 2022-10-17 13:23:29

如果一个只想抑制整个应用程序的警告,那么将其添加到one build.gradle文件中:

代码语言:javascript
复制
android {
  lintOptions {
    disable 'NonConstantResourceId'
  }
}
票数 1
EN

Stack Overflow用户

发布于 2022-07-28 16:01:25

即使你把大小写R.id.nav_drawer_settings: AS (R.id.nav_drawer_settings):放在括号里,它也能工作

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69120822

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档