导航抽屉不会切换活动但会获得选择[重复]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了导航抽屉不会切换活动但会获得选择[重复]相关的知识,希望对你有一定的参考价值。

当我单击导航抽屉菜单时,抽屉将关闭但不执行任何操作。在我的情况下,它必须转移到另一个活动/干杯消息,但两个都没有完成。采取点击操作,但没有得到响应。

我希望在单击导航菜单时更改活动。建议所需的更改以使抽屉作为活动切换器工作

public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener,NavigationView.OnNavigationItemSelectedListener
{

    private DrawerLayout drawer;

    private WebView webView;
    private String currentURL = "";
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // code for side bar

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        navigationView.setVerticalFadingEdgeEnabled(true);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
                R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();         

        webView = findViewById(R.id.webView);
        swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
        swipe.setOnRefreshListener((SwipeRefreshLayout.OnRefreshListener) this);

        // Define url that will open in webview
        String webViewUrl = "http://www.tekies.online";

        webView.getSettings().setjavascriptEnabled(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        webView.setScrollbarFadingEnabled(false);
        // webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setPluginState(PluginState.ON);
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setSupportZoom(true);

    }


    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        //here is the main place where we need to work on.
        int id=item.getItemId();
        switch (id){

            case R.id.nav_home:
                Intent h= new Intent(this,MainActivity.class);
                startActivity(h);
                break;
            case R.id.nav_profile:
                Intent i= new Intent(this,settings.class);
                startActivity(i);
                break;
            case R.id.nav_chat:
                Intent g= new Intent(this,settings.class);
                startActivity(g);
                break;
            case R.id.nav_share:
                Toast.makeText(getApplicationContext(), "No Link to Share!", Toast.LENGTH_SHORT).show();
                break;
            case R.id.nav_send:
                String link = "http://tekies.online/app";
                Intent referIntent = new Intent(Intent.ACTION_SEND);
                referIntent.setType("text/plain");
                referIntent.putExtra(Intent.EXTRA_TEXT,link); // your above url
                startActivity(Intent.createChooser(referIntent, "Share link using...."));
                break;
            case R.id.nav_message:
                Intent m= new Intent(this,settings.class);
                startActivity(m);
                break;
            case R.id.rate:
                Toast.makeText(getApplicationContext(), "Rate us!", Toast.LENGTH_SHORT).show();
                break;
            case R.id.exit:
                AlertDialog.Builder dialog = new AlertDialog.Builder(this);
                dialog.setTitle("Exit App Confirmation");
                dialog.setMessage("Are you sure ?");
                dialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        onBackPressed();
                        Toast.makeText(getApplicationContext(), "App Closed!", Toast.LENGTH_SHORT).show();
                    }
                });
                dialog.setCancelable(false);
                dialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        Toast.makeText(getApplicationContext(), "Operation Canceled!", Toast.LENGTH_SHORT).show();
                    }
                }).show();
                break;

        }
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

}

以上是关于导航抽屉不会切换活动但会获得选择[重复]的主要内容,如果未能解决你的问题,请参考以下文章

导航抽屉活动:在按钮单击时从片段移动到片段

单击导航抽屉的项目不会打开片段

所有活动中的Android导航抽屉[重复]

java 导航抽屉切换活动而不是片段。字体:https://stackoverflow.com/questions/19442378/navigation-drawer-to-switch-

禁用导航抽屉,在片段中切换主页按钮/向上指示器

如何在主片段中单击按钮时打开导航抽屉?