Android Wear应用程序java.lang.OutOfMemoryError:分配失败
我有个问题。我想为我的Android Wear设备制作一个Android应用程序。我想获得我当前位置的纬度和经度。但当我运行应用程序时,出现了以下错误:Android Wear应用程序java.lang.OutOfMemoryError:分配失败,java,android,gps,location,wear-os,Java,Android,Gps,Location,Wear Os,我有个问题。我想为我的Android Wear设备制作一个Android应用程序。我想获得我当前位置的纬度和经度。但当我运行应用程序时,出现了以下错误: 03-22 11:14:00.096 29013-29013/? E/AndroidRuntime: Error reporting crash java.lang.OutOfMemoryError: Failed to allocate a 5243
03-22 11:14:00.096 29013-29013/? E/AndroidRuntime: Error reporting crash
java.lang.OutOfMemoryError: Failed to allocate a 52435096 byte allocation with 2097152 free bytes and 32MB until OOM
at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:95)
at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:125)
at java.lang.StringBuffer.append(StringBuffer.java:278)
at java.io.StringWriter.write(StringWriter.java:123)
at com.android.internal.util.FastPrintWriter.flushLocked(FastPrintWriter.java:358)
at com.android.internal.util.FastPrintWriter.appendLocked(FastPrintWriter.java:303)
at com.android.internal.util.FastPrintWriter.write(FastPrintWriter.java:625)
at com.android.internal.util.FastPrintWriter.append(FastPrintWriter.java:658)
at java.io.PrintWriter.append(PrintWriter.java:691)
at java.io.PrintWriter.append(PrintWriter.java:687)
at java.io.Writer.append(Writer.java:198)
at java.lang.Throwable.printStackTrace(Throwable.java:324)
at java.lang.Throwable.printStackTrace(Throwable.java:300)
at android.util.Log.getStackTraceString(Log.java:343)
at com.android.internal.os.RuntimeInit.Clog_e(RuntimeInit.java:61)
at com.android.internal.os.RuntimeInit.-wrap0(RuntimeInit.java)
at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:86)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
这是我的代码:
MainActivity.java
}
GPSTracker.java
}
我希望任何人都能看到这个问题,并能帮助我解决这个问题
android:largeHeap="true"
在你的android清单中,希望这能解决你的问题。
请更详细地说明这个问题到底发生在哪里对不起!当我点击btSetHuidigeLocatie按钮时,Android Studio在日志中给出了这个问题。使用调试器继续执行,直到按钮按下后出现这个问题。这就是问题所在。它将帮助我们找到问题。stacktrace不完整,或者说stacktrace行中没有任何一行提到您的类?我们需要知道错误发生在您编写的文件的哪一行。很可能您的应用程序正在某处泄漏内存,特别是考虑到当您遵循错误的largeHeap=true建议时,这种情况仍在发生。这里的任何人都不太可能仅仅通过查看源代码就发现内存泄漏。了解如何在上监视内存使用,并在逐步浏览应用程序时这样做,以缩小泄漏发生的范围。可能在一些设备上工作,但不是最佳样式,不是吗?大多数应用程序不应该需要这样做,而是应该专注于减少总体内存使用以提高性能。启用此选项也不能保证可用内存的固定增长,因为某些设备受其总可用内存的限制。我也添加了此选项,但没有帮助。是的,这不是解决OOM的好方法,但这是一个非常快速的选项,否则,您必须减少设备中的内存消耗。我觉得这可以作为评论,而不是回答。因为它是一个非常可怕的黑客,并可以促进坏的实践。@ DeBoMiTrayi我同意你,这是不好的做法,但同时你必须考虑,这个选项是由Android本身。如果你在看了代码后给他一些关于如何应用良好实践的提示,这将非常有助于他解决这个问题。我的主要目标是解决这个问题,如果你去阅读它的链接,那么很明显,你必须减少内存的使用。
public class GPSTracker extends Service implements LocationListener {
private final Context context;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
Location location;
double latitude;
double longitude;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.context = context;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
if ( Build.VERSION.SDK_INT >= 23 &&
ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission( context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return location;
}
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
public void stopUsingGPS() {
if (locationManager != null) {
locationManager.removeUpdates(GPSTracker.this);
}
}
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}
return latitude;
}
public double getLongitude(){
if(location != null){
longitude = location.getLongitude();
}
return longitude;
}
public boolean canGetLocation(){
return this.canGetLocation();
}
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle("GPS in settings");
alertDialog.setMessage("GPS staat niet aan, Wilt u naar uw instellingen gaan?");
alertDialog.setPositiveButton("Instellingen", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
context.startActivity(intent);
}
});
alertDialog.setNegativeButton("Annuleer", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
android:largeHeap="true"