これはエラーです:
MainActivity 型の外側のインスタンスにアクセスできません。MainActivity 型の囲みインスタンス (x は MainActivity のインスタンスである xnew A()) で割り当てを修飾する必要があります。
このコード行の MyLocationListener.java のこのコード行でエラーが発生します
MyMapOverlays marker = new MyMapOverlays(p) ;
誰でも私を助けてくれますか?
コードは次のとおりです。
MainActivity.class
package com.sample.dorm_locator_gps;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import android.os.Bundle;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
public class MainActivity extends MapActivity {
LocationManager locMgr;
MyLocationListener locLstnr;
private MapView mapView;
private MapController mc;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview1);
mc = mapView.getController();
String coordinates[] = {"30", "71"};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
GeoPoint p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(7);
mapView.invalidate();
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
/*My overlay Class starts*/
class MyMapOverlays extends com.google.android.maps.Overlay
{
GeoPoint location = null;
public MyMapOverlays(GeoPoint location)
{
super();
this.location = location;
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow)
{
super.draw(canvas, mapView, shadow);
//translate the screen pixels
Point screenPoint = new Point();
mapView.getProjection().toPixels(this.location, screenPoint);
//add the image
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.our_cross_image),
screenPoint.x, screenPoint.y , null); //Setting the image location on the screen (x,y).
}
}
/*My overlay Class ends*/
}
MyLocationListener.java
package com.sample.dorm_locator_gps;
import java.util.List;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.sample.dorm_locator_gps.MainActivity.MyMapOverlays;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.widget.Toast;
public class MyLocationListener implements LocationListener
{
private MapController mc;
private MapView mapView;
public void onLocationChanged(Location loc) {
// TODO Auto-generated method stub
loc.getLatitude();
loc.getLongitude();
String Text = "My current location is: " + "Latitud = " +
loc.getLatitude() + "Longitud = " + loc.getLongitude();
Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
String coordinates[] = {""+loc.getLatitude(), ""+loc.getLongitude()};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
GeoPoint p = new GeoPoint((int) (lat * 1E6),(int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(7);
//add a location marker.
MyMapOverlays marker = new MyMapOverlays(p) ;
List listOfOverLays = mapView.getOverlays();
listOfOverLays.clear();
listOfOverLays.add(marker);
mapView.invalidate();
}
private Context getApplicationContext() {
// TODO Auto-generated method stub
return null;
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText( getApplicationContext(),"Gps Disabled", Toast.LENGTH_SHORT ).show();
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}