If you run a map app using Kakao api, it will be forced to end.

Asked 2 years ago, Updated 2 years ago, 73 views

/////////////////////Logcat

06-10 16:18:29.638 5991-5991/com.example.bencall E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.bencall, PID: 5991 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bencall/com.example.bencall.MapsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void net.daum.mf.map.api.MapView.setCurrentLocationEventListener(net.daum.mf.map.api.MapView$CurrentLocationEventListener)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread.access$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void net.daum.mf.map.api.MapView.setCurrentLocationEventListener(net.daum.mf.map.api.MapView$CurrentLocationEventListener)' on a null object reference at com.example.bencall.MapsActivity.onCreate(MapsActivity.java:44) at android.app.Activity.performCreate(Activity.java:5937) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)  at android.app.ActivityThread.access$800(ActivityThread.java:144)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5221)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////MapsActivity.java//////////////////////////////////////////////////////////

public class MapsActivity extends AppCompatActivity implements MapView.CurrentLocationEventListener, MapReverseGeoCoder.ReverseGeoCodingResultListener {

private static final String LOG_TAG = "MainActivity";

private MapView mMapView;


private static final int GPS_ENABLE_REQUEST_CODE = 2001;
private static final int PERMISSIONS_REQUEST_CODE = 100;
String[] REQUIRED_PERMISSIONS  = {Manifest.permission.ACCESS_FINE_LOCATION};




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    mMapView = (MapView) findViewById(R.id.map_view);
    //mMapView.setDaumMapApiKey(MapApiConst.DAUM_MAPS_ANDROID_APP_API_KEY);
    mMapView.setCurrentLocationEventListener(this);

    if (!checkLocationServicesStatus()) {

        showDialogForLocationServiceSetting();
    }else {

        checkRunTimePermission();
    }

}

@Override
protected void onDestroy() {
    super.onDestroy();
    mMapView.setCurrentLocationTrackingMode(MapView.CurrentLocationTrackingMode.TrackingModeOff);
    mMapView.setShowCurrentLocationMarker(false);
}

@Override
public void onCurrentLocationUpdate(MapView mapView, MapPoint currentLocation, float accuracyInMeters) {
    MapPoint.GeoCoordinate mapPointGeo = currentLocation.getMapPointGeoCoord();
    Log.i(LOG_TAG, String.format("MapView onCurrentLocationUpdate (%f,%f) accuracy (%f)", mapPointGeo.latitude, mapPointGeo.longitude, accuracyInMeters));
}


@Override
public void onCurrentLocationDeviceHeadingUpdate(MapView mapView, float v) {

}

@Override
public void onCurrentLocationUpdateFailed(MapView mapView) {

}

@Override
public void onCurrentLocationUpdateCancelled(MapView mapView) {

}

@Override
public void onReverseGeoCoderFoundAddress(MapReverseGeoCoder mapReverseGeoCoder, String s) {
    mapReverseGeoCoder.toString();
    onFinishReverseGeoCoding(s);
}

@Override
public void onReverseGeoCoderFailedToFindAddress(MapReverseGeoCoder mapReverseGeoCoder) {
    onFinishReverseGeoCoding("Fail");
}

private void onFinishReverseGeoCoding(String result) {

// Toast.makeText(LocationDemoActivity.this, "Reverse Geo-coding : " + result, Toast.LENGTH_SHORT).show(); }

/*
 * Method of receiving the result of a permission request using ActivityCompat.requestPermissions.
 */
@Override
public void onRequestPermissionsResult(int permsRequestCode,
                                       @NonNull String[] permissions,
                                       @NonNull int[] grandResults) {

    if ( permsRequestCode == PERMISSIONS_REQUEST_CODE && grandResults.length == REQUIRED_PERMISSIONS.length) {

        // If the request code is PERMISSIONS_REQUEST_CODE and the number of permissions requested is received,

        boolean check_result = true;


        // Check that you have allowed all permissions.

        for (int result : grandResults) {
            if (result != PackageManager.PERMISSION_GRANTED) {
                check_result = false;
                break;
            }
        }


        if ( check_result ) {
            Log.d("@@@", "start");
            //Can get location values
            mMapView.setCurrentLocationTrackingMode(MapView.CurrentLocationTrackingMode.TrackingModeOnWithHeading);
        }
        else {
            // If there is a permission that you rejected, explain why you cannot use the app and exit the app.There are two cases.

            if (ActivityCompat.shouldShowRequestPermissionRationale(this, REQUIRED_PERMISSIONS[0])) {

                Toast.makeText(MapsActivity.this), "Permission denied. Please run the app again to allow permission.", Toast.LENGTH_LONG).show();
                finish();


            }else {

                Toast.makeText(MapsActivity.this), "Permission denied. Permission must be allowed in Settings (App Properties). ", ", Toast.LENGTH_LONG).show();

            }
        }

    }
}

