处理启动活动时的错误

时间:2013-08-22 16:57:55

标签: java android

我只是尝试与我的应用程序的链接共享字符串。 这是我的代码:

public static void shareApp(Activity activity){
    ShareCompat.IntentBuilder builder = ShareCompat.IntentBuilder.from(activity);
    builder.setText("some link")
           .setType("text/html");

    activity.startActivity(builder.createChooserIntent());
}

通常它运作良好。但有时我会在下一个堆栈跟踪中遇到错误:

0   java.lang.OutOfMemoryError: bitmap size exceeds VM budget
1  at  android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
2  at  android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
3  at  android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
4  at  android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
5  at  android.content.res.Resources.loadDrawable(Resources.java:1709)
6  at  android.content.res.Resources.getDrawable(Resources.java:581)
7  at  android.app.ContextImpl$ApplicationPackageManager.getDrawable(ContextImpl.java:2201)
8  at  android.content.pm.PackageItemInfo.loadIcon(PackageItemInfo.java:140)
9  at  android.content.pm.ComponentInfo.loadDefaultIcon(ComponentInfo.java:154)
10  at  android.content.pm.PackageItemInfo.loadIcon(PackageItemInfo.java:145)
11  at  android.content.pm.ResolveInfo.loadIcon(ResolveInfo.java:181)
12  at  com.android.internal.app.ResolverActivity$ResolveListAdapter.bindView(ResolverActivity.java:431)
13  at  com.android.internal.app.ResolverActivity$ResolveListAdapter.getView(ResolverActivity.java:415)
14  at  android.widget.AbsListView.obtainView(AbsListView.java:1430)
15  at  android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
16  at  android.widget.ListView.onMeasure(ListView.java:1127)

或与:

0   java.lang.OutOfMemoryError: bitmap size exceeds VM budget
1   at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
2   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
3   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
4   at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
5   at android.content.res.Resources.loadDrawable(Resources.java:1709)
6   at android.content.res.Resources.getDrawable(Resources.java:581)
7   at android.widget.AbsListView.setOverScrollMode(AbsListView.java:655)
8   at android.view.View.<init>(View.java:1879)
9   at android.view.View.<init>(View.java:1921)
10  at android.view.ViewGroup.<init>(ViewGroup.java:292)
11  at android.widget.AdapterView.<init>(AdapterView.java:228)
12  at android.widget.AbsListView.<init>(AbsListView.java:592)
13  at android.widget.ListView.<init>(ListView.java:163)
14  at android.widget.ListView.<init>(ListView.java:159)
15  at com.android.internal.app.AlertController$RecycleListView.<init>(AlertController.java:673)

这个错误在HTC Nexus One和Android 2.3.6上得到了重现,我认为这个设备存在问题 - 它很老而且很慢。 但是,我可以为startActivity()方法或其他解决方法创建一些包装来阻止此崩溃吗?

1 个答案:

答案 0 :(得分:0)

您的logcat显示内存不足:

java.lang.OutOfMemoryError: bitmap size exceeds VM budget  

并且你说它有时候工作得很好所以我建议你首先下载一个软件来阻止任何不需要的后台进程,例如Advanced Task Manager Google PlayStore免费提供,运行你的应用程序,如果有效,请再次查看。

另一个“解决方法”是WeakReferencing您的位图,因此它是第一个被垃圾回收的。