0

我想从初始活动中调用片段活动..

该应用程序由两个类组成。我要做的就是当我从 MainActivity 单击 btn1(Button) 时,它应该显示 Google Map V2 ..(我不想将其称为属于的片段活动到 MainActivity,,)

  1. MainActivity.java (活动)
  2. MapView.java (FragmentActivity)

但是,它显示以下错误

10-20 17:44:51.243: E/AndroidRuntime(1615): FATAL EXCEPTION: main
10-20 17:44:51.243: E/AndroidRuntime(1615): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mgrs.converter.pro/com.mgrs.converter.pro.MapView}: java.lang.NullPointerException

10-20 17:44:51.243: E/AndroidRuntime(1615):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-20 17:44:51.243: E/AndroidRuntime(1615):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)

10-20 17:44:51.243: E/AndroidRuntime(1615):     at android.app.ActivityThread.access$600(ActivityThread.java:130)

10-20 17:44:51.243: E/AndroidRuntime(1615):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)

10-20 17:44:51.243: E/AndroidRuntime(1615):     at android.os.Handler.dispatchMessage(Handler.java:99)

10-20 17:44:51.243: E/AndroidRuntime(1615):     at android.os.Looper.loop(Looper.java:137)
10-20 17:44:51.243: E/AndroidRuntime(1615):     at android.app.ActivityThread.main(ActivityThread.java:4745)

10-20 17:44:51.243: E/AndroidRuntime(1615):     at java.lang.reflect.Method.invokeNative(Native Method)
10-20 17:44:51.243: E/AndroidRuntime(1615):     at java.lang.reflect.Method.invoke(Method.java:511)

10-20 17:44:51.243: E/AndroidRuntime(1615):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-20 17:44:51.243: E/AndroidRuntime(1615):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-20 17:44:51.243: E/AndroidRuntime(1615):     at dalvik.system.NativeStart.main(Native Method)
10-20 17:44:51.243: E/AndroidRuntime(1615): Caused by: java.lang.NullPointerException
10-20 17:44:51.243: E/AndroidRuntime(1615):     at com.mgrs.converter.pro.MapView.onCreate(MapView.java:65)
10-20 17:44:51.243: E/AndroidRuntime(1615):     at android.app.Activity.performCreate(Activity.java:5008)
10-20 17:44:51.243: E/AndroidRuntime(1615):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-20 17:44:51.243: E/AndroidRuntime(1615):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-20 17:44:51.243: E/AndroidRuntime(1615):     ... 11 more

------MainActivity.java

public class MainActivity extends Activity {


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


        mLocMan = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        mProvider = mLocMan.getBestProvider(new Criteria(), true);
        LocationListener mListener = new Geocoord();



    //Button 0
    final Button btn = (Button)findViewById(R.id.btn);
    btn.setOnClickListener(new Button.OnClickListener() {
    public void onClick(View v) {
        ....    
    }
    }); //ends button0

    //Button 1
    Button btn1 = (Button)findViewById(R.id.btn1);
    btn1.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            btn.performClick();



            Intent intent = new Intent(MainActivity.this, MapView.class);
            startActivity(intent);
        }//ends onClick 

        });

    }//ends onCreate


}//ends Activity

------MapView.java

public class MapView extends FragmentActivity {

    GoogleMap mGoogleMap;
    //Variables vari;
    Converter converter = new Converter();



    //initial values for marker
    LocationManager mLocMan;
    String mProvider;
      //current position
      double lon = converter.getLongitude();
      double lat = converter.getLatitude();
      //site#1
      double lon1 = converter.getfinalLon();
      double lat1 = converter.getfinalLat();
      //site#2
      double lon2 = converter.getinitLon();
      double lat2 = converter.getinitLat();       



      //google Maps LatLng() method
      LatLng position = new LatLng(lat, lon);
      LatLng position1 = new LatLng(lat1, lon1);
      LatLng position2 = new LatLng(lat2, lon2);

    @Override
     protected void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      setContentView(R.layout.mapview);          

      mGoogleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
      mGoogleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapview)).getMap();

      //add marker
      //mGoogleMap.addMarker(new MarkerOptions().position(position).title("current")).showInfoWindow();
      mGoogleMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.site1)).position(position1).title("Site#1")).showInfoWindow();
      mGoogleMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.site2)).position(position2).title("Sitee#2")).showInfoWindow();

      //
      mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(position1, 10));
     }//ends onCreate
}//ends MapView
4

2 回答 2

1

mGoogleMap在初始化之前使用:

mGoogleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
mGoogleMap = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.mapview)).getMap();

您应该交换这些行。

于 2013-10-20T10:28:16.133 回答
1

setMapType()在你得到它的参考后打电话。资源

// Get a handle to the Map Fragment
GoogleMap map = ((SupportMapFragment)getSupportFragmentManager()
            .findFragmentById(R.id.map)).getMap();
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
于 2013-10-20T10:28:42.017 回答