4

I am trying to ask user to enable GPS, I am new to app development I found some resources from internet of my requirement. I took code from one website and implemented on my app but when I try to build I am getting an error of cannot find symbol

See my java code below

package gps.gpstest;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;


public class MainActivity extends AppCompatActivity {
    Context context;
    private Context activity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE );
        boolean statusOfGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        if(!statusOfGPS) 
        {
            buildAlertMessageNoGps();
        }
    }

    private void buildAlertMessageNoGps() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
        builder.setMessage(R.string.gps_disabled)
                .setCancelable(false)
                .setTitle(R.string.gps_disabled_title)
                .setPositiveButton(R.string.enable,
                        new DialogInterface.OnClickListener() {
                            public void onClick(
                                    @SuppressWarnings("unused") final DialogInterface dialog,
                                    @SuppressWarnings("unused") final int id) {
                                startActivity(new Intent(
                                        android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                            }
                        })
                .setNegativeButton(R.string.cancel,
                        new DialogInterface.OnClickListener() {
                            public void onClick(final DialogInterface dialog,
                                                @SuppressWarnings("unused") final int id) {
                                dialog.cancel();

                            }
                        });
        final AlertDialog alert = builder.create();
        alert.show();
    }

}

Errors during build process

Error:(31, 73) error: cannot find symbol method getActivity() Error:(44, 44) error: cannot find symbol variable cancel Error:(35, 44) error: cannot find symbol variable enable Error:(34, 35) error: cannot find symbol variable gps_disabled_title Error:(32, 36) error: cannot find symbol variable gps_disabled Error:Execution failed for task ':app:compileDebugJavaWithJavac'.

Help me out

InsaneCat
  • 2,115
  • 5
  • 21
  • 40
Jason
  • 415
  • 1
  • 6
  • 16

3 Answers3

0

Error:(31, 73) error: cannot find symbol method getActivity()

The getActivity() is used in fragments to get context not in activity

To get context in activity use

  • this
  • YourActvity.this
  • getApplicationContext()

Use this

final AlertDialog.Builder builder = new AlertDialog.Builder(context);

Instead of this

final AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());

error: cannot find symbol variable enable Error:(34, 35) error: cannot find symbol variable gps_disabled_title Error:(32, 36) error: cannot find symbol variable gps_disabled

You need to create following resource in string.xml check String resources

  • gps_disabled
  • cancel
  • enable
  • gps_disabled_title
  • gps_disabled
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
0
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
Syed Danish Haider
  • 1,334
  • 11
  • 15
0

Let's go through your errors:

Error:(44, 44) error: cannot find symbol variable cancel

Error:(35, 44) error: cannot find symbol variable enable

Error:(34, 35) error: cannot find symbol variable gps_disabled_title

Error:(32, 36) error: cannot find symbol variable gps_disabled

You're not accessing string resources correctly. Instead of:

.setTitle(R.string.gps_disabled_title);

You should do:

.setTitle(getResources().getString(R.string.gps_disabled_title));

Error:(31, 73) error: cannot find symbol method getActivity()

As you're already on an activity, you should retrieve its context as follows:

final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
Community
  • 1
  • 1
Cris
  • 774
  • 9
  • 32