0

Just started on Android Studio. i followed instructions to install the google map, tried it and works perfectly. Then I copied the files to my existing application, called the map activity, but crashes every time. Appreciate if someone can help me take a look and see where is wrong.

Below is the content_my.xml file with the onclick action to call the showLocation function:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:showIn="@layout/activity_my"
      app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <EditText android:id="@+id/edit_message"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/edit_message"/>

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/edit_message">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/button_send"
                android:onClick="sendMessage"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/button_get_location"
                android:onClick="showLocation" />

            <TextView android:id="@+id/get_coordinates"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"/>
            </LinearLayout>

    </RelativeLayout>  

Next, myActivity.java file

package com.example.richardlow.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MyActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "com.example.richardlow.myapplication.MESSAGE";
public void sendMessage(View view){
    Intent intent = new Intent(this,DisplayMessageActivity.class);
    EditText editText = (EditText)findViewById(R.id.edit_message);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE,message);
    startActivity(intent);
}

public void showLocation(View view){
    Intent intent = new Intent(this,MapsActivity.class);
    startActivity(intent);
}

public void getLocation(View view){
    Toast toast = new Toast(getApplicationContext());
    GPSTracker gps;
        gps = new GPSTracker(this);
    if(gps.canGetLocation()){

        double latitude = gps.getLatitude();
        double longitude = gps.getLongitude();

        String string;
        string="Your Location is - Latitude: " + latitude + " Longtitude: " + longitude;

        TextView text = (TextView) findViewById(R.id.get_coordinates);
        text.setText(string);
        // \n is for new line
        Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
    }else{
        // can't get location
        // GPS or Network is not enabled
        // Ask user to enable GPS/network in settings
        gps.showSettingsAlert();
    }

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

   FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_my, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

here is the MapsActivity file:

package com.example.richardlow.myapplication;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
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 MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    //LatLng sydney = new LatLng(-34, 151);
    LatLng sydney = new LatLng(22.2783503,114.1747504);
    mMap.addMarker(new MarkerOptions().position(sydney).title(""));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}

the manifest file:

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

    <activity
        android:name=".MyActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".DisplayMessageActivity"
        android:label="@string/title_activity_display_message"
        android:theme="@style/AppTheme.NoActionBar"/>
    <activity android:name=".MapsActivity"/>

</application>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
</manifest>

Appreciate it if someone could look at it and help point out the mistake.

Thank you in advance Richard

sorry, don't know how to reply, first time using this, post all logcat?

logcat:

com.example.richardlow.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.richardlow.myapplication/com.example.richardlow.myapplication.MapsActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
        at android.app.ActivityThread.access$700(ActivityThread.java:168)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5493)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1225)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1041)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:382)
        at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:361)
        at android.app.Activity.setContentView(Activity.java:1956)
        at com.example.richardlow.myapplication.MapsActivity.onCreate(MapsActivity.java:20)
        at android.app.Activity.performCreate(Activity.java:5372)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
        at android.app.ActivityThread.access$700(ActivityThread.java:168)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5493)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1225)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1041)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.RuntimeException: API key not found.  Check that <meta-data android:name="com.google.android.geo.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml
        at maps.db.r.a(Unknown Source)
        at maps.cz.c.a(Unknown Source)
        at com.google.android.gms.maps.internal.CreatorImpl.b(Unknown Source)
        at com.google.android.gms.maps.internal.CreatorImpl.newMapFragmentDelegate(Unknown Source)
        at com.google.android.gms.maps.internal.d$a.onTransact(:com.google.android.gms.alldynamite:62)
        at android.os.Binder.transact(Binder.java:347)
        at com.google.android.gms.maps.internal.ah.b(:com.google.android.gms:179)
        at com.google.android.gms.maps.internal.CreatorImpl.b(:com.google.android.gms:100)
        at com.google.android.gms.maps.internal.ag.onTransact(:com.google.android.gms:62)
        at android.os.Binder.transact(Binder.java:347)
        at com.google.android.gms.maps.internal.zzc$zza$zza.zzs(Unknown Source)
        at com.google.android.gms.maps.SupportMapFragment$zzb.zzzW(Unknown Source)
        at com.google.android.gms.maps.SupportMapFragment$zzb.zza(Unknown Source)
        at com.google.android.gms.dynamic.zza.zza(Unknown Source)
        at com.google.android.gms.dynamic.zza.onInflate(Unknown Source)
        at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
        at android.support.v4.app.Fragment.onInflate(Fragment.java:1142)
        at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2287)
        at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
        at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:357)
        at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
        at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:80)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:738)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:382)
        at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:361)
        at android.app.Activity.setContentView(Activity.java:1956)
        at com.example.richardlow.myapplication.MapsActivity.onCreate(MapsActivity.java:20)
        at android.app.Activity.performCreate(Activity.java:5372)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
        at android.app.ActivityThread.access$700(ActivityThread.java:168)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5493)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1225)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1041) 
