gpt4 book ai didi

android - 位图大小超出 VM 预算

转载 作者:行者123 更新时间:2023-11-29 00:48:49 25 4
gpt4 key购买 nike

我已经阅读了很多关于这个问题的帖子,但我找不到适用于我的代码的帖子(或者至少我可以找到相关的帖子)。

我有一个可以横向和纵向显示的主屏幕小部件。如果我不断改变设备的方向,我会在我的 logcat 中得到这个:

03-08 10:41:35.793: ERROR/dalvikvm-heap(1159): 4707316-byte external allocation too large for this process.
03-08 10:41:35.793: ERROR/GraphicsJNI(1159): VM won't let us allocate 4707316 bytes
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): updateAppWidget couldn't find any view, using error view
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): android.view.InflateException: Binary XML file line #1: Error inflating class android.widget.LinearLayout
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at android.view.LayoutInflater.createView(LayoutInflater.java:513)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at android.widget.RemoteViews.apply(RemoteViews.java:930)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at android.appwidget.AppWidgetHostView.updateAppWidget(AppWidgetHostView.java:219)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at android.appwidget.AppWidgetHostView.updateAppWidget(AppWidgetHostView.java:155)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at android.appwidget.AppWidgetHost.createView(AppWidgetHost.java:218)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at com.android.launcher2.Launcher.bindAppWidget(Launcher.java:2393)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at com.android.launcher2.LauncherModel$Loader$LoaderThread$7.run(LauncherModel.java:1051)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at com.android.launcher2.DeferredHandler$Impl.handleMessage(DeferredHandler.java:48)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at android.os.Handler.dispatchMessage(Handler.java:99)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at android.os.Looper.loop(Looper.java:123)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at android.app.ActivityThread.main(ActivityThread.java:4645)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at java.lang.reflect.Method.invokeNative(Native Method)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at java.lang.reflect.Method.invoke(Method.java:521)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at dalvik.system.NativeStart.main(Native Method)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): Caused by: java.lang.reflect.InvocationTargetException
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at android.widget.LinearLayout.(LinearLayout.java:115)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at java.lang.reflect.Constructor.constructNative(Native Method)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at android.view.LayoutInflater.createView(LayoutInflater.java:500)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): ... 19 more
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at android.graphics.Bitmap.nativeCreate(Native Method)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at android.graphics.Bitmap.createBitmap(Bitmap.java:468)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at android.graphics.Bitmap.createBitmap(Bitmap.java:435)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:340)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:488)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:462)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:323)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at android.content.res.Resources.loadDrawable(Resources.java:1718)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at android.view.View.(View.java:1885)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at android.view.View.(View.java:1834)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): at android.view.ViewGroup.(ViewGroup.java:285)
03-08 10:41:35.793: WARN/AppWidgetHostView(1159): ... 23 more

我的代码是这样的,它利用了 remoteViews:

    public class WidgetLayoutBuilder {

public static final String DEG = "\u00B0";

public static final int LAYOUT_CURRENT = 1000;
public static final int LAYOUT_FORECAST = 1001;

/**
* Constructs current conditions layout.
*
* @param context
* @param wd
* @return
*/
public static RemoteViews buildCurrentLayout(Context context, int appWidgetId, ACCUWX_WeatherData wd){
Log.d("########", "remoteViews buildCurrentLayout");
// Return object
RemoteViews toRet = new RemoteViews(context.getPackageName(), R.layout.widget_current_layout);

String cityText = wd.getCityName();
String tempUnits = wd.getMetric() == 0 ? "F" : "C";
String iconId = "icon_" + wd.getCurrentIconCode();

// Populate views with weather data
toRet.setTextViewText (R.id.current_location_text, cityText);
toRet.setTextViewText (R.id.current_temp_text, wd.getCurrentTemperature() + DEG + tempUnits);
toRet.setTextViewText (R.id.current_conditions_text, wd.getCurrentConditions());
toRet.setImageViewResource (R.id.current_condition_image, ACCUWX.getDrawableId(iconId));
toRet.setTextViewText (R.id.current_hi_temp, context.getString(R.string.hi) + ": " + wd.getForecast().get(0).getDailyHigh() + DEG);
toRet.setTextViewText (R.id.current_lo_temp, context.getString(R.string.lo) +": " + wd.getForecast().get(0).getDailyLow() + DEG);

// Make alert icon visible if necessary.
int visibility = wd.getAlertIsActive() == 0 ? View.INVISIBLE : View.VISIBLE;
toRet.setViewVisibility(R.id.alert_icon, visibility);

if (MainActivity.mLocations == null)
MainActivity.mLocations = new ArrayList<String>();
if (MainActivity.mLocName == null)
MainActivity.mLocName = new String("");


if (!MainActivity.mLocations.contains(wd.getZipCode()) || !MainActivity.mLocName.contains(wd.getCityName()))
MainActivity.mLocations.add(wd.getZipCode());

MainActivity.isFromLocationSearchActivity = false;

// Launch SimpleWeather application intent
Intent intentAppLaunch = new Intent(context, MainActivity.class);
intentAppLaunch.putExtra(ACCUWX.Extras.LOCATION_CODE, wd.getZipCode());
intentAppLaunch.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingAppLaunch = PendingIntent.getActivity(context, appWidgetId, intentAppLaunch, PendingIntent.FLAG_CANCEL_CURRENT);
toRet.setOnClickPendingIntent(R.id.current_layout, pendingAppLaunch);

return toRet;
}

/**
* Returns pretty names for display purposes in the form 'city name, state name' for the US and 'cityname, countryname (region)' for other countries.
* @return
*/
public static String getPrettyName(ACCUWX_WeatherData wd){
String prettyName = "null";

String state = wd.getStateName();
String city = wd.getCityName();
String country = wd.getCountryName();
String countryCode = wd.getCountryCode();

if (countryCode.equalsIgnoreCase("us"))
prettyName = city + ", " + state;
else
prettyName = city + ", " + country + " (" + state + ")";

return prettyName;
}


/**
* Constructs deleted layout.
*
* @param context
* @return
*/
public static RemoteViews buildDeletedLayout(Context context){
Log.d("########", "remoteViews buildDeletedLayout");
return new RemoteViews(context.getPackageName(), R.layout.widget_deleted_layout);

}

}

位图错误是否来自设备本身而不是我的应用程序的问题?

非常感谢任何帮助或指导!

最佳答案

bitmap size exceeds VM budget

这只是内存泄漏的一般错误。在真实设备中使用 DDMS 或各种免费应用程序监控您的内存使用情况,您会看到您的内存使用量一直在上升,直到它爆炸(对我来说大约是 28MB)。

您有内存泄漏,只有您拥有所有源代码才能找到它。查找已加载但未释放的位图以及已加载但未关闭的游标。

关于android - 位图大小超出 VM 预算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4725066/

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