4

I am working with Android studio 2.1.2 . I have checked around and most of the questions either use older versions of Android studio and some old classes that don't apply to my situation.

From file > new Project > I used option Google Maps Activity. I have not changed any of the default code that are in it. Its XML has a Fragment. Now is there a way to bring in this activity as it is into another Activity by means of a Fragment? Basically I am building an app that shows a map in the middle portion of the screen. This middle portion I hope would be a Fragment. The bottom and top area has some components for the user to interact.

I am not trying to make the map come up in a new Activity but rather remain in the middle portion of a particualar Activity.

Or do I just add new UI components to the default Google Maps Activity.

At first I had an idea of creating a Fragment(with its own .java file, XML Layout)then placing the map code in there and taking it over to the Activity where I want it. What would be the best way to get this done? I appreciate any help.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
user3650467
  • 173
  • 1
  • 1
  • 9
  • You used the standard Google Map Activity template... Feel free to modify however you want. Or start from scratch. What exactly is the problem you are having? – OneCricketeer Aug 10 '16 at 16:15
  • My issue is I dont want to start from scratch since I already have a working Activity that just needs a map in the middle, its the main Activity. I also want to implement lifecycle callbacks in there. I later created a Google map Activity hoping to bring it into place. – user3650467 Aug 10 '16 at 16:40
  • I didn't mean start your app from scratch, I meant, create a blank XML and Java file, then start loading Google Maps pieces into it. – OneCricketeer Aug 10 '16 at 16:40
  • Ok sounds like a plan. Can I put in Google Maps Pieces into a Fragment then bring it in? I know that Activities extend a different class from Fragments. What I'm asking is can a Fragment implement the OnMapReadyCallback interface without problems? – user3650467 Aug 10 '16 at 16:47
  • Fragments are simply contained within Activities. Nothing too special there. I do believe the callback should work fine. I haven't personally used the Maps API, but I know there is a [`SupportMapFragment`](https://developers.google.com/android/reference/com/google/android/gms/maps/SupportMapFragment) class that sounds like what you want. – OneCricketeer Aug 10 '16 at 16:50
  • Ok I will try it out and see if it suits my current workflow.. – user3650467 Aug 10 '16 at 18:54
  • I ended up using the existing Activity that I had in place. set up the project successfully, but now the app crashes. I am testing on real device, Android 4.4.2. I dont get any errors in Android Studio. It says Unfortunately appProj has stopped. – user3650467 Aug 10 '16 at 21:06
  • There's a great post here about the many reason why that could be and what you can do to solve it. http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – OneCricketeer Aug 10 '16 at 21:36
  • Thank you I have taking a great amount of time to go over the resource. My issue now is I am trying to find out what could be the cause of the crash but logcat is outputing so much info that I cant even read anything. I tried viewing the ouput with the Android Device Manager, is there a way to pause the amount of info coming into logcat? I just tried adding a filter to show only info about myapp but still nothing. I am determined to know what part of the App is causing the crash. Do I need to wrap the mapfragment code around a try-catch block? – user3650467 Aug 11 '16 at 10:01
  • That depends what the error is. Look for the words "Caused by", then google the error reason. – OneCricketeer Aug 11 '16 at 12:18
  • I have been able to get info from Android Device Monitor. I have traced the error to a line inside my main Activity file. Caused by: java.lang.NullPointerException at com.example.myApp.MainActivity.onCreate(MainActivity.java:23). Line 23 code is mapFragment.getMapAsync(this); I am working with the example from https://developers.google.com/maps/documentation/android-api/start#the_maps_activity_java_file . Any ideas on how to fix this? – user3650467 Aug 11 '16 at 13:17
  • I could point you at "What is a NullPointerException, and how do I fix it", or I could tell you that `mapFragment` has not been initialized... Like I said earlier, I haven't used that library, but I can't see your code, and I trust your ability to understand standard Java errors – OneCricketeer Aug 11 '16 at 13:20
  • Like I pointed out I am trying out code from official Google Maps documentation. Yes mapFragment has been initialised. I will update my post to include my code – user3650467 Aug 11 '16 at 13:26
  • If it is null, then `findFragmentById` couldn't find the Fragment. Maybe you used the wrong ID or the wrong content view of the Activity – OneCricketeer Aug 11 '16 at 13:33

1 Answers1

12

I have realised that either way one can arrive at the same result, a map inside an Activity, alongside other UI Components. Whether Google Maps Activity is used or if an Empty Activity is created and map code implemented in it. My code is included below. I chose an Empty Activity (Android 2.1.2) and then brought in the map code into the .java file and placed the map fragment with tag in the XML Layout of the activity.

The error was that I had used

android:name="com.google.android.gms.maps.MapFragment"

in the XML Layout but declared and initialised mapFragment(the object instance) with SupportMapFragment. The mix up came while copying and pasting code from the Google Documentation. As I was trying to update my post here I spotted the error. The correct thing to do is if you are using MapFragment(API level 12 and over) use it all through. If you decide to use SupportMapFragment(below API level 12) use SupportMapFragment all through.

The code below is the correction. Just in case anyone has the same issue.

XML Layout (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.myapp.MainActivity">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/teachers_link"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/parent_link"
            android:layout_marginLeft="20dp" />

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="340dp"
        android:orientation="vertical">
        <fragment xmlns:android="http://schemas.android.com/apk/res/android"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="135dp"
        android:layout_marginTop="12dp"
        android:orientation="vertical">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/search_term" />
        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:id="@+id/select_level"
            android:entries="@array/select_level"></Spinner>
        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/select_course"
            android:entries="@array/select_course"></Spinner>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn_search" />
    </LinearLayout>


</LinearLayout>

</ScrollView>

</LinearLayout>

.java file (MainActivity.java)

package name;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


public class MainActivity extends AppCompatActivity implements OnMapReadyCallback{

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

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}


@Override
public void onMapReady(GoogleMap map) {
    map.addMarker(new MarkerOptions()
            .position(new LatLng(0, 0))
            .title("Marker"));
}

}
user3650467
  • 173
  • 1
  • 1
  • 9