Search code examples
androidgoogle-mapsandroid-fragmentactivity

Setting new fragmentActivity over the mainActivity


in my project, im supposed to work with the google maps API, so i already have a class Maps.class, and on my MainActivity i should work with that Maps after an event, im not sure why isnt this working, but i really dont know how to make it happen. (the event call the Maps class with the fragmentactivity to show it on screen).

Maps class:

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.Marker;

public class Maps extends FragmentActivity {

GoogleMap googleMap;

Marker marker = null;

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

    // Getting Google Play availability status
    int status = GooglePlayServicesUtil
            .isGooglePlayServicesAvailable(getBaseContext());

    // Showing status
    if (status != ConnectionResult.SUCCESS) { // Google Play Services are
                                                // not available

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

    } else { // Google Play Services are available

        // Getting reference to the SupportMapFragment of activity_main.xml
        SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);

        // Getting GoogleMap object from the fragment
        googleMap = fm.getMap();
    }
}
}

MainActivity class:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.Marker;

public class MainActivity extends FragmentActivity {

GoogleMap googleMap;

Marker marker = null;

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

public void teste(View view) {
    Maps a = new Maps();
    MainActivity.this.finish();
}
}

EDIT: My new manifest xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mzubair.mapkey"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="8" />

<!-- android:name="YOUR_PACKAGE_NAME.permission.MAPS_RECEIVE" -->
<permission
    android:name="com.mzubair.mapkey.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.mzubair.mapkey.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission   android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<application
    android:icon="@drawable/common_signin_btn_icon_disabled_dark"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Maps"
        android:label="@string/app_name" >
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyCzeIVpUbL-HnxloaRbsOGxJbnAfP-tmpk" />
    </activity>
</application>

</manifest>

Solution

  • Maps a = new Maps();
    

    This is not how we start activities.

    Use this code instead:

    Intent intent = new Intent(this, Maps.class);
    startActivity(intent);
    

    Remember to add Maps to your AndroidManifest.xml.

    Also you probably don't want the same layout in both activities, so change this line:

    setContentView(R.layout.activity_main);
    

    in your Maps.java to something else.

    Have fun learning Android.