Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java I';我正在为我的应用程序和我';我想,从现在起,我已经被困了五天多了,很简单';问题_Java_Android_Google Maps_Nullpointerexception - Fatal编程技术网

Java I';我正在为我的应用程序和我';我想,从现在起,我已经被困了五天多了,很简单';问题

Java I';我正在为我的应用程序和我';我想,从现在起,我已经被困了五天多了,很简单';问题,java,android,google-maps,nullpointerexception,Java,Android,Google Maps,Nullpointerexception,我对Android Studio和Java语言都是全新的,我正在尝试构建一个基于谷歌地图的应用程序。我将感谢详细的解决方案 我尝试了onCreateView()和getChildFragmentManager()。但既然我是新手,我可能做错了 我认为每次我的应用程序崩溃的主要问题是 方法调用“getmapasync”可能会产生“nullpointerexception” MapsActivity.java package com.example.silentium50; 导入androidx.a

我对Android Studio和Java语言都是全新的,我正在尝试构建一个基于谷歌地图的应用程序。我将感谢详细的解决方案

我尝试了onCreateView()和getChildFragmentManager()。但既然我是新手,我可能做错了

我认为每次我的应用程序崩溃的主要问题是

方法调用“getmapasync”可能会产生“nullpointerexception”

MapsActivity.java
package com.example.silentium50;
导入androidx.annotation.RequiresApi;
导入androidx.core.app.ActivityCompat;
导入androidx.fragment.app.FragmentActivity;
导入android.Manifest;
导入android.content.pm.PackageManager;
导入android.location.Address;
导入android.location.Geocoder;
导入android.location.location;
导入android.location.LocationListener;
导入android.location.LocationManager;
导入android.os.Build;
导入android.os.Bundle;
导入com.google.android.gms.maps.CameraUpdateFactory;
导入com.google.android.gms.maps.GoogleMap;
导入com.google.android.gms.maps.OnMapReadyCallback;
导入com.google.android.gms.maps.SupportMapFragment;
导入com.google.android.gms.maps.model.LatLng;
导入com.google.android.gms.maps.model.MarkerOptions;
导入java.io.IOException;
导入java.util.List;
类MapsActivity扩展了FragmentActivity在MapReadyCallback上的实现{
公共谷歌地图;
地点经理地点经理;
@RequiresApi(api=Build.VERSION\u CODES.M)
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_映射);
//获取SupportMapFragment,并在地图准备好使用时收到通知。
SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
getMapAsync(这个);
locationManager=(locationManager)getSystemService(位置服务);
if(ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u FINE\u LOCATION)!=PackageManager.permission\u已授予和&checkSelfPermission(Manifest.permission.ACCESS\u rough\u LOCATION)!=PackageManager.permission\u已授予){
考虑到呼叫
//活动#请求权限
//在此处请求缺少的权限,然后覆盖
//public void onRequestPermissionsResult(int-requestCode,字符串[]权限,
//int[]格兰特结果)
//处理用户授予权限的情况。请参阅文档
//对于活动#请请求权限以获取更多详细信息。
返回;
}
//检查网络提供程序是否已启用
if(locationManager.isProviderEnabled(locationManager.NETWORK_PROVIDER)){
locationManager.RequestLocationUpdate(locationManager.NETWORK_提供程序,1,1,新LocationListener(){
@凌驾
已更改位置上的公共无效(位置){
//获得纬度
双纬度=location.getLatitude();
//得到经度
double longitude=location.getLongitude();
//实例化这个类,LatLng
LatLng LatLng=新LatLng(纬度、经度);
//实例化类Geocoder
Geocoder Geocoder=新的Geocoder(getApplicationContext());
试一试{
列表地址列表=地理编码器.getFromLocation(纬度,经度,1);
String str=adressList.get(0.getLocality()+“,”;
str+=addresslist.get(0.getCountryName();
mMap.addMarker(新的MarkerOptions().position(latLng).title(str));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,30.5f));
}捕获(IOE异常){
e、 printStackTrace();
}
}
@凌驾
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){
}
@凌驾
公共无效onProviderEnabled(字符串提供程序){
}
@凌驾
公共无效onProviderDisabled(字符串提供程序){
}
});
}
else if(locationManager.isProviderEnabled(locationManager.GPS\U提供程序)){
locationManager.RequestLocationUpdate((locationManager.GPS_提供程序),1,1,新LocationListener(){
@凌驾
已更改位置上的公共无效(位置){
//获得纬度
双纬度=location.getLatitude();
//得到经度
double longitude=location.getLongitude();
//实例化这个类,LatLng
LatLng LatLng=新LatLng(纬度、经度);
//实例化类Geocoder
Geocoder Geocoder=新的Geocoder(getApplicationContext());
试一试{
列表地址列表=地理编码器.getFromLocation(纬度,经度,1);
String str=adressList.get(0.getLocality()+“,”;
str+=addresslist.get(0.getCountryName();
mMap.addMarker(新的MarkerOptions().position(latLng).title(str));
mMap.moveCamera(CameraUpdateFactory.n
package com.example.silentium50;


import androidx.annotation.RequiresApi;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.FragmentActivity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
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;

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

class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    public GoogleMap mMap;

    LocationManager locationManager;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public 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);
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    Activity#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for Activity#requestPermissions for more details.
            return;
        }
        // check if the network provider is enabled
            if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 1, new LocationListener() {
                    @Override
                    public void onLocationChanged(Location location) {
                        //get the latitude
                        double latitude = location.getLatitude();
                        //get the longitude
                        double longitude = location.getLongitude();
                        //instantiate the class, LatLng
                        LatLng latLng = new LatLng(latitude, longitude);
                        //Instantiate the class, Geocoder
                        Geocoder geocoder = new Geocoder(getApplicationContext());
                        try {
                            List<Address> adressList = geocoder.getFromLocation(latitude, longitude, 1);
                            String str = adressList.get(0).getLocality()+",";
                            str += adressList.get(0).getCountryName();
                            mMap.addMarker(new MarkerOptions().position(latLng).title(str));
                            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 30.5f));
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

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

                    }

                    @Override
                    public void onProviderEnabled(String provider) {

                    }

                    @Override
                    public void onProviderDisabled(String provider) {

                    }
                });
            }
            else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                locationManager.requestLocationUpdates((LocationManager.GPS_PROVIDER), 1, 1, new LocationListener() {
                    @Override
                    public void onLocationChanged(Location location) {
                        //get the latitude
                        double latitude = location.getLatitude();
                        //get the longitude
                        double longitude = location.getLongitude();
                        //instantiate the class, LatLng
                        LatLng latLng = new LatLng(latitude, longitude);
                        //Instantiate the class, Geocoder
                        Geocoder geocoder = new Geocoder(getApplicationContext());
                        try {
                            List<Address> adressList = geocoder.getFromLocation(latitude, longitude, 1);
                            String str = adressList.get(0).getLocality()+",";
                            str += adressList.get(0).getCountryName();
                            mMap.addMarker(new MarkerOptions().position(latLng).title(str));
                            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10f));
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }

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

                    }

                    @Override
                    public void onProviderEnabled(String provider) {

                    }

                    @Override
                    public void onProviderDisabled(String provider) {

                    }
                });
            }
        }





    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        //LatLng sydney = new LatLng(-34, 151);
        //mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        //mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 30.5f));
    }
}



