【问题标题】:unreachable statement, menuItem.setActionView(R.layout.theme_switch);无法访问的语句,menuItem.setActionView(R.layout.theme_switch);
【发布时间】:2020-11-24 12:09:01
【问题描述】:
  1. 第一节

包 com.gmsofficial.GmSOfficial;

 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.os.Bundle;
 import android.view.MenuItem;
 import android.widget.CompoundButton;
 import android.widget.Switch;

 import androidx.annotation.NonNull;
 import androidx.appcompat.app.ActionBarDrawerToggle;
 import androidx.appcompat.app.AppCompatActivity;
 import androidx.appcompat.app.AppCompatDelegate;
 import androidx.appcompat.widget.Toolbar;
 import androidx.drawerlayout.widget.DrawerLayout;
  1. 第二节

导入 com.google.android.material.navigation.NavigationView;

 public class NavigationDrawer extends AppCompatActivity {


 DrawerLayout drawerLayout;
 ActionBarDrawerToggle actionBarDrawerToggle;
 NavigationView navigationView;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_navigation_drawer);

    if (loadState() == true){
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        setTheme(R.style.darkTheme);
    } else {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        setTheme(R.style.Theme_AppCompat);
    }

    setUpToolbar();
    navigationView = (NavigationView) findViewById(R.id.navigation_menu);
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
      

     

3.第三节

    @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            switch (menuItem.getItemId())
            {
                case R.id.nav_home:

                    Intent intent = new Intent(NavigationDrawer.this, NavigationDrawer.class);
                    startActivity(intent);
                    break;

                case R.id.nav_Website:

                    intent = new Intent(NavigationDrawer.this, Websitewebview.class);
                    startActivity(intent);
                    break;

                case R.id.nav_Mobiles:

                    intent = new Intent(NavigationDrawer.this, Mobileswebview.class);
                    startActivity(intent);
                    break;

             
  1. 第四节

                case R.id.nav_TechNews:
    
                    intent = new Intent(NavigationDrawer.this, TechNewswebview.class);
                    startActivity(intent);
                    break;
    
                case R.id.nav_Gadgets:
    
                    intent = new Intent(NavigationDrawer.this, Gadgetswebview.class);
                    startActivity(intent);
                    break;
    
                case R.id.nav_CustomROM:
    
                    intent = new Intent(NavigationDrawer.this, CustomRomwebview.class);
                    startActivity(intent);
                    break;
    
                    menuItem.setActionView(R.layout.theme_switch);
                    final Switch themeswitch = (Switch) menuItem.getActionView().findViewById(R.id.action_switch);
                    if (loadState() == true){
                        themeswitch.setChecked(true);
            }
    
  2. 无法访问的错误部分

                        themeswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                            if (isChecked) {
                                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                                saveState(true);
                                recreate();
                            } else {
                                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                                saveState(false);
                            }
                        }
                    });
                    break;
    
                case R.id.nav_AboutUs:
    
                    intent = new Intent(NavigationDrawer.this, AboutUS.class);
                    startActivity(intent);
                    break;
    
  3. 第六节 //粘贴您的隐私政策链接

    // case R.id.nav_Policy:{ // // Intent browserIntent = new Intent(Intent.ACTION_VIEW , Uri.parse("")); // startActivity(browserIntent); // // } // 休息; 案例 R.id.nav_share:{

         Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
         sharingIntent.setType("text/plain");
         String shareBody =  "http://play.google.com/store/apps/detail?id=" + getPackageName();
         String shareSub = "Try now";
         sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, shareSub);
         sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
         startActivity(Intent.createChooser(sharingIntent, "Share using"));
    
              }
                break;
            }
            return false;
        }
    });
    

    }

  4. 第七节

    public void setUpToolbar() {
    drawerLayout = findViewById(R.id.drawerLayout);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name);
    drawerLayout.addDrawerListener(actionBarDrawerToggle);
    actionBarDrawerToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.purple));
    actionBarDrawerToggle.syncState();
    

    }

    private void saveState(Boolean state){
    SharedPreferences sharedPreferences = getSharedPreferences("GmS Official", MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean("NightMode", state);
    editor.apply();
    

    }

     private Boolean loadState(){
    SharedPreferences sharedPreferences = getSharedPreferences("GmS Official", MODE_PRIVATE);
    Boolean state = sharedPreferences.getBoolean("NightMode", false);
    return state;
    

    } }

【问题讨论】:

  • 请原谅我的错误
  • 我们不知道该代码之间的关系。它在一个文件中吗?在一种方法中? ... ?我们也不知道您到底从哪里得到错误

标签: javascript java android


【解决方案1】:
break;
menuItem.setActionView(R.layout.theme_switch);

我猜你不知道break 做了什么?这两行紧挨着,你的编译器/编辑器正指向它。 break 将完全跳出switch 语句,menuItem... 行无法运行,这就是编译器告诉你的原因:“这不可能是正确的;我不会帮你挖这个坟墓更进一步。”。

【讨论】:

  • 为什么要帮助我
  • @Adi 你的问题相当于:“请教我java”。对于stackoverflow问题来说,这有点太宽泛了,但是那里有很多教程!
猜你喜欢
  • 2017-04-29
  • 2016-03-03
  • 2015-11-30
  • 2018-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
  • 2019-02-18
相关资源
最近更新 更多