没有找到适合 requestLocationUpdates 的方法

2023-12-11

这是我的 MainActivity.java:

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;


public class MainActivity extends Activity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        LocationListener locationListener = new GetCurrentLocation();
        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 5000, 10, locationListener);
}

这是我的 GetCurrentLocation.java:

import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.google.android.gms.location.LocationListener;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class GetCurrentLocation extends Activity implements LocationListener {
    String longitude;
    String latitude;
    String cityName;

    public void onLocationChanged(Location loc) {
        //editLocation.setText("");
        //pb.setVisibility(View.INVISIBLE);
        Toast.makeText(
                getBaseContext(),
                "Location changed: Lat: " + loc.getLatitude() + " Lng: "
                        + loc.getLongitude(), Toast.LENGTH_SHORT).show();
        longitude = "Longitude: " + loc.getLongitude();
        //Log.v(TAG, longitude);
        latitude = "Latitude: " + loc.getLatitude();
        //Log.v(TAG, latitude);

        /*------- To get city name from coordinates -------- */
        //String cityName = null;
        Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
        List<Address> addresses;
        try {
            addresses = gcd.getFromLocation(loc.getLatitude(),
                    loc.getLongitude(), 1);
            if (addresses.size() > 0)
                System.out.println(addresses.get(0).getLocality());
            cityName = addresses.get(0).getLocality();
        }
        catch (IOException e) {
            e.printStackTrace();
        }

        //editLocation.setText(s);
    }

    //@Override
    public void onProviderDisabled(String provider) {}

    //@Override
    public void onProviderEnabled(String provider) {}

    //@Override
    public void onStatusChanged(String provider, int status, Bundle extras) {}


}

我得到的错误是:

Error:(40, 24) error: no suitable method found for requestLocationUpdates(String,int,int,com.google.android.gms.location.LocationListener)
method LocationManager.requestLocationUpdates(long,float,Criteria,PendingIntent) is not applicable
(actual argument String cannot be converted to long by method invocation conversion)
method LocationManager.requestLocationUpdates(String,long,float,PendingIntent) is not applicable
(actual argument com.google.android.gms.location.LocationListener cannot be converted to PendingIntent by method invocation conversion)
method LocationManager.requestLocationUpdates(long,float,Criteria,android.location.LocationListener,Looper) is not applicable
(actual and formal argument lists differ in length)
method LocationManager.requestLocationUpdates(String,long,float,android.location.LocationListener,Looper) is not applicable
(actual and formal argument lists differ in length)
method LocationManager.requestLocationUpdates(String,long,float,android.location.LocationListener) is not applicable
(actual argument com.google.android.gms.location.LocationListener cannot be converted to android.location.LocationListener by method invocation conversion)

我完全不知道应该修复哪部分代码,我尝试用谷歌搜索,但找不到相关的解决方案。


你用错了LocationListener班级。你需要替换这个:

import com.google.android.gms.location.LocationListener;

有了这个:

import android.location.LocationListener;

The com.google.android.gms.location东西是用来与FusedLocationProviderApi这是 Google Play 服务的一部分。

标准的 Android 位置信息位于android.location.

另外,你还有GetCurrentLocation extends Activity。但这并不是真正的Activity。您需要删除extends Activity条款。这会导致问题,因为你正在调用getBaseContext()在该类的某些方法中。要解决这个问题,请使用以下构造函数GetCurrentLocation take a Context参数并将其保存在私有成员变量中。然后在需要时使用它Context而不是打电话getBaseContext().

In MainActivity,当你创建一个新的GetCurrentLocation, 你可以这样做:

LocationListener locationListener = new GetCurrentLocation(this);

passing this as the Context范围 (Activity延伸Context).

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

没有找到适合 requestLocationUpdates 的方法 的相关文章

