android – java.lang.SecurityException“gps”位置提供程序需要ACCESS_FINE_LOCATION权限

所以这是我的MyLocationListener类
package com.example.gpslocater;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class MyLocationListener implements LocationListener {

        public TextView mLocationTextView;
    Context mContent;
    public MyLocationListener(Context context) {
        mContent = context;
    }
    @Override
    public void onLocationChanged(Location location) {

        location.getLatitude();
        location.getLongitude();

        String Text = "My current location is :" + 
        "Latitude = " + location.getLatitude() +
        " Longitude = " + location.getLongitude();
        //figure out a way to make it display through a text view
        mLocationTextView.setText(Text);
    }

    @Override
    public void onProviderdisabled(String provider) {
        Toast.makeText(mContent,"Gps disabled",Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onProviderEnabled(String provider) {
        Toast.makeText(mContent,"Gps Enabled",Toast.LENGTH_SHORT);


    }

    @Override
    public void onStatusChanged(String provider,int status,Bundle extras) {
        // Todo Auto-generated method stub

    }

}

这是我的GPS活动课程

public class GPSActivity extends Activity {
    private Button mGPSButton;
    private TextView mLatitudeTextView;
    private TextView mLongitudeTextView;
    private LocationManager mLocationManager;
    private LocationListener mLocationListener;

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

        mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        mLocationListener = new MyLocationListener(null);
        mGPSButton = (Button)findViewById(R.id.press_button);
        mGPSButton.setonClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // Todo Auto-generated method stub
                mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,mLocationListener);
            }
        });
    }


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

}

现在,当我运行它可以工作,但按下按钮后程序停止响应有很多错误消息

有一条错误消息说java.lang.SecurityException“gps”位置提供程序需要ACCESS_FINE_LOCATION权限,我是否必须添加,如果是这样,我在哪里添加它?我认为这是我的程序崩溃的原因.

解决方法

从提供的logcat有意义.
在清单中添加内容
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...