【问题标题】:The method getSupportFragmentManager() is undefined for the type MainActivity: android方法 getSupportFragmentManager() 未为 MainActivity 类型定义:android
【发布时间】:2014-11-20 19:53:10
【问题描述】:

当我将语句更改为:时出现此错误:The method getSupportFragmentManager() is undefined for the type MainActivity.

SupportMapFragment fm = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);

然后我收到此错误:

Multiple markers at this line
- Cannot cast from Fragment to 
 SupportMapFragment
- Line breakpoint:MainActivity [line: 52] - 
 onCreate(Bundle)

源代码:

 package in.wptrafficanalyzer.proximitymapv2;

import android.app.Activity;
import android.app.Dialog;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color; 
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends Activity {

GoogleMap googleMap;
LocationManager locationManager;
PendingIntent pendingIntent;
SharedPreferences sharedPreferences;

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


    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

    if(status!=ConnectionResult.SUCCESS){ 

        int requestCode = 10;
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
        dialog.show();

    }else {             

        SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

        googleMap = fm.getMap();

        googleMap.setMyLocationEnabled(true);           


        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);


        sharedPreferences = getSharedPreferences("location", 0);

        String lat = sharedPreferences.getString("lat", "0");

        String lng = sharedPreferences.getString("lng", "0");

        String zoom = sharedPreferences.getString("zoom", "0");

        if(!lat.equals("0")){

            drawCircle(new LatLng(Double.parseDouble(lat), Double.parseDouble(lng)));

            drawMarker(new LatLng(Double.parseDouble(lat), Double.parseDouble(lng)));

            googleMap.moveCamera(CameraUpdateFactory.newLatLng(new                LatLng(Double.parseDouble(lat), Double.parseDouble(lng))));

            googleMap.animateCamera(CameraUpdateFactory.zoomTo(Float.parseFloat(zoom)));                


        }


        googleMap.setOnMapClickListener(new OnMapClickListener() {

            @Override
            public void onMapClick(LatLng point) {

                googleMap.clear();      

                drawMarker(point);

                drawCircle(point);                  

                Intent proximityIntent = new Intent("in.wptrafficanalyzer.activity.proximity");                 

                pendingIntent = PendingIntent.getActivity(getBaseContext(), 0,   proximityIntent,Intent.FLAG_ACTIVITY_NEW_TASK);                    


                locationManager.addProximityAlert(point.latitude, point.longitude, 20, -1, pendingIntent);  

                SharedPreferences.Editor editor = sharedPreferences.edit();

                editor.putString("lat", Double.toString(point.latitude));

                editor.putString("lng", Double.toString(point.longitude));

                editor.putString("zoom", Float.toString(googleMap.getCameraPosition().zoom));               

                editor.commit();                

                Toast.makeText(getBaseContext(), "Proximity Alert is added", Toast.LENGTH_SHORT).show();                    

            }
        });    

        googleMap.setOnMapLongClickListener(new OnMapLongClickListener() {              
            @Override
            public void onMapLongClick(LatLng point) {
                Intent proximityIntent = new Intent("in.wptrafficanalyzer.activity.proximity");

                pendingIntent = PendingIntent.getActivity(getBaseContext(), 0, proximityIntent,Intent.FLAG_ACTIVITY_NEW_TASK);

                locationManager.removeProximityAlert(pendingIntent);

                googleMap.clear();

                SharedPreferences.Editor editor = sharedPreferences.edit();

                editor.clear();

                editor.commit();

                Toast.makeText(getBaseContext(), "Proximity Alert is removed", Toast.LENGTH_LONG).show();
            }
        });           
    }   
}   

private void drawMarker(LatLng point){
    MarkerOptions markerOptions = new MarkerOptions();                  

    markerOptions.position(point);

    googleMap.addMarker(markerOptions);

}


private void drawCircle(LatLng point){

    CircleOptions circleOptions = new CircleOptions();

    circleOptions.center(point);

    circleOptions.radius(20);

    circleOptions.strokeColor(Color.BLACK);

    circleOptions.fillColor(0x30ff0000);

    circleOptions.strokeWidth(2);

    googleMap.addCircle(circleOptions);

}   

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

【问题讨论】:

  • 你给support-library做广告了吗?
  • android.app.Activity 没有这个方法
  • 当我尝试扩展 ActionBarActivity 时。即将出现以下错误 ActionBarActivity 无法解析为类型
  • 您在 xml 中使用 SupportMapFragment 吗?

标签: java android eclipse google-maps


【解决方案1】:

您导入FragmentActivity,但您不使用它。 Activity 既不是来自 v4 也不是来自 v7 支持库。这应该有效:

public class MainActivity extends FragmentActivity {

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多