Javascript 不可访问语句,menuItem.setActionView(R.layout.theme\u开关); 第一节

Javascript 不可访问语句,menuItem.setActionView(R.layout.theme\u开关); 第一节,javascript,java,android,Javascript,Java,Android,包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.

包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;
  • 第二节
  • 导入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.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;
    
                 
    
  • 第四节

                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);
            }
    
  • 不可达错误部分

                        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;
    
  • 第六节 //粘贴您的隐私策略链接

    //案例R.id.nav_政策:{ // //Intent browserIntent=新意图(Intent.ACTION\u视图,Uri.parse(“”); //startActivity(浏览器内容); // // } //中断; 案例R.id.nav_份额:{

         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;
        }
    });
    
    }

  • 第七节

    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();
    
    }

    }

    } }


  • 我猜你不知道
    break
    做什么?这两行紧挨着一行,编译器/编辑器正好指向它。break将完全跳出
    switch
    语句,该
    菜单项…
    行无法运行,这就是为什么编译器告诉您:“这不可能是正确的;我不会再帮您挖这个坟墓了。”

    请解决我的错误我们不知道代码之间的关系。它在一个文件中吗?用一种方法?我们也不知道你的错误是从哪里来的为什么请帮忙me@Adi你的问题相当于:“请教我java”。这对于stackoverflow问题来说有点太宽泛了,但是有很多教程!
    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);