繁体   English   中英

无法访问的语句,menuItem.setActionView(R.layout.theme_switch);

[英]unreachable statement, menuItem.setActionView(R.layout.theme_switch);

  1. 第一节

包 com.gmsofficial.GmSOOfficial;

 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;

    } }

break; menuItem.setActionView(R.layout.theme_switch);

我猜你不知道break是什么? 这两行紧挨着对方,您的编译器/编辑器正将您指向它。 break 将完全跳出switch语句,该menuItem...行无法运行,这就是为什么编译器会告诉你:“这不可能是正确的;我不会帮你挖这个坟墓任何更远。”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM