0

i am trying to open Gps activity from the main activity to retrieve location but it it crashes when i click the button and say can't open the activity

Update::: i changed the code just to see why the gps return null now i just want to be directed to the gps activity but it wouldn't i tried to enable multidex and install it in application class but it didn't work out

P.S : i am using min sdk 17 and target sdk 25 and the gps class works fine alone

Main activity :

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button two = (Button) findViewById(R.id.button);
        Recieve = (TextView) findViewById(R.id.textView2);
        two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sendMessage();
            }

        });
    }

    public void sendMessage() {
        Intent intent = new Intent(this, Activity2.class);
        startActivity(intent);

       // startActivityForResult(intent, REQUEST_CODE);
    }

   /* @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
            Response = data.getStringExtra("key");

            Recieve.setText("latitude is :" + Response);

        }
    }*/

}

Gps Activity :

    public void onConnected(Bundle connectionHint) {
        createLocationRequest();
        startLocationUpdates();
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        Location Location = LocationServices.FusedLocationApi.getLastLocation(GAC);
        if (Location != null) {
            Double Lat = Location.getLatitude();
            Double lon = Location.getLongitude();
            x = (TextView) findViewById(R.id.textView);
            y = (TextView) findViewById(R.id.textView2);
            x.setText("Ltitude is " + String.valueOf(Lat));
            y.setText("Longitude is " + String.valueOf(lon));
        }

    }

    @Override
    protected void onStart() {
        super.onStart();
        if (GAC != null) {
            GAC.connect();
        }
    }

    @Override

    protected void onStop() {
        GAC.disconnect();
        super.onStop();
    }

    @Override
    public void onConnectionSuspended(int cause) {
        Toast.makeText(this, "the connection suspended", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {

        Toast.makeText(this, "the connection failed", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onLocationChanged(Location location) {
        x = (TextView) findViewById(R.id.textView);
        y = (TextView) findViewById(R.id.textView2);
        x.setText("latitude is " + String.valueOf(location.getLatitude()));
        y.setText("longitude is " + String.valueOf(location.getLongitude()));
    }

    protected void createLocationRequest() {
        LR = new LocationRequest();
        LR.setInterval(5000);
        LR.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    }

    protected void startLocationUpdates() {

        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        LocationServices.FusedLocationApi.requestLocationUpdates(
                GAC, LR, (this));

    }



    protected void build_GAC(){
        GAC =new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        build_GAC();

    }

}

And that is the error i get :

E/dalvikvm: Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.internal.zzacw.zzg

1 Answers1

0

There are many ways to solve this as stated in this post

1.If you don't use Application Class, you can declare this class as the application in your AndroidManifest.xml.

<application android:name="android.support.multidex.MultiDexApplication">

2.Define that your Application extends MultiDexApplication class, like:

public class MyApplication extends MultiDexApplication {
    ...
}

3.Have your Application override attachBaseContext:

public class MyApplication extends Application {
    @Override
    protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);}
}

You can also try selecting only the required Google Play Services APIs to fix your problem or disabling Instant Run.

Before gradle:-

compile 'com.google.android.gms:play-services:8.4.0'

After gradle:-

compile 'com.google.android.gms:play-services-analytics:8.4.0'
compile 'com.google.android.gms:play-services-gcm:8.4.0'
compile 'com.google.android.gms:play-services-location:8.4
Community
  • 1
  • 1
Mr.Rebot
  • 6,703
  • 2
  • 16
  • 91