Navigation Drawer - Call camera through Menu

2

I have a problem, get the Android Studio Navigation Drawer Layout to do my project, however when I want to call the camera class error.

  

I created another class for the camera, where I have   GPS and where the image will be stored.

public class Camera extends Principal 
 {
private AlertDialog alerta;
public static final int IMAGEM_INTERNA = 1313;
public void Tirafoto (View view) 
  {
    LocationManager locationManager = (LocationManager)
    getSystemService(Context.LOCATION_SERVICE);
    final boolean GPSEnabled = locationManager.isProviderEnabled
(LocationManager.GPS_PROVIDER);
    if (GPSEnabled) {
    Intent camera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(camera, IMAGEM_INTERNA);
    } else {
        AlertDialog.Builder alertas = new AlertDialog.Builder(this);
        alertas.setTitle("Ligar GPS");
        alertas.setMessage("Para usar essa função você deve ativar seu GPS");
        alertas.setPositiveButton("Ativar GPS", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
            }
        });
        alertas.setNegativeButton("Não Ligar", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
 Toast.makeText(Camera.this, "Camera disponivel somente com o 
 uso do GPS=" + arg1, Toast.LENGTH_SHORT).show();
            }
        });
        alerta = alertas.create();
        alerta.show();
    }
}

}

And in the main Activity I have

if (id == R.id.nav_camera) {
        Camera camera =  new Camera();

I do not know how to continue because I tried in several ways but could not make it work.

    
asked by anonymous 11.05.2016 / 16:15

1 answer

0

I have simulated your project here and the error that happens is: java.lang.IllegalStateException: System services not available to Activities before onCreate ()

I did not understand the reason for the Camera extender class of the Main Activity. Because you do not call the code directly in the NavigationDrawer item click:

if (id == R.id.nav_camera) {
        // Handle the camera action
        LocationManager locationManager = (LocationManager)
                getSystemService(Context.LOCATION_SERVICE);
        final boolean GPSEnabled = locationManager.isProviderEnabled
                (LocationManager.GPS_PROVIDER);
        if (GPSEnabled) {
            Intent camera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(camera, IMAGEM_INTERNA);
        } else {
            AlertDialog.Builder alertas = new AlertDialog.Builder(this);
            alertas.setTitle("Ligar GPS");
            alertas.setMessage("Para usar essa função você deve ativar seu GPS");
            alertas.setPositiveButton("Ativar GPS", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    startActivity(intent);
                }
            });
            alertas.setNegativeButton("Não Ligar", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    Toast.makeText(MainActivity.this, "Camera disponivel somente com o uso do GPS=" + arg1, Toast.LENGTH_SHORT).show();
                }
            });
            alerta = alertas.create();
            alerta.show();
        }
    }
    
11.05.2016 / 20:35