When I say add place from the menu in Android Studio, it sometimes opens the map and sometimes the application closes without opening it

huangapple go评论77阅读模式
英文:

When I say add place from the menu in Android Studio, it sometimes opens the map and sometimes the application closes without opening it

问题

以下是翻译好的内容:

当我在Android Studio中从菜单中选择“添加地点”时,有时会打开地图,有时应用程序会在未打开地图的情况下关闭。当我说“添加地点”时,应用程序在未打开地图的情况下关闭,我会收到FATAL EXCEPTION: main错误。

Logcat错误:

2020-10-04 09:50:34.691 12343-12343/com.nuresrasoylu.myfavoriteplaces E/AndroidRuntime: 在writeCrashedAppName中,pkgName:com.nuresrasoylu.myfavoriteplaces
2020-10-04 09:50:34.693 12343-12343/com.nuresrasoylu.myfavoriteplaces E/AndroidRuntime: 致命异常:main
进程:com.nuresrasoylu.myfavoriteplaces,PID:12343
java.lang.AbstractMethodError: 抽象方法“void android.location.LocationListener.onStatusChanged(java.lang.String, int, android.os.Bundle)”
在android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:304)
在android.location.LocationManager$ListenerTransport.-wrap0(LocationManager.java)
在android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:242)
在android.os.Handler.dispatchMessage(Handler.java:102)
在android.os.Looper.loop(Looper.java:154)
在android.app.ActivityThread.main(ActivityThread.java:6138)
在java.lang.reflect.Method.invoke(Native Method)
在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783)

ManActivity.java

package com.nuresrasoylu.myfavoriteplaces;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.add_place,menu);

    return super.onCreateOptionsMenu(menu);

}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {

    if (item.getItemId() == R.id.add_place) {
        Intent intent = new Intent(MainActivity.this,MapsActivity.class);
        intent.putExtra("info","new");
        startActivity(intent);
    }

    return super.onOptionsItemSelected(item);
}

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

}

MapsActivity.java

package com.nuresrasoylu.myfavoriteplaces;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
LocationManager locationManager;
LocationListener locationListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}


@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(@NonNull Location location) {
            LatLng userLocation = new LatLng(location.getLatitude(),location.getLongitude());
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation,15));
        }
    };

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)  != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,new String[] {Manifest.permission.ACCESS_FINE_LOCATION},1);
    } else {
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);

        Location lastLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (lastLocation != null) {
            LatLng lastUserLocation = new LatLng(lastLocation.getLatitude(),lastLocation.getLongitude());
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(lastUserLocation,15));
        }
    }

}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (grantResults.length > 0 ) {
        if (requestCode == 1) {
            if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED ) {
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
            }
        }
    }

}

}

add_place.xml

```

英文:

When I say add place from the menu in Android Studio, it sometimes opens the map and sometimes the application closes without opening it. When I say add place, when the application is closed without opening the map, I get the FATAL EXCEPTION: main error.

Logcat Error:

22020-10-04 09:50:34.691 12343-12343/com.nuresrasoylu.myfavoriteplaces E/AndroidRuntime: in writeCrashedAppName, pkgName :com.nuresrasoylu.myfavoriteplaces
2020-10-04 09:50:34.693 12343-12343/com.nuresrasoylu.myfavoriteplaces E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.nuresrasoylu.myfavoriteplaces, PID: 12343
    java.lang.AbstractMethodError: abstract method "void android.location.LocationListener.onStatusChanged(java.lang.String, int, android.os.Bundle)"
        at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:304)
        at android.location.LocationManager$ListenerTransport.-wrap0(LocationManager.java)
        at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:242)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6138)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783)

ManActivity.java

package com.nuresrasoylu.myfavoriteplaces;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.add_place,menu);

        return super.onCreateOptionsMenu(menu);

    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        if (item.getItemId() == R.id.add_place) {
            Intent intent = new Intent(MainActivity.this,MapsActivity.class);
            intent.putExtra("info","new");
            startActivity(intent);
        }

        return super.onOptionsItemSelected(item);
    }

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


}

MapsActivity.java

package com.nuresrasoylu.myfavoriteplaces;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    LocationManager locationManager;
    LocationListener locationListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }


    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(@NonNull Location location) {
                LatLng userLocation = new LatLng(location.getLatitude(),location.getLongitude());
                mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation,15));
            }
        };

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)  != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,new String[] {Manifest.permission.ACCESS_FINE_LOCATION},1);
        } else {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);

            Location lastLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if (lastLocation != null) {
                LatLng lastUserLocation = new LatLng(lastLocation.getLatitude(),lastLocation.getLongitude());
                mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(lastUserLocation,15));
            }
        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (grantResults.length > 0 ) {
            if (requestCode == 1) {
                if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED ) {
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
                }
            }
        }

    }
}

add_place.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/add_place" android:title="Add Place"></item>
</menu>

答案1

得分: 2

包括以下代码:

public void onLocationChanged(@NonNull Location location){} :
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
英文:

Include the below code:

public void onLocationChanged(@NonNull Location location){} :
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

huangapple
  • 本文由 发表于 2020年10月4日 14:59:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64191944.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定