我有这个内存不足错误我有一个viewpager使用活动而不是片段和imageview然后当我得到这个错误我把它改为片段...但这个错误一直显示我尝试了很多解决方案没有任何工作。 ..
我的代码:
public class ScreenSlidePageFragment extends Fragment {
public static final String ARG_PAGE = "page";
private int mPageNumber;
private int[] pics = {R.drawable.intro1, R.drawable.intro2,R.drawable.intro3,R.drawable.intro4,R.drawable.intro5,R.drawable.intro6,R.drawable.intro7,R.drawable.intro8,
R.drawable.intro9,R.drawable.intro10,R.drawable.intro11,R.drawable.intro12,R.drawable.intro13,R.drawable.intro14,R.drawable.intro15,R.drawable.intro16,R.drawable.intro17,R.drawable.intro18,
R.drawable.intro19,R.drawable.intro20,R.drawable.intro21,R.drawable.intro22,R.drawable.intro23,R.drawable.intro24,R.drawable.intro25,R.drawable.intro26,R.drawable.intro27,R.drawable.intro28,R.drawable.intro29,R.drawable.intro30,
R.drawable.intro31,R.drawable.intro32,R.drawable.intro33,R.drawable.intro34,R.drawable.intro35,R.drawable.intro36,R.drawable.intro37,R.drawable.intro38,R.drawable.intro39,R.drawable.intro40,R.drawable.intro41,R.drawable.intro42,
R.drawable.intro43,R.drawable.intro44,R.drawable.intro45,R.drawable.intro46,R.drawable.intro47,R.drawable.intro48,R.drawable.intro49,R.drawable.intro50,R.drawable.intro51,R.drawable.intro52,R.drawable.intro53,R.drawable.intro54,
R.drawable.intro55,R.drawable.intro56,R.drawable.intro57,R.drawable.intro58,R.drawable.intro59,R.drawable.intro60,R.drawable.intro61,R.drawable.intro62,R.drawable.intro63,R.drawable.intro64,R.drawable.intro65,R.drawable.intro66,
R.drawable.intro67,R.drawable.intro68,R.drawable.intro69,R.drawable.intro70,R.drawable.intro71,R.drawable.intro72,R.drawable.intro73};
int count=72;//it's the number of the images-1;
public static ScreenSlidePageFragment create(int pageNumber) {
ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
fragment.setArguments(args);
return fragment;
}
public ScreenSlidePageFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPageNumber = getArguments().getInt(ARG_PAGE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater
.inflate(R.layout.image_view_layout, container, false);
ImageView img=(ImageView) rootView.findViewById(R.id.img_pager);
img.setImageResource(pics[count-mPageNumber]);
return rootView;
}
public int getPageNumber() {
return mPageNumber;
}
}
然后活动是:
public class Introduction_Activity extends FragmentActivity {
private static final int NUM_PAGES = 73;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_introduction);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.imgs_viewpager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
mPager.setCurrentItem(NUM_PAGES-1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.introduction_, menu);
return true;
}
static class ViewHolder {
ImageView img_view;
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return ScreenSlidePageFragment.create(position);
}
@Override
public int getCount() {
return NUM_PAGES;
}
}
}
那是我的logcat:
10-01 14:44:49.224: E/AndroidRuntime(4770): FATAL EXCEPTION: main
10-01 14:44:49.224: E/AndroidRuntime(4770): java.lang.OutOfMemoryError
10-01 14:44:49.224: E/AndroidRuntime(4770): at android.graphics.Bitmap.nativeCreate(Native Method)
10-01 14:44:49.224: E/AndroidRuntime(4770): at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
10-01 14:44:49.224: E/AndroidRuntime(4770): at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
10-01 14:44:49.224: E/AndroidRuntime(4770): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
10-01 14:44:49.224: E/AndroidRuntime(4770): at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:618)
10-01 14:44:49.224: E/AndroidRuntime(4770): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:593)
10-01 14:44:49.224: E/AndroidRuntime(4770): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:445)
10-01 14:44:49.224: E/AndroidRuntime(4770): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:775)
10-01 14:44:49.224: E/AndroidRuntime(4770): at android.content.res.Resources.loadDrawable(Resources.java:1968)
10-01 14:44:49.224: E/AndroidRuntime(4770): at android.content.res.Resources.getDrawable(Resources.java:677)
10-01 14:44:49.224: E/AndroidRuntime(4770): at android.widget.ImageView.resolveUri(ImageView.java:542)
10-01 14:44:49.224: E/AndroidRuntime(4770): at android.widget.ImageView.setImageResource(ImageView.java:315)
10-01 14:44:49.224: E/AndroidRuntime(4770): at packagename.ScreenSlidePageFragment.onCreateView(ScreenSlidePageFragment.java:60)
10-01 14:44:49.224: E/AndroidRuntime(4770): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
10-01 14:44:49.224: E/AndroidRuntime(4770): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
10-01 14:44:49.224: E/AndroidRuntime(4770): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
10-01 14:44:49.224: E/AndroidRuntime(4770): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
10-01 14:44:49.224: E/AndroidRuntime(4770): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
10-01 14:44:49.224: E/AndroidRuntime(4770): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
10-01 14:44:49.224: E/AndroidRuntime(4770): at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
10-01 14:44:49.224: E/AndroidRuntime(4770): at android.support.v4.view.ViewPager.populate(ViewPager.java:1064)
10-01 14:44:49.224: E/AndroidRuntime(4770): at android.support.v4.view.ViewPager.populate(ViewPager.java:911)
10-01 14:44:49.224: E/AndroidRuntime(4770): at android.support.v4.view.ViewPager$3.run(ViewPager.java:242)
10-01 14:44:49.224: E/AndroidRuntime(4770): at android.os.Handler.handleCallback(Handler.java:605)
10-01 14:44:49.224: E/AndroidRuntime(4770): at android.os.Handler.dispatchMessage(Handler.java:92)
10-01 14:44:49.224: E/AndroidRuntime(4770): at android.os.Looper.loop(Looper.java:137)
10-01 14:44:49.224: E/AndroidRuntime(4770): at android.app.ActivityThread.main(ActivityThread.java:4514)
10-01 14:44:49.224: E/AndroidRuntime(4770): at java.lang.reflect.Method.invokeNative(Native Method)
10-01 14:44:49.224: E/AndroidRuntime(4770): at java.lang.reflect.Method.invoke(Method.java:511)
10-01 14:44:49.224: E/AndroidRuntime(4770): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
10-01 14:44:49.224: E/AndroidRuntime(4770): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
10-01 14:44:49.224: E/AndroidRuntime(4770): at dalvik.system.NativeStart.main(Native Method)
我甚至补充道:
@Override
public void onDetach(){
super.onDetach();
//img.setImageResource(0);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(this).commit();
Log.d("was detached","yes");
}
到我的片段,它仍然让我错误;
修改 我用代码调整了图像大小并回收了不需要的位图并再次出现此错误:
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class ScreenSlidePageFragment extends Fragment {
/**
* The argument key for the page number this fragment represents.
*/
public static final String ARG_PAGE = "page";
/**
* The fragment's page number, which is set to the argument value for {@link #ARG_PAGE}.
*/
ImageView img;
int height;
int width;
private int mPageNumber;
private int[] pics = {R.drawable.intro1, R.drawable.intro2,R.drawable.intro3,R.drawable.intro4,R.drawable.intro5,R.drawable.intro6,R.drawable.intro7,R.drawable.intro8,
R.drawable.intro9,R.drawable.intro10,R.drawable.intro11,R.drawable.intro12,R.drawable.intro13,R.drawable.intro14,R.drawable.intro15,R.drawable.intro16,R.drawable.intro17,R.drawable.intro18,
R.drawable.intro19,R.drawable.intro20,R.drawable.intro21,R.drawable.intro22,R.drawable.intro23,R.drawable.intro24,R.drawable.intro25,R.drawable.intro26,R.drawable.intro27,R.drawable.intro28,R.drawable.intro29,R.drawable.intro30,
R.drawable.intro31,R.drawable.intro32,R.drawable.intro33,R.drawable.intro34,R.drawable.intro35,R.drawable.intro36,R.drawable.intro37,R.drawable.intro38,R.drawable.intro39,R.drawable.intro40,R.drawable.intro41,R.drawable.intro42,
R.drawable.intro43,R.drawable.intro44,R.drawable.intro45,R.drawable.intro46,R.drawable.intro47,R.drawable.intro48,R.drawable.intro49,R.drawable.intro50,R.drawable.intro51,R.drawable.intro52,R.drawable.intro53,R.drawable.intro54,
R.drawable.intro55,R.drawable.intro56,R.drawable.intro57,R.drawable.intro58,R.drawable.intro59,R.drawable.intro60,R.drawable.intro61,R.drawable.intro62,R.drawable.intro63,R.drawable.intro64,R.drawable.intro65,R.drawable.intro66,
R.drawable.intro67,R.drawable.intro68,R.drawable.intro69,R.drawable.intro70,R.drawable.intro71,R.drawable.intro72,R.drawable.intro73};
int count=72;//it's the number of the images-1;
/**
* Factory method for this fragment class. Constructs a new fragment for the given page number.
*/
public static ScreenSlidePageFragment create(int pageNumber) {
ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
fragment.setArguments(args);
return fragment;
}
public ScreenSlidePageFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DisplayMetrics displaymetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
height = displaymetrics.heightPixels;
width = displaymetrics.widthPixels;
Log.d("width", ""+width);
Log.d("height", ""+height);
mPageNumber = getArguments().getInt(ARG_PAGE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Resources res = getResources() ;
ViewGroup rootView = (ViewGroup) inflater
.inflate(R.layout.image_view_layout, container, false);
Bitmap btm=decodeSampledBitmapFromResource(res,pics[count-mPageNumber],600,600);
img=(ImageView) rootView.findViewById(R.id.img_pager);
img.setImageBitmap(btm);
return rootView;
}
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
@Override
public void onDetach(){
super.onDetach();
Bitmap bitmap = ((BitmapDrawable)img.getDrawable()).getBitmap();
bitmap.recycle();
//FragmentManager fragmentManager = getFragmentManager();
//FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//fragmentTransaction.remove(this).commit();
}
/**
* Returns the page number represented by this fragment object.
*/
public int getPageNumber() {
return mPageNumber;
}
}
我可以比以前刷卡更多,但问题仍然存在...... 有没有办法查看我的应用程序的内存的可用空间?请有人有解决方案......
答案 0 :(得分:5)
我不得不补充道:
机器人:largeHeap = “真”
到我的应用程序清单!!
答案 1 :(得分:0)
你得到这个错误,因为你没有有效地显示位图(顺便说一句,记住logcat不是可选的,你总是发布它)。
我建议您自己阅读并实施this official guide。
答案 2 :(得分:0)
如果您只想在apl中使用图片,请尝试使用此代码
public class Animals extends Activity{
private static final String POS = "PosAnimals";
private static ViewPager viewPager;
private SharedPreferences mySharedPreferences;
private Context context;
boolean flag;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
AnotherAnotherOneAdapter pagerAdapter = new AnotherAnotherOneAdapter();
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(pagerAdapter);
viewPager.setCurrentItem(0);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
params.screenBrightness = -1f;
getWindow().setAttributes(params);
}
public void onResume() {
super.onResume();
context = getApplicationContext();
mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
viewPager.setCurrentItem(mySharedPreferences.getInt(POS, 0));
}
public void onPause(){
context = getApplicationContext();
mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
Editor editor = mySharedPreferences.edit();
editor.putInt(POS, viewPager.getCurrentItem());
editor.commit();
super.onPause();
}
public class AnotherAnotherOneAdapter extends PagerAdapter {
private int[] mImages = new int[] {
R.drawable.anim1,
R.drawable.anim2,
R.drawable.anim3,
R.drawable.anim4,
R.drawable.anim5,
R.drawable.anim6,
R.drawable.anim7,
R.drawable.anim8,
R.drawable.anim9,
R.drawable.anim10,
R.drawable.anim11,
R.drawable.anim12,
R.drawable.anim13,
R.drawable.anim14
};
public Object instantiateItem(View collection, int position){
Context context = Animals.this;
ImageView imageView = new ImageView(context);
imageView.setImageResource(mImages[position]);
imageView.setScaleType(ScaleType.FIT_XY);
((ViewPager) collection).addView(imageView, 0);
return imageView;
}
public void destroyItem(View collection, int position, Object view){
((ViewPager) collection).removeView((ImageView) view);
}
public int getCount(){
return mImages.length;
}
public boolean isViewFromObject(View view, Object object){
return view == ((ImageView) object);
}
public void finishUpdate(View arg0){
}
public void restoreState(Parcelable arg0, ClassLoader arg1){
}
public Parcelable saveState(){
return null;
}
public void startUpdate(View arg0){
}
}
}
此活动的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
我建议使用图像高度不超过750像素