void checkRunTimePermission(){

    //Runtime Permission Processing
    // 1. Check if you have a location permission.
    int hasFineLocationPermission = ContextCompat.checkSelfPermission(MapsActivity.this,
            Manifest.permission.ACCESS_FINE_LOCATION);


    if (hasFineLocationPermission == PackageManager.PERMISSION_GRANTED ) {

        // 2. If you already have a permission,
        // (For Android versions 6.0 and earlier, it is recognized as already permitted because no runtime permission is required.)


        // 3. Location values can be imported
        mMapView.setCurrentLocationTrackingMode(MapView.CurrentLocationTrackingMode.TrackingModeOnWithHeading);


    } else { //2. Permission request is required if permission request has never been allowed. There are two cases (3-1, 4-1).

        // 3-1. If the user has refused permission,
        if (ActivityCompat.shouldShowRequestPermissionRationale(MapsActivity.this, REQUIRED_PERMISSIONS[0])) {

            // 3-2. Before proceeding with the request, it is necessary for the user to explain why permission is required.
            Toast.makeText (MapsActivity.this, "You need location access to run this app," Toast.LENGTH_LONG).show();
            // 3-3. Make a permission request to the user store. The result of the request is received from the onRequestPermissionResult.
            ActivityCompat.requestPermissions(MapsActivity.this, REQUIRED_PERMISSIONS,
                    PERMISSIONS_REQUEST_CODE);


        } } else {
            // 4-1. If the user has never denied the permission, the permission request is made immediately.
            // The result of the request is received from the onRequestPermissionResult.
            ActivityCompat.requestPermissions(MapsActivity.this, REQUIRED_PERMISSIONS,
                    PERMISSIONS_REQUEST_CODE);
        }

    }

}



//From here on, methods for GPS activation
private void showDialogForLocationServiceSetting() {

    AlertDialog.Builder builder = new AlertDialog.Builder(MapsActivity.this);
    builder.setTitle ("Disable Location Service");
    builder.setMessage ("The app requires a location service to use it).\n"
            + "Do you want to modify the location settings?");
    builder.setCancelable(true);
    builder.setPositiveButton("setup", newDialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {
            Intent callGPSSettingIntent
                    = = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivityForResult(callGPSSettingIntent, GPS_ENABLE_REQUEST_CODE);
        }
    });
    builder.setNegativeButton("cancel", newDialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    });
    builder.create().show();
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {

        case GPS_ENABLE_REQUEST_CODE:

            //Check if the user has enabled GPS
            if (checkLocationServicesStatus()) {
                if (checkLocationServicesStatus()) {

                    Log.d ("@@@", "onActivityResult : GPS enabled");
                    checkRunTimePermission();
                    return;
                }
            }

            break;
    }
}

public boolean checkLocationServicesStatus() {
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
            || || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////activity_maps.xml//////////////////////////////////////////////////////////

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="194dp"
    android:layout_weight="8"
    android:orientation="vertical">


    <EditText
        android:id="@+id/start"
        android:layout_width="320dp"
        android:layout_height="wrap_content"
        android:hint="origin" />

    <EditText
        android:id="@+id/goal"
        android:layout_width="320dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/start"
        android:hint="Destination" />

    <Button
        android:id="@+id/search1"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@+id/start"
        android:background="#f8dd43"
        android:text="Search" />

    <Button
        android:id="@+id/search2"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_below="@+id/search1"
        android:layout_marginTop="10dp"
        android:layout_toRightOf="@+id/goal"
        android:background="#f8dd43"
        android:text="Search" />


    <Button
        android:id="@+id/call"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/goal"
        android:layout_marginTop="5dp"
        android:background="#f8dd43"
        android:text="call" />

    <Button
        android:id="@+id/cancel"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/goal"
        android:layout_toRightOf="@+id/call"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="5dp"
        android:background="#f8dd43"
        android:text="Cancel" />


</RelativeLayout>



<net.daum.mf.map.api.MapView
    android:id="@+id/map_view"
    android:layout_width="match_parent"
    android:layout_height="550dp" />

android java xml error api

2022-09-20 22:03

1 Answers

Error caused by net.daum.mf.map.api.mapView object is null.

Is the object properly written in xml?

Check if the import is done properly.


2022-09-20 22:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.