Reputation: 31
I was trying to make google map application but when i try to build the app it gives me a weird error on some casting sentences please help the error the error 1 the androidstudio can not find R Class
This is the map activity where i am loading the map.............................................................................................................................
public class MapActivity extends AppCompatActivity implements OnMapReadyCallback {
public void onMapReady(GoogleMap googleMap) {
Toast.makeText(this, "Map is Ready", Toast.LENGTH_SHORT).show();
Log.d(TAG, "Map is Ready");
mMap = googleMap;
if (mLocationPermissionGranted)
{
getDeviceLocation();
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, 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;
}
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(false);
init();
}
}
private static final String TAG = "MapActivity";
private static final String FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;
private static final String COARSE_LOCATION = Manifest.permission.ACCESS_COARSE_LOCATION;
private Boolean mLocationPermissionGranted = false;
private static final int LOCATON_PERMISSION_REQUEST_CODE = 1234;
private static final float DEFAULT_ZOOM= 15f;
private GoogleMap mMap;
private FusedLocationProviderClient mFusedLocationProviderClient;
private EditText mSearchText;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
mSearchText = (EditText) findViewById(R.id.input_search);
getLocationPermission();
/* MobileAds.initialize(this,"ca-app-pub-3940256099942544~3347511713");
mAdView = (AdView)findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().addTestDevice("7B0B6DDFA6EB4A71955387A3EA155884").build();
mAdView.loadAd(adRequest);*/
}
private void init()
{
Log.d(TAG,"initializing map");
mSearchText.setOnEditorActionListener(new TextView.OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if(actionId== EditorInfo.IME_ACTION_SEARCH || actionId==EditorInfo.IME_ACTION_DONE
||actionId==KeyEvent.ACTION_DOWN || actionId== KeyEvent.KEYCODE_ENTER)
{
geoLocate();
}
return false;
}
});
}
private void geoLocate()
{
Log.d(TAG,"locating");
String searchString = mSearchText.getText().toString();
Geocoder geocoder = new Geocoder(MapActivity.this);
List<Address> list = new ArrayList<>();
try
{
list = geocoder.getFromLocationName(searchString,1);
}
catch(IOException e)
{
Log.e(TAG,"IOException"+ e.getMessage());
}
if(list.size()>0)
{
Address address = list.get(0);
Log.d(TAG,"found location" + address.toString());
// Toast.makeText(this,address.toString(),Toast.LENGTH_SHORT).show();
}
}
private void getDeviceLocation()
{
Log.d(TAG,"GetDeviceLocation");
mFusedLocationProviderClient= LocationServices.getFusedLocationProviderClient(this);
try
{
if(mLocationPermissionGranted)
{
Task location = mFusedLocationProviderClient.getLastLocation();
location.addOnCompleteListener(new OnCompleteListener()
{
@Override
public void onComplete(@NonNull Task task)
{
if(task.isSuccessful())
{
Log.d(TAG,"location found");
Location CurrentLocation = (Location) task.getResult();
moveCamera(new LatLng(CurrentLocation.getLatitude(),CurrentLocation.getLongitude()),DEFAULT_ZOOM);
}
else
{
Log.d(TAG,"location not found");
Toast.makeText(MapActivity.this,"unable to find location",Toast.LENGTH_SHORT).show();
}
}
});
}
}catch (SecurityException e)
{
Log.e(TAG,"security exception"+e.getMessage());
}
}
private void moveCamera(LatLng latlng,float zoom)
{
Log.d(TAG,"move camera to location lat:"+latlng.latitude + ",lng" + latlng.longitude);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng,zoom));
}
private void initMap()
{
Log.d(TAG,"initializing map");
SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(MapActivity.this);
}
private void getLocationPermission()
{
Log.d(TAG,"Getting permission");
String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION};
if(ContextCompat.checkSelfPermission(this.getApplicationContext(),FINE_LOCATION)==PackageManager.PERMISSION_GRANTED)
{
if(ContextCompat.checkSelfPermission(this.getApplicationContext(),COARSE_LOCATION)==PackageManager.PERMISSION_GRANTED)
{
mLocationPermissionGranted = true;
initMap();
}
else
{
ActivityCompat.requestPermissions(this,permissions,LOCATON_PERMISSION_REQUEST_CODE);
}
}
else
{
ActivityCompat.requestPermissions(this,permissions,LOCATON_PERMISSION_REQUEST_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
{
Log.d(TAG,"onRequestPermissionsResult:called");
// super.onRequestPermissionsResult(requestCode, permissions, grantResults);
mLocationPermissionGranted = false;
switch(requestCode)
{
case LOCATON_PERMISSION_REQUEST_CODE:
{
if(grantResults.length>0 )
{
for(int i = 0 ; i < grantResults.length;i++)
{
if(grantResults[i]!= PackageManager.PERMISSION_GRANTED)
{
mLocationPermissionGranted = false;
Log.d(TAG,"onRequestPermissionsResult:failed");
return;
}
}
Log.d(TAG,"onRequestPermissionsResult:granted");
mLocationPermissionGranted= true;
initMap();
}
}
}
}
}
Upvotes: 0
Views: 48
Reputation: 171
Go to Build
-> Clean Project
Project
It surely will work.If not restart Android Studio
Upvotes: 1