Marv
  • 3,517
  • 2
  • 22
  • 47
Richard Low
  • 77
  • 1
  • 9
  • 1
    Post your `LogCat` with your errors. – Aspicas Mar 16 '16 at 09:17
  • Post the error message. – Dania Mar 16 '16 at 09:22
  • sorry, trying to work out how to post the logcat – Richard Low Mar 16 '16 at 09:26
  • Only the error part which appears in red color and states the exception type. – Dania Mar 16 '16 at 09:27
  • on the phone it just says "application has stopped" – Richard Low Mar 16 '16 at 09:28
  • did you insert the api-key – Neeraj Mar 16 '16 at 09:29
  • Not on the phone, it will not show the error there, it will appear in the logcat in Android Studio, usually at the bottom of the IDE interface. – Dania Mar 16 '16 at 09:30
  • yes, i did insert the key – Richard Low Mar 16 '16 at 09:30
  • Not on Phone. If you are working with Android Studio then, you can see the error log by clicking on Android Monitor tab located near bottom left corner – Chintan Soni Mar 16 '16 at 09:30
  • 3 more lines further up the logcat: – Richard Low Mar 16 '16 at 09:31
  • 03-16 17:28:29.158 31641-31641/com.example.richardlow.myapplication E/GMPM: GoogleService failed to initialize, status: 10, Missing an expected resource: 'R.string.google_app_id' for initializing Google services. Possible causes are missing google-services.json or com.google.gms.google-services gradle plugin. 03-16 17:28:29.158 31641-31641/com.example.richardlow.myapplication E/GMPM: Scheduler not set. Not logging error/warn. 03-16 17:28:29.188 31641-31664/com.example.richardlow.myapplication E/GMPM: Uploading is not possible. App measurement disabled – Richard Low Mar 16 '16 at 09:31
  • You are missing the Google Map API key – Chintan Soni Mar 16 '16 at 09:32
  • oops, forgot where i pasted the key.. but it shows in the manifest file – Richard Low Mar 16 '16 at 09:35
  • It's written in the logcat that API key is not found, make sure that you've added the key. – Dania Mar 16 '16 at 09:35
  • found it, it's at google_maps_api.xml – Richard Low Mar 16 '16 at 09:38
  • we just need one key, right, copied from my earlier test application with google map – Richard Low Mar 16 '16 at 09:39
  • It's better if you create a new key for this app in google console – Neeraj Mar 16 '16 at 09:47
  • Try to get the key as this link suggests and add it to your app instead of the one you already have https://developers.google.com/maps/documentation/android-api/start#step_4_get_a_google_maps_api_key – Dania Mar 16 '16 at 09:48
  • ok, thanks for your help, will try out a new key and let you guys know the outcome – Richard Low Mar 16 '16 at 10:05
  • [android.view.InflateException: Binary XML file line #1: Error inflating class fragment](http://stackoverflow.com/questions/19874882/android-view-inflateexception-binary-xml-file-error-inflating-class-fragment) – ELITE Mar 16 '16 at 10:30
  • I think we can only generate one key, but can add a package and fingerprint. Not sure how to do it, trying to get the fingerprint from the app without success – Richard Low Mar 17 '16 at 08:37
  • Hi, managed to generate another key, but still having the same error message, "key not found". what else can we do? – Richard Low Mar 17 '16 at 09:44

0 Answers0