【问题标题】:cannot find symbol class location [closed]找不到符号类位置[关闭]
【发布时间】:2021-07-26 11:15:14
【问题描述】:

这是我活动的 xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/showLocation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Location"
        android:textSize="24sp" />
    <Button
        android:id="@+id/btnGetLocation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Get Location" />
</LinearLayout> 

这是用于 MainActivity

 public class MainActivity extends AppCompatActivity {

    private static final int REQUEST_LOCATION = 1;
    private static final int ACCESS_COARSE_LOCATION =2 ;
    private static final int ACCESS_FINE_LOCATION =3 ;
    private static final int INTERNET =4 ;
    Button btnGetLocation;
    TextView showLocation;
    LocationManager locationManager;
    String latitude, longitude;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActivityCompat.requestPermissions( this,
                new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
        showLocation = findViewById(R.id.showLocation);
        btnGetLocation = findViewById(R.id.btnGetLocation);
       locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        btnGetLocation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
       //  location nManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);//error come here
                if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                    OnGPS();
                } else {
                    getLocation();
                }
            }
        });
    }
    private void OnGPS() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Enable GPS").setCancelable(false).setPositiveButton("Yes", new  DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
            }
        }).setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        final AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == ACCESS_COARSE_LOCATION) {
            if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(MainActivity.this, "ACCESS Permission Granted", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(MainActivity.this, "ACCESS Permission Denied", Toast.LENGTH_SHORT).show();
            }
        } else if (requestCode == ACCESS_FINE_LOCATION) {
            if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(MainActivity.this, "ACCESS Permission Granted", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(MainActivity.this, "ACCESS Permission Denied", Toast.LENGTH_SHORT).show();
            }
        } else if (requestCode == INTERNET) {
            if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(MainActivity.this, "INTERNET Permission Granted", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(MainActivity.this, "INTERNET  Permission Denied", Toast.LENGTH_SHORT).show();
            }
        }
    }

 private void getLocation() {
        if (ActivityCompat.checkSelfPermission(
                MainActivity.this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
                MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
        } else {
            Location locationGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if (locationGPS != null) {
                double lat = locationGPS.getLatitude();
                double longi = locationGPS.getLongitude();
                latitude = String.valueOf(lat);
                longitude = String.valueOf(longi);
                showLocation.setText("Your Location: " + "\n" + "Latitude: " + latitude + "\n" + "Longitude: " + longitude);
            } else {
                Toast.makeText(this, "Unable to find location.", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

当我运行我的应用程序时,我遇到了这样的错误找不到符号类位置(我在谷歌上搜索了解决方案,但尚未解决我的问题,请帮助我找出问题所在...我授予 INTERNET、FINE_LOCATION 等权限和清单中的 COARSE_LOCATION

【问题讨论】:

  • 也发布堆栈跟踪

标签: java android class android-intent gps


【解决方案1】:

只是错别字,改一下

location nManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

最好把它移到按钮点击上方

【讨论】:

  • 好的现在它会吐司找不到位置。我可以使用wifi或移动数据连接到互联网
  • 你没有 onRequestPermissionsResult 的覆盖方法,在运行时请求用户许可。添加它,然后它应该可以工作了。
  • 最后感谢您与我分享您的信息......
猜你喜欢
  • 2015-02-03
  • 2021-05-21
  • 2019-04-10
  • 2011-08-12
  • 1970-01-01
  • 2016-05-08
  • 1970-01-01
  • 1970-01-01
  • 2014-04-05
相关资源
最近更新 更多