Reputation: 21
My application is based on displaying the current location to the user. so in my MainActivity.java i put a locate button. but when i run the app and press locate button, my app stops.
My Gps.java
package com.shalu.alzcare;
import android.app.Dialog;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
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.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
public class Gps extends FragmentActivity implements LocationListener {
GoogleMap googleMap;
Button sendBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gps);
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available
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 = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null){
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 20000, 0, this);
}
sendBtn = (Button) findViewById(R.id.button1);
sendBtn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view) {
sendSMSMessage();
}
});
}
@Override
public void onLocationChanged(Location location) {
TextView tvLocation = (TextView) findViewById(R.id.textView1);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
tvLocation.setText("Latitude:" +latitude+ ", Longitude:"+longitude);
}
protected void sendSMSMessage() {
Log.i("Send SMS", "");
String con1="9876543210";
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(con1, null, "I am Out please call me!!", null, null);
Toast.makeText(getApplicationContext(), "SMS sent.",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again.",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
}
My Activity_gps.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<fragment
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/button1" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Help" />
</RelativeLayout>
my Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.shalu.alzcare"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="21" />
<permission android:name="com.shalu.alzcare.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.shalu.alzcare.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-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.SEND_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<uses-library android:name="com.google.android.maps" />
<activity
android:name=".Login"
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=".Contacts"
android:label="@string/title_activity_contacts"
android:parentActivityName=".Login" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.shalu.alzcare.Login" />
</activity>
<activity
android:name=".Reminder"
android:label="@string/title_activity_reminder"
android:parentActivityName=".Contacts" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.shalu.alzcare.Contacts" />
</activity>
<activity
android:name=".Details"
android:label="@string/title_activity_details"
android:parentActivityName=".Reminder" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.shalu.alzcare.Reminder" />
</activity>
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
</activity>
<activity
android:name=".Gps"
android:label="@string/title_activity_gps"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.shalu.alzcare.MainActivity" />
</activity>
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<receiver android:name=".DeviceBootReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="My API" />
</application>
</manifest>
previously it was showing me null pointer exception and now runtime exception
Log File
04-17 20:15:36.956: E/AndroidRuntime(24437): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shalu.alzcare/com.shalu.alzcare.Gps}: android.view.InflateException: Binary XML file line #14: Error inflating class fragment
04-17 20:15:36.956: E/AndroidRuntime(24437): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2436)
04-17 20:15:36.956: E/AndroidRuntime(24437): Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class fragment
04-17 20:15:36.956: E/AndroidRuntime(24437): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:372)
04-17 20:15:36.956: E/AndroidRuntime(24437): at android.app.Activity.setContentView(Activity.java:2046)
04-17 20:15:36.956: E/AndroidRuntime(24437): at com.shalu.alzcare.Gps.onCreate(Gps.java:30)
04-17 20:15:36.956: E/AndroidRuntime(24437): Caused by: java.lang.SecurityException: The Maps API requires the additional following permissions to be set in the AndroidManifest.xml to ensure a correct behavior:
04-17 20:15:36.956: E/AndroidRuntime(24437): <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
04-17 20:15:36.956: E/AndroidRuntime(24437): at com.google.android.gms.maps.internal.IMapFragmentDelegate$zza$zza.onCreateView(Unknown Source)
04-17 20:15:36.956: E/AndroidRuntime(24437): at com.google.android.gms.maps.SupportMapFragment$zza.onCreateView(Unknown Source)
04-17 20:15:36.956: E/AndroidRuntime(24437): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
Upvotes: 2
Views: 115
Reputation: 2363
The logcat is saying it:
04-16 21:04:18.033: E/AndroidRuntime(18078): Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 7095000 but found 0. You must have the following declaration within the <application> element: <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
Always look fot this Caused by: in the logcat, it should show you a enough information to find and correct a problem with your app, like in this case.
The meta-data tag in your app's AndroidManifest.xml does not have the right value.
Expected 7095000 but found 0.
You must have the following declaration within the <application> element: <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
So just do what it says, put this
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
inside the <application>
tag in your AndroidManifest.xml file.
UPDATE:
As I said, you shuld look for this Caused by, here it is
04-17 20:15:36.956: E/AndroidRuntime(24437): Caused by: java.lang.SecurityException: The Maps API requires the additional following permissions to be set in the AndroidManifest.xml to ensure a correct behavior:
04-17 20:15:36.956: E/AndroidRuntime(24437): <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
See this:
The Maps API requires the additional following permissions to be set in the AndroidManifest.xml to ensure a correct behavior:<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
And now just add that permission to your AndroidManifest:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Upvotes: 1
Reputation: 2129
Change in your Fragment tag, from
android:name="com.google.android.gms.maps.MapFragment"
to
android:name="com.google.android.gms.maps.SupportMapFragment"
If this does not solve, then
Add meta-data within application element: Logcat clearly shows the error.
04-16 21:04:18.033: E/AndroidRuntime(18078): Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 7095000 but found 0. You must have the following declaration within the <application> element: <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
Like This:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
...
Upvotes: 0
Reputation: 482
Append <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
in your manifest file's <application>
element.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.shalu.alzcare"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="21" />
<permission android:name="com.shalu.alzcare.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.shalu.alzcare.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-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.SEND_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
<uses-library android:name="com.google.android.maps" />
<activity
android:name=".Login"
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=".Contacts"
android:label="@string/title_activity_contacts"
android:parentActivityName=".Login" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.shalu.alzcare.Login" />
</activity>
<activity
android:name=".Reminder"
android:label="@string/title_activity_reminder"
android:parentActivityName=".Contacts" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.shalu.alzcare.Contacts" />
</activity>
<activity
android:name=".Details"
android:label="@string/title_activity_details"
android:parentActivityName=".Reminder" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.shalu.alzcare.Reminder" />
</activity>
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
</activity>
<activity
android:name=".Gps"
android:label="@string/title_activity_gps"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.shalu.alzcare.MainActivity" />
</activity>
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<receiver android:name=".DeviceBootReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="My_Api" />
</application>
</manifest>
Upvotes: 1