java - android studio 应用程序在发布 json 时不断停止

标签 java android-studio

我想知道两个坐标之间的距离。

当坐标发生变化时,我想知道它们之间的距离(以米为单位)。

代码主要 Activity :

 private TextView textView;
    private TextView label3,label2;
    private EditText editText;

    protected LocationManager locationManager;
    private String name;
    double latitude;
    double longitude;

    RequestQueue queue;

    double Lan;
    double Lon;

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

        editText = (EditText)findViewById(R.id.editText);
        label3 = (TextView)findViewById(R.id.label3);
        label2 = (TextView)findViewById(R.id.label2);
        textView = (TextView) findViewById(R.id.label);

        int permissionStatus = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);

        if (permissionStatus == PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(getApplicationContext(),"access is",Toast.LENGTH_LONG).show();
        } else {
            ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.ACCESS_FINE_LOCATION},
                    1);
        }

        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000 * 10,
                10, this);

        queue = Volley.newRequestQueue(this);
    }


    @Override
    public void onLocationChanged(final Location location) {
        textView.setText("My location = Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());

        if (latitude != location.getLatitude() && longitude != location.getLongitude()) {
            Toast.makeText(this,"location changed",Toast.LENGTH_LONG).show();
            latitude  = location.getLatitude();
            longitude = location.getLongitude();

            getvalue();
        }
    }

    @Override
    public void onProviderDisabled(String provider) {
        Log.e("Latitude","disable");
    }

    @Override
    public void onProviderEnabled(String provider) {
        Log.e("Latitude","enable");
    }


    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.e("Latitude","status");
    }

    public void Insert(View view){

        name = editText.getText().toString();
        Insert.BackGround b=new Insert.BackGround();

        String lan = String.valueOf(latitude);
        String lon = String.valueOf(longitude);

        b.execute(name,lan,lon);

        String result = b.Result();
        Toast.makeText(this,result,Toast.LENGTH_LONG).show();
    }

public void getvalue(){
        Log.e("st","getvalue");

        String url = "http://my_url";

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {

                        String name = response.getString("name");
                        Lan = response.getDouble("latitude");
                        Lon = response.getDouble("longitude");

                        Log.e("lan", String.valueOf(Lan));
                        Log.e("lon", String.valueOf(Lon));

                        calculate(latitude,longitude,Lan,Lon);

                        label3.setText("name="+name+" Lan="+Lan+" Lon="+Lon);

                            } catch (JSONException ex) {
                            ex.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });

        queue.add(request);
    }

    public void calculate( double lan1, double lon1, double lan2, double lon2){

        Log.e("st","calculate");
        Location loc1 = new Location("point A");
        loc1.setLatitude(lan1);
        loc1.setLongitude(lon1);

        Location loc2 = new Location("point B");
        loc2.setLatitude(lan2);
        loc2.setLongitude(lon2);

        String distanceInMeters = String.valueOf(loc1.distanceTo(loc2));

        label2.setText("Distance = "+distanceInMeters );
    }

list :

    <uses-sdk
        android:targetSdkVersion="28" />

    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission. ACCESS_COARSE_LOCATION" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<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"
    android:networkSecurityConfig="@xml/network_security_config"
    android:usesCleartextTraffic="true"
    android:testOnly="false"
    android:debuggable="true"
    tools:ignore="HardcodedDebugMode">

该程序运行良好 Redmi Note 4 和模拟器 BlueStacks 但在 Redmi Note 5 和 Android > 8 应用程序中出现此类错误“应用程序不断停止”

我在网上找不到错误和答案 请帮助我

如果有人编写可以在任何设备上运行的代码,我会非常高兴:)

Android 日志猫

2020-02-26 18:22:42.553 3897-3897/com.example.getlocation E/GMPM: GoogleService failed to initialize, status: 10, Missing an expected resource: 'R.string.google_app_id' for initializing Google services.  Possible causes are missing google-services.json or com.google.gms.google-services gradle plugin.
2020-02-26 18:22:42.554 3897-3897/com.example.getlocation E/GMPM: Scheduler not set. Not logging error/warn.
2020-02-26 18:22:42.560 3897-3927/com.example.getlocation E/GMPM: Uploading is not possible. App measurement disabled
2020-02-26 18:22:44.854 3897-3897/com.example.getlocation E/st: getvalue
2020-02-26 18:22:44.874 3897-3931/com.example.getlocation E/AndroidRuntime: FATAL EXCEPTION: Thread-4
    Process: com.example.getlocation, PID: 3897
    java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/ProtocolVersion;
        at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:108)
        at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:93)
        at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:105)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.http.ProtocolVersion" on path: DexPathList[[zip file "/data/app/com.example.getlocation-9UATZ0srGfq2w1rY4vtYKQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.getlocation-9UATZ0srGfq2w1rY4vtYKQ==/lib/arm64, /system/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:108) 
        at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:93) 
        at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:105) 

最佳答案

您可能正在使用已弃用的 volley 库。使用official version相反

implementation 'com.android.volley:volley:1.1.1'

对于我在 log cat 中看到的其他问题与此相关 GoogleService failed to initialize, status: 10, Missing an expected resource: 'R.string.google_app_id' for initializing Google services. Possible causes are missing google-services.json or com.google.gms.google-services gradle plugin.

关于java - android studio 应用程序在发布 json 时不断停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60400774/

相关文章:

java - 我想当用户单击下一个播放按钮时停止当前播放的音频

java - 当我在 netbeans 上使用 Maven 创建 Java Web 项目时,应该忽略哪些文件和文件夹?

java - 从 android studio 中的 onCreateview 调用新方法中的 View

android - 从图库和相机获取图像 (Android Studio)

java - Eclipse Formatter 在代码块之间添加空行

java - '(' or '['UNIX下预期编译错误

android - 屏幕录制按钮在 Android Studio 中显示为灰色

java - CardView 未使用 LayoutInflater 正确膨胀

SQL Server 的 Java O/R 层?

android - 如何更改 XML 文件中的字体 android studio