【问题标题】:Android Studio: Unable to resolve Symbol 'requestLocationUpdates'Android Studio:无法解析符号“requestLocationUpdates”
【发布时间】:2013-07-17 17:58:41
【问题描述】:

使用基本 GPS 代码时,我遇到了新的 Android Studio 的问题 - 我的意思是它的预览软件到目前为止它已经足够可靠,可以全职使用。所以这是给我问题的代码:

import android.app.Activity;
import android.app.AlertDialog; 
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

public class WhereAmI extends Activity implements LocationListener{

TextView lat, lon, alt, status;
Context myContext = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.whereami);
    lat = (TextView)findViewById(R.id.latTxt);
    lon = (TextView)findViewById(R.id.lonTxt);
    alt = (TextView)findViewById(R.id.altTxt);
    status = (TextView)findViewById(R.id.gpsStatus);
}

/* Use the LocationManager class to obtain GPS locations */
LocationManager LM = (LocationManager) getSystemService(LOCATION_SERVICE);
//Gather GPS data at a certain time interval.
LM.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1f, myContext);
//Check to see if GPS is on
boolean isGPS = LM
        .isProviderEnabled(LocationManager.GPS_PROVIDER);

//If its off, request to turn it on.
if (isGPS == false)
{
    //Enable GPS pop-up notification.
    AlertDialog.Builder adb = new AlertDialog.Builder(myContext);

    // set title
    adb.setTitle("Enable GPS?");

    // set dialog message
    adb.setMessage("Enable GPS to get full function from the app.");
    adb.setCancelable(false);

    //Yes Button
    adb.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            startActivityForResult(
                    new Intent(
                            android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS),
                    0);
        }
    });

    //No Button
    adb.setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    });

    // create GPS Alert [Pop-up]
    AlertDialog alertDialog = adb.create();

    // show it
    alertDialog.show();
}
else
{
    //Added this so when the app gets refreshed it will show the true GPS info status.
    status.setText("GPS Status: Enabled");
}

@Override
public void onLocationChanged(Location location) {
    lat.setText("Latitude: " + location.getLatitude());
    lon.setText("Longitude: " + location.getLongitude());
    alt.setText("Altitude: " + location.getAltitude());
}

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {

}

@Override
public void onProviderEnabled(String s) {
    status.setText("GPS Status: Enabled");
}

@Override
public void onProviderDisabled(String s) {

}
}

由于某种原因,它不断出现无法解析 requestLocationUpdates 的符号。

还有其他人遇到过这个问题或知道解决方案吗?

【问题讨论】:

  • myContext 是什么对象?是Context 吗?
  • @ShobhitPuri 是 myContext 转换为 'Context myContext = this;'

标签: java android android-studio


【解决方案1】:

没有将Context 作为输入的requestLocationUpdates() 函数。以下是requestLocationUpdates() 函数允许的参数:

requestLocationUpdates(long minTime, float minDistance, Criteria criteria, PendingIntent intent)
requestLocationUpdates(long minTime, float minDistance, Criteria criteria, LocationListener listener, Looper looper)
requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)
requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener, Looper looper)
requestLocationUpdates(String provider, long minTime, float minDistance, PendingIntent intent)

参见LocationManager 页面。希望这可以帮助。

更新

将以下代码移入oncreate()

/* Use the LocationManager class to obtain GPS locations */
LocationManager LM = (LocationManager) getSystemService(LOCATION_SERVICE);
//Gather GPS data at a certain time interval.
LM.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1f, myContext);
//Check to see if GPS is on
boolean isGPS = LM
        .isProviderEnabled(LocationManager.GPS_PROVIDER);

//If its off, request to turn it on.
if (isGPS == false)
{
    //Enable GPS pop-up notification.
    AlertDialog.Builder adb = new AlertDialog.Builder(myContext);

    // set title
    adb.setTitle("Enable GPS?");

    // set dialog message
    adb.setMessage("Enable GPS to get full function from the app.");
    adb.setCancelable(false);

    //Yes Button
    adb.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            startActivityForResult(
                    new Intent(
                            android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS),
                    0);
        }
    });

    //No Button
    adb.setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    });

    // create GPS Alert [Pop-up]
    AlertDialog alertDialog = adb.create();

    // show it
    alertDialog.show();
}
else
{
    //Added this so when the app gets refreshed it will show the true GPS info status.
    status.setText("GPS Status: Enabled");
}

【讨论】:

  • 我做了一些更改,但在设置了正确的值后仍然说它没有被解决。
  • 只有这个功能会报错吗?尝试重建项目(从 Build 菜单)
  • 文件中显然存在一些问题。然而,这是主要的。
  • 你能把代码贴出来吗?这可能是由于某些错误的导入语句或其他原因。即使有一个错误,项目也不会构建。
  • 查看我的编辑。在 onCreate() 中移动那段代码。一旦您开始活动,那段代码就应该运行。所以它进入了 onCreate() 函数。
猜你喜欢
  • 2018-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-15
  • 1970-01-01
  • 2015-12-17
  • 2015-08-22
相关资源
最近更新 更多