随机推荐

  • 登录后的 C# .NET Cookie 处理

    我花了一些时间研究这个问题 但仍然无法弄清楚 它看起来很简单 所以我觉得自己像个白痴问这个问题 但经过一段时间的研究后 我似乎无法掌握它的窍门 我需要以编程方式登录该网站 https wholesale frontiercoop com 存
  • 如何在 Spark 中跳过 RDD 中的多行标头

    我的第一个 RDD 中的数据就像 1253 545553 12344896 1 2 1 1 43 2 1 46 1 1 53 2 现在前 3 个整数是我需要广播的一些计数器 之后所有行都具有相同的格式 例如 1 2 1 1 43 2 在函数
  • 平板电脑的 Genymotion 屏幕方向问题

    我正在使用 Genymotion 我为 sw800dp 平板电脑和 sw600dp 创建了布局 我希望我的应用程序仅用于肖像 所以我做了什么
  • 通过 XMLHttpRequest 从 HTML5 文件系统上传文件

    尝试上传一些存储在 Google Chrome 文件系统中的图像 但我无法上传图像 知道怎么做吗 服务器收到一个空数组 posttest php 的代码只是 print r POST var xhr new XMLHttpRequest x
  • 无法在 Android 中使用trigger.io 和 parse.com 接收推送通知

    我刚刚开始开发适用于 Android 和 iPhone 的应用程序trigger io and parse com 现在我一直在尝试向应用程序添加推送通知 我从控制面板发送测试推送parse com但我正在测试的 Android 手机上没有
  • XML 文件上的 Regex::captures_iter 比预期慢

    我的第一个小型 Rust 项目涉及在大型 XML 文件上运行正则表达式 extern crate regex use regex Regex use std fs File use std io Read fn main let filen
  • YouTube 视频结束时发生的事件

    我有简单的 html 代码 可以在单击图像后播放 YouTube 视频 div style display none div
  • 是否可以将成员初始化推迟到构造函数主体?

    我有一个类 其中一个对象作为成员 但没有默认构造函数 我想在构造函数中初始化这个成员 但似乎在 C 中我不能这样做 这是课程 include
  • 如何通过group_concat获取的id删除行

    我想从表中删除一些行 首先 我尝试使用子查询 但是当在子查询中使用相同的表时 我无法从表中删除数据 我尝试用另一种方式做到这一点 set list id remove select group concat mra media id fro
  • 从多值列查询不同值

    我正在尝试查询 标签 列中的所有唯一值 标签列中的每一行都可以包含多个值 因此 在不被迫进行规范化的情况下 如何查询多值列 示例行 Networking Professionalism Time Management Communicati
  • 错误 2002 (HY000): 无法通过套接字 '/tmp/mysql.sock' 连接到本地 MySQL 服务器 (2)

    有人可以帮忙吗 因为我花了一整天的时间试图解决这个问题 我安装了最新的 XAMPP 现在我无法从终端连接到 mysql 我检查了我的 profile 文件 路径似乎没问题 有谁知道发生了什么事以及解决方案是什么 PATH PATH Appl
  • Java 字符串与数组的区别

    我目前很无聊 正在做一些 Java 练习测试 因为我已经用 Java 编程很长时间了 在提出某个问题之后 我现在想知道以下之间的区别 String test1 A B C String test2 new String A B C Stri
  • Windows 上的程序员编辑器用于印度语编辑

    我们将构建一些 J2ME 应用程序和 Java Rails Web 应用程序 它们将具有卡纳达语 一种南印度语言 对于那些对印度不太了解的人来说 UI 这些应用程序的用户界面和数据都将采用卡纳达语 因此 我们需要在源代码中编写包含一些这些语
  • 如何在 C++ 中将 RSA* 打印为字符串?

    如何在 C 中将 RSA 正确打印为字符串 我在用OpenSSL 似乎没有 c str 方法对吧 include
  • JQuery + SVG 对象:正确捕获点击事件

    好的 我正在尝试捕获菜单项上的点击事件 这是我的html
  • 捆绑多个工件进行部署?

    这是在此基础上进行的后续answer 我有一个看起来像的结构 ls service target classes lib maven status surefire reports classes 1194128992 timestamp
  • 在 Ruby on Rails 中链接模型

    我正在构建一个 Rails 应用程序 并且我想要连接两个模型 有一个称为 用户 的模型来处理用户身份验证 例如用户名和密码数据 然后有一个称为 个人资料 的模型 其中包含一个人的位置 描述等 我想链接这些模型 以便每个用户都有一个配置文件
  • 重复捕获组PCRE

    无法理解为什么这个正则表达式 regex101 a z0 9A Z g 捕获所有输入 而这个 regex101 a z0 9A Z g 仅捕获 Func 输入字符串是 Func param1 param2 param32 param54 p
  • java.net 包 - 覆盖 UDP 传输

    我需要重写 DatagramSocket 传输的默认实现 即 PlainDatagramSocketImpl 我发现 java 提供了一种通过 DatagramSocketImplFactory 类来执行此操作的方法 然而 我的要求是在非常
  • 没有找到适合 requestLocationUpdates 的方法

    这是我的 MainActivity java import android app Activity import android app AlertDialog import android app ProgressDialog impo