gpt4 book ai didi

使用 GPS 连接的 Android 应用程序停止工作

转载 作者:行者123 更新时间:2023-12-02 04:19:56 25 4
gpt4 key购买 nike

我无法获取 Android 设备的速度。所以我尝试使用一个示例,应该可以工作。使用代码 http://www.androidhub4you.com/2013/06/how-to-get-device-current-speed-in_112.html我应该得到速度。但应用程序总是在连接 GPS 之前崩溃。代码是:

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;



public class MainActivity extends Activity {


Context context;
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.Geschwindigkeit);

LocationManager locationManager = (LocationManager) this .getSystemService(Context.LOCATION_SERVICE);

LocationListener locationListener = newLocationListener(){ public void onLocationChanged(Location location) {

location.getLatitude();

Toast.makeText(context, "Current speed:" + location.getSpeed(), Toast.LENGTH_SHORT).show(); }

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

public void onProviderEnabled(String provider) { }

public void onProviderDisabled(String provider) { }
};

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
}

日志内容如下:

08-04 14:36:51.023  11219-11219/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at android.widget.Toast.<init>(Toast.java:105)
at android.widget.Toast.makeText(Toast.java:261)
at test.gpsspeed.MainActivity$1.onLocationChanged(MainActivity.java:34)
at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:257)
at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:186)
at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:202)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5365)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)

我能做什么?或者我做错了什么?

最佳答案

所以这有效:

public class MainActivity extends Activity {



Context context;
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//geschw=(TextView) findViewById(R.id.Geschwindigkeit);

LocationManager locationManager = (LocationManager) this .getSystemService(Context.LOCATION_SERVICE);



LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) {

location.getLatitude();
Context neu = MainActivity.this;

try {

Toast.makeText(neu, "Current speed:" + location.getSpeed(), Toast.LENGTH_SHORT).show();
}
catch (NullPointerException e){}
}


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

public void onProviderEnabled(String provider) { }

public void onProviderDisabled(String provider) { }
};

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
}

关于使用 GPS 连接的 Android 应用程序停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31810058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com