申请突然停止了

时间:2015-04-16 15:48:07

标签: android

我的应用程序基于向用户显示当前位置。 所以在我的MainActivity.java中我放了一个locate按钮。  但是当我运行应用程序并按下定位按钮时,我的应用就会停止。

我的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

}
}

我的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>

我的清单文件

<?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>

之前它显示了空指针异常,现在是运行时异常

日志文件

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)

3 个答案:

答案 0 :(得分:1)

在您的清单文件的<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />元素中附加<application>

<?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>

答案 1 :(得分:1)

logcat正在说:

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" />

总是在logcat中看这个引起:,它应该向您显示足够的信息来查找和纠正您的应用程序的问题,就像在这种情况下一样。

    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" />

所以只要做它说的,把这个

<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> 

AndroidManifest.xml 文件中的<application>标记内。

更新:

正如我所说的那样,你应该寻找引起的,这里是

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"/>

见:

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"/>

现在只需将该权限添加到AndroidManifest:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

答案 2 :(得分:0)

中更改Fragment代码
android:name="com.google.android.gms.maps.MapFragment"

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

如果这不能解决,那么

在应用程序元素中添加元数据:Logcat清楚地显示错误。

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" />

喜欢这个:

<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" />
            ...
相关问题