<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MapsActivity" />



Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/art: Not late-enabling -Xcheck:jni (already on)
W/art: Unexpected CPU variant for X86 using defaults: x86
W/System: ClassLoader referenced unknown path: /data/app/com.example.silentium50-1/lib/x86
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.silentium50, PID: 7103
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.silentium50/com.example.silentium50.MapsActivity}: java.lang.IllegalAccessException: java.lang.Class<com.example.silentium50.MapsActivity> is not accessible from java.lang.Class<android.app.Instrumentation>
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
     Caused by: java.lang.IllegalAccessException: java.lang.Class<com.example.silentium50.MapsActivity> is not accessible from java.lang.Class<android.app.Instrumentation>
        at java.lang.Class.newInstance(Native Method)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6077) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.silentium50">

    <!--
         The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but you must specify either coarse or fine
         location permissions for the 'MyLocation' functionality.
    -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <!--
             The API key for Google Maps-based APIs is defined as a string resource.
             (See the file "res/values/google_maps_api.xml").
             Note that the API key is linked to the encryption key used to sign the APK.
             You need a different API key for each encryption key, including the release key that is used to
             sign the APK for publishing.
             You can define the keys for the debug and release targets in src/debug/ and src/release/.
        -->
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />

        <activity
            android:name=".MapsActivity"
            android:label="@string/title_activity_maps">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>