简体   繁体   English

我的 Java 速度计移动应用程序将编译但无法启动

[英]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这是 mainactivity.java 文件夹

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这是 CLocation.java 文件夹

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:在格式字符串中,您指定了宽度和精度 (5.1),但没有指定格式操作的转换,因此出现错误:

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.有关转换选项的 Format 类,请参阅Java 文档,并选择适合您希望如何格式化值 (nCurrentSpeed) 的选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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