我有一款适用于手机和平板电脑的应用。手机版工作正常,但平板电脑让我头疼。
应用程序从URL下载图像并在Gridview中将其显示为缩略图。
Android 4.1.2 minSdkVersion =“7” targetSdkVersion =“17”
首次运行时运行良好,但如果退出并重新运行,它会冻结设备(我正在Google Nexus 7平板电脑上测试),我将不得不重新启动设备。同样,它只发生在平板电脑版本上。
所以,这是我到目前为止研究和分析的主题,但因为我是新手,所以确定正确的解决方案有点困难。任何建议将不胜感激!
失败的粘合剂交易
android.os.TransactionTooLargeException
使用通用图像加载程序出现内存错误
这是我在应用程序崩溃时遇到的logcat。
05-29 10:29:47.842: W/ActivityManager(485): Scheduling restart of crashed service com.android.nfc/.handover.HandoverService in 0ms
05-29 10:29:47.852: E/JavaBinder(485): !!! FAILED BINDER TRANSACTION !!!
05-29 10:29:48.012: W/ActivityManager(485): Exception in new application when starting activity com.rssapp.mobile/.MainActivityLarge
05-29 10:29:48.012: W/ActivityManager(485): android.os.TransactionTooLargeException
05-29 10:29:48.012: W/ActivityManager(485): at android.os.BinderProxy.transact(Native Method)
05-29 10:29:48.012: W/ActivityManager(485): at android.app.ApplicationThreadProxy.scheduleLaunchActivity(ApplicationThreadNative.java:708)
05-29 10:29:48.012: W/ActivityManager(485): at com.android.server.am.ActivityStack.realStartActivityLocked(ActivityStack.java:696)
05-29 10:29:48.012: W/ActivityManager(485): at com.android.server.am.ActivityManagerService.attachApplicationLocked(ActivityManagerService.java:4252)
05-29 10:29:48.012: W/ActivityManager(485): at com.android.server.am.ActivityManagerService.attachApplication(ActivityManagerService.java:4316)
05-29 10:29:48.012: W/ActivityManager(485): at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:386)
05-29 10:29:48.012: W/ActivityManager(485): at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:1674)
05-29 10:29:48.012: W/ActivityManager(485): at android.os.Binder.execTransact(Binder.java:351)
05-29 10:29:48.012: W/ActivityManager(485): at com.android.server.SystemServer.init1(Native Method)
05-29 10:29:48.012: W/ActivityManager(485): at com.android.server.SystemServer.main(SystemServer.java:1064)
05-29 10:29:48.012: W/ActivityManager(485): at java.lang.reflect.Method.invokeNative(Native Method)
05-29 10:29:48.012: W/ActivityManager(485): at java.lang.reflect.Method.invoke(Method.java:511)
05-29 10:29:48.012: W/ActivityManager(485): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-29 10:29:48.012: W/ActivityManager(485): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-29 10:29:48.012: W/ActivityManager(485): at dalvik.system.NativeStart.main(Native Method)
这是我正在使用的通用图像加载器。
@Override
public void onCreate() {
super.onCreate();
applicationController = this;
File cacheDir = StorageUtils.getOwnCacheDirectory(getApplicationContext(), "UniversalImageLoader/Cache");
DisplayImageOptions options = new DisplayImageOptions.Builder()
.resetViewBeforeLoading()
.cacheInMemory()
.cacheOnDisc()
.imageScaleType(ImageScaleType.EXACTLY)
.build();
ImageLoader imageLoader = ImageLoader.getInstance();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.threadPoolSize(5)
.threadPriority(Thread.NORM_PRIORITY - 1)
.denyCacheImageMultipleSizesInMemory()
.memoryCache(new WeakMemoryCache())
.memoryCache(new LRULimitedMemoryCache(3 * 1024 * 1024))
.discCache(new FileCountLimitedDiscCache(cacheDir, 20))
.discCacheFileNameGenerator(new HashCodeFileNameGenerator())
.imageDownloader(new URLConnectionImageDownloader(5 * 1000, 30 * 1000))
.defaultDisplayImageOptions(options)
.enableLogging()
.build();
imageLoader.init(config);
}
这是MainActivityLarge.java
public class MainActivityLarge extends Activity {
RSSFeed feed;
GridView gridView;
CustomListAdapterLarge adapter;
String feedLink="http://mywebsite.com/feed/";
ArrayList<RSSItem> rssItems=new ArrayList<RSSItem>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
// set the feed link for refresh
feedLink = new SplashActivity().RSSFEEDURL;
// Get feed form the file
feed = (RSSFeed) getIntent().getExtras().get("feed");
createFeedGroup();
// Initialize the variables:
gridView = (GridView) findViewById(R.id.grid_view);
// Set an Adapter to the ListView
adapter = new CustomListAdapterLarge(this,rssItems);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// actions to be performed when a list item clicked
int pos = arg2;
Bundle bundle = new Bundle();
bundle.putSerializable("feed", feed);
Intent intent = new Intent(MainActivityLarge.this, SwipeDetailViewLarge.class);
intent.putExtras(bundle);
intent.putExtra("type", rssItems.get(pos).getCategory());
startActivity(intent);
overridePendingTransition(R.anim.left2right,R.anim.right2left);
}
});
}
private void createFeedGroup() {
rssItems=new ArrayList<RSSItem>();
for (int i = 0; i < AppConstants.ARRAY.length; i++) {
RSSItem rssItem=feed.getMapValues(AppConstants.ARRAY[i]);
rssItems.add(rssItem);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(adapter!=null){
adapter.imageLoader.clearMemoryCache();
adapter.notifyDataSetChanged();
}
}
}