【问题标题】:Want to move to another activity from navigation drawer想要从导航抽屉移动到另一个活动
【发布时间】:2020-08-04 07:15:00
【问题描述】:

我有一个地图活动。它有一个导航抽屉。但是当我点击任何导航项目时,我无法进入另一个活动。我在这里附上我的代码。没有错误。当我单击导航抽屉的任何项目时,它不会移动到另一个活动。

     nv = (NavigationView) findViewById(R.id.nv);
            nv.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    int id = item.getItemId();
                    switch (id) {
                        case R.id.profile:

                            Intent i = new Intent(HomeActivity.this, ProfileActivity.class);
                            startActivity(i);
                            break;
                        case R.id.dona:
                            Intent j = new Intent(HomeActivity.this, DonateActivity.class);
                            startActivity(j);
                            break;

                        case R.id.pla:
                            Intent k = new Intent(HomeActivity.this, PlantActivity.class);
                            startActivity(k);
                            break;

                        case R.id.not:
                            Intent l = new Intent(HomeActivity.this, NotificationActivity.class);
                            startActivity(l);
                            break;

                        case R.id.hist:
                            Intent m = new Intent(HomeActivity.this, HistoryActivity.class);
                            startActivity(m);
                            break;

                        case R.id.hel:
                            Intent n = new Intent(HomeActivity.this, HelpActivity.class);
                            startActivity(n);
                            break;



                    }

                    return false;

                }
            });





 SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);


    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.ACCESS_FINE_LOCATION)
                    == PackageManager.PERMISSION_GRANTED) {
                buildGoogleApiClient();
                mMap.setMyLocationEnabled(true);
            }
        } else {
            buildGoogleApiClient();
            mMap.setMyLocationEnabled(true);
        }

    }






 protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API).build();
        mGoogleApiClient.connect();
    }

    @Override
    public void onConnected(Bundle bundle) {

        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(1000);
        mLocationRequest.setFastestInterval(1000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
        }

    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onLocationChanged(Location location) {

        mLastLocation = location;
        if (mCurrLocationMarker != null) {
            mCurrLocationMarker.remove();
        }
        //Place current location marker
        LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(latLng);
        markerOptions.title("Current Position");
        markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
        mCurrLocationMarker = mMap.addMarker(markerOptions);

        //move map camera
        mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        mMap.animateCamera(CameraUpdateFactory.zoomTo(11));

        //stop location updates
        if (mGoogleApiClient != null) {
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        }

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (t.onOptionsItemSelected(item))
            return true;
        int id = item.getItemId();



        if (id == R.string.action_settings) {
            return true;
        }


            return super.onOptionsItemSelected(item);


    }
}

【问题讨论】:

  • 你能告诉我你得到了什么错误
  • 你确定是 HomeActivity 吗?改用 getActivity
  • 没有错误。当我单击导航抽屉上的任何项目时,我必须移动到另一个活动。我已经添加了一个开关盒和一个意图。但是当我点击时没有任何反应
  • @Arahasya HomeActivity 是我当前的活动
  • @AseelAli 您的代码看起来不错..您必须调试您的代码,并检查所有活动是否已注册到清单文件。还要检查您当前的活动,即确保它是 HomeActivity

标签: android


【解决方案1】:

你需要return trueonNavigationItemSelected结尾

  nv.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    int id = item.getItemId();
                    switch (id) {
                        case R.id.profile:

                            Intent i = new Intent(HomeActivity.this, ProfileActivity.class);
                            startActivity(i);
                            break;

                        /*--your code--*/


                        case R.id.hist:
                            Intent m = new Intent(HomeActivity.this, HistoryActivity.class);
                            startActivity(m);
                            break;

                        case R.id.hel:
                            Intent n = new Intent(HomeActivity.this, HelpActivity.class);
                            startActivity(n);
                            break;



                    }

                yourDrawer.closeDrawer(GravityCompat.START);

                    return true;       //you need to return true here, not false

                }
            });

【讨论】:

【解决方案2】:

在仔细检查了您的完整代码后,我意识到 .setNavigationItemSelectedListener 没有被调用。即使抽屉正在展示,它也位于您活动的后面。所以你实际上并没有点击它。

所以简单的解决方案是:

nv.bringToFront();

在你初始化 nv 之后放这个

【讨论】:

  • 如果您觉得答案有用,也请点击向上箭头
  • 我需要更多帮助
  • 嘿,我需要帮助 我有一个包含地图和通知的活动。所以当我参加这个活动时,我想显示一个带有 4 个图像按钮的叠加图像。在访问地图之前。我将在下面附上来自谷歌驱动器的屏幕截图链接。请帮忙
  • 有什么问题
【解决方案3】:

试试这个

  • 我正在使用像这样的直接选择项目的点击方法

    public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    
    if (id == R.id.nav_buildings) {
        startActivity(new Intent(SuperAdminHomeActivity.this,BuildingsActivity.class));
    
    } else if (id == R.id.nav_companies) {
        startActivity(new Intent(SuperAdminHomeActivity.this,CompaniesActivity.class));
    
    } else if (id == R.id.nav_reports) {
        startActivity(new Intent(SuperAdminHomeActivity.this,ReportsActivity.class));
    
    } else if (id == R.id.nav_invoice) {
        startActivity(new Intent(SuperAdminHomeActivity.this,InvoiceActivity.class));
    
    } else if (id == R.id.nav_notifications) {
        startActivity(new Intent(SuperAdminHomeActivity.this,NotificationsActivity.class));
    
    } else if (id == R.id.nav_ticket) {
        startActivity(new Intent(SuperAdminHomeActivity.this,TicketActivity.class));
    
    }
    else if (id == R.id.nav_my_ticket) {
        startActivity(new Intent(SuperAdminHomeActivity.this,TicketsAddActivity.class));
    }
    
    else if(id==R.id.nav_logout){
        alertDialogue("Do you want to logout?");
    }
    
    drawer.closeDrawer(GravityCompat.START);
    return true;
     }
    
  • 我没有使用抽屉点击监听器。

  • 这是我的抽屉布局

它工作正常,请尝试

【讨论】:

  • 你已经关闭了这里的抽屉。这是你要关闭的抽屉布局吗?
  • 还是没有
【解决方案4】:

使您的导航视图成为全局变量

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多