简体   繁体   中英

My Java speedometer mobile app will compile but not start

My app will compile but not start on my phone when I am trying to test it it comes up with a lot of errors on the console and phone just says it keeps stopping.

These are the errors

W/t.myapplicatio: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
W/t.myapplicatio: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.SeniorProject.myapplication, PID: 2649
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.SeniorProject.myapplication/com.SeniorProject.myapplication.MainActivity}: java.util.UnknownFormatConversionException: Conversion = 'End of String'
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3086)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3229)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1926)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:6981)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
     Caused by: java.util.UnknownFormatConversionException: Conversion = 'End of String'
        at java.util.Formatter$FormatSpecifierParser.peek(Formatter.java:2641)
        at java.util.Formatter$FormatSpecifierParser.<init>(Formatter.java:2618)
        at java.util.Formatter.parse(Formatter.java:2557)
        at java.util.Formatter.format(Formatter.java:2504)
        at com.SeniorProject.myapplication.MainActivity.updateSpeed(MainActivity.java:97)
        at com.SeniorProject.myapplication.MainActivity.onCreate(MainActivity.java:44)
        at android.app.Activity.performCreate(Activity.java:7326)
        at android.app.Activity.performCreate(Activity.java:7317)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3066)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3229) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1926) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:6981) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445) 
Disconnected from the target VM, address: 'localhost:8602', transport: 'socket'

This is the mainactivity.java folder

package com.SeniorProject.myapplication;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SwitchCompat;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Formatter;
import java.util.Locale;

public class MainActivity extends AppCompatActivity implements 
LocationListener {

SwitchCompat Sw_metric;
TextView tv_speed;

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

    Sw_metric = findViewById(R.id.sw_metric);
    tv_speed = findViewById(R.id.tv_speed);

    //check for gps permission
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && 
checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
    != PackageManager.PERMISSION_GRANTED){
        requestPermissions(new String[ 
{Manifest.permission.ACCESS_FINE_LOCATION}, 1000);
    } else{
        //start the program if the permission is granted
        doStuff();
    }

    this.updateSpeed(null);

    Sw_metric.setOnCheckedChangeListener(new 
CompoundButton.OnCheckedChangeListener(){
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean 
isChecked){
            MainActivity.this.updateSpeed(null);
        }
    });
}

@Override
public void onLocationChanged(Location location) {
    if(location != null){
        CLocation myLocation =new CLocation(location, 
this.useMetricUnits());
        this.updateSpeed(myLocation);
    }
}

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

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

@SuppressLint("MissingPermission")
private void doStuff(){
    LocationManager locationManager = (LocationManager) 
this.getSystemService(Context.LOCATION_SERVICE);
    if(locationManager != null) {

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, 
this);
    }
    Toast.makeText(this, "Waiting for GPS connection", 
Toast.LENGTH_SHORT).show();
}

@SuppressWarnings("MalformedFormatString")
private void updateSpeed(CLocation location) {
    float nCurrentSpeed = 0;

    if (location != null) {
        location.setUseMericUnits(this.useMetricUnits());
        nCurrentSpeed = location.getSpeed();
    }

    Formatter fmt = new Formatter(new StringBuilder());
    fmt.format(Locale.US, "%5.1", nCurrentSpeed);
    String strCurrentSpeed = fmt.toString();
    strCurrentSpeed = strCurrentSpeed.replace(" ", "0");

    if (this.useMetricUnits()) {
        tv_speed.setText(strCurrentSpeed + " km/h");
    } else {
        tv_speed.setText(strCurrentSpeed + " mp/h");
    }
}
private boolean useMetricUnits() {
    return Sw_metric.isChecked();
}

    @Override
    public void onRequestPermissionsResult(int requestCode,@NonNull 
String[] permissions,
    @NonNull int[] grantResults) {
    if(requestCode == 1000) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            doStuff();
        } else {
            finish();
        }
    }
}
}

This is the CLocation.java folder

package com.SeniorProject.myapplication;

import android.location.Location;

public class CLocation extends Location {

    private boolean bUseMetricUnits = false;

    public CLocation(Location location){
        this(location, true);
    }

    public CLocation(Location location, boolean bUseMetricUnits) {
         super(location);
         this.bUseMetricUnits = bUseMetricUnits;
    }
    public boolean getUseMetricUnits() {
        return this.bUseMetricUnits;
    }

    public void setUseMericUnits(boolean bUseMetricUnits){
        this.bUseMetricUnits = bUseMetricUnits;
    }

    @Override
    public float distanceTo(Location dest) {
        float nDistance = super.distanceTo(dest);

        if(!this.getUseMetricUnits()) {
            // convert meters to feet
            nDistance = nDistance * 3.28083989501312f;
        }
        return nDistance;
    }

    @Override
    public double getAltitude() {
        double nAltitude = super.getAltitude();

        if(!this.getUseMetricUnits()) {
            // convert meters to feet
            nAltitude = nAltitude * 3.28083989501312d;
        }
        return nAltitude;
    }

    @Override
    public float getSpeed() {
        float nSpeed = super.getSpeed() * 3.6f;

        if(!this.getUseMetricUnits()) {
            // convert meters/second to miles/hour
            nSpeed = super.getSpeed() * 2.2363629f;
        }
        return nSpeed;
    }

    @Override
    public float getAccuracy() {
        float nAccuracy = super.getAccuracy();

        if(!this.getUseMetricUnits()) {
            // convert meters to feet
            nAccuracy = nAccuracy * 3.28083989501312f;
        }
        return nAccuracy;
    }
}

it will not allow me to post the errors that are there. It's saying I need to add some more details because it is mostly code so I am just trying to get the word count higher.

I'd guess the cause of your problem is in the following line:

fmt.format(Locale.US, "%5.1", nCurrentSpeed);

In the format string, you specified a width and a precision (5.1), but not a conversion for the format operation, hence the error:

Caused by: java.util.UnknownFormatConversionException: Conversion = 'End of String'

See the Java documentation for the Format class for conversion options, and pick the one that is appropriate for how you want the value (nCurrentSpeed) to be formatted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM