2016-10-17 2 views
0
10-16 21:49:03.318 29852-29852/com.klau.jobsinaustralia I/dalvikvm-heap: Grow heap (frag case) to 171.874MB for 14400016-byte allocation 
10-16 21:49:03.578 29852-29852/com.klau.jobsinaustralia I/dalvikvm-heap: Grow heap (frag case) to 175.314MB for 14400016-byte allocation 
10-16 21:49:03.689 29852-29852/com.klau.jobsinaustralia I/dalvikvm-heap: Grow heap (frag case) to 177.603MB for 3600016-byte allocation 
10-16 21:49:03.839 29852-29852/com.klau.jobsinaustralia I/dalvikvm-heap: Grow heap (frag case) to 178.716MB for 14364016-byte allocation 
10-16 21:49:04.029 29852-29852/com.klau.jobsinaustralia I/dalvikvm-heap: Grow heap (frag case) to 182.185MB for 14400016-byte allocation 
10-16 21:49:04.139 29852-29852/com.klau.jobsinaustralia I/dalvikvm-heap: Forcing collection of SoftReferences for 3600016-byte allocation 
10-16 21:49:04.199 29852-29852/com.klau.jobsinaustralia E/dalvikvm-heap: Out of memory on a 3600016-byte allocation. 
10-16 21:49:04.199 29852-29852/com.klau.jobsinaustralia I/dalvikvm: "main" prio=5 tid=1 RUNNABLE 
10-16 21:49:08.223 29852-29852/com.klau.jobsinaustralia E/AndroidRuntime: FATAL EXCEPTION: main 
java.lang.OutOfMemoryError: (Heap Size=194912KB, Allocated=182627KB) 
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:628) 
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:475) 
at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:504) 
at com.klau.jobsinaustralia.scrollable.CustomList.decodeSampledBitmapFromResource 

Aktivität"OutOfMemoryError" Wenn Bilder in der Listview Scrollen

public class ExtraInfoActivity extends BaseActivity { 

ListView list; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); 
    final String languageToLoad = sharedPreferences.getString("PREF_LIST", "no selection"); 
    Locale locale = new Locale(languageToLoad); 
    Locale.setDefault(locale); 
    Configuration config = new Configuration(); 
    config.locale = locale; 
    getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics()); 

    setContentView(R.layout.activity_extra_info); 
    String[] nsw_attraction = { 
      "Sydney Bridge", 
      "Blue Mountains", 
      "Bondi Beach", 
      "The Hunter Valley", 
      "The Jenolan Caves" 
    }; 


    final String[] nsw_web = { 
      getResources().getString(R.string.nsw_attraction_1), 
      getResources().getString(R.string.nsw_attraction_2), 
      getResources().getString(R.string.nsw_attraction_3), 
      getResources().getString(R.string.nsw_attraction_4), 
      getResources().getString(R.string.nsw_attraction_5) 
    }; 

    Integer[] nswImageId = { 
      R.drawable.nsw_attraction_bridge, 
      R.drawable.nsw_attraction_blue_mountains, 
      R.drawable.nsw_attraction_bondi_beach, 
      R.drawable.nsw_attraction_hunter_valley, 
      R.drawable.nsw_attraction_jenolan_caves 
    }; 
    CustomList adapter = new CustomList(ExtraInfoActivity.this, nsw_attraction, nsw_web, nswImageId); 
    list = (ListView) findViewById(R.id.list); 
    list.setAdapter(adapter); 
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {   
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       Toast.makeText(ExtraInfoActivity.this, nsw_web[+position], Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 

@Override 
public void onStart() { 
    super.onStart(); 
    try { 
     mTracker.setScreenName("Extra Infomation"); 
     mTracker.send(new HitBuilders.AppViewBuilder().build()); 
    }catch (Exception ex){ 
     ex.printStackTrace(); 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_home, menu); 
    return true; 
} 

}

+0

Sorry, vergessen zu geschlossenen –

+0

die Klasse Custom Ihre Bildgröße überprüfen? Die meiste Zeit Fehler kommt bcoz der großen Bildgröße .... – sushildlh

Antwort

0

Sie benötigen eine Bibliothek verwenden, um die Bilder in der ListView zu laden.

Glide ist eine erstaunliche Bibliothek, um die Bilder zu laden.

seine Einrichtung ist sehr einfach:

diese beiden

in Ihrem build.gradle setzen
dependencies { 
    compile 'com.github.bumptech.glide:glide:3.7.0' 
    compile 'com.android.support:support-v4:19.1.0' 
} 

und dann in Ihrem Adapter alles, was Sie tun müssen, ist:

Glide 
    .with(context) 
    .load(nswImageId[position]) 
    .into(myImageView); 

es Das ist Es wird sich um alles kümmern. Weitere Informationen und Verwendungsdetails finden Sie unter here.

+0

Vielen Dank für Ihre Hilfe. Ja. Das ist sehr einfach, um das Bild mit Glide zu verwalten. Lerne etwas von dir heute. –

0

Ich habe bereits verkleinert das Bild. Klasse meine Custom unter

public class Custom erweitert ArrayAdapter {

private final Activity context; 
private final String[] placeName; 
private final String[] web; 
private final Integer[] imageId; 

public CustomList(Activity context, String[] placeName, String[] web, Integer[] imageId) { 
    super(context, R.layout.list_single, web); 
    this.context = context; 
    this.placeName = placeName; 
    this.web = web; 
    this.imageId = imageId; 

} 
@Override 
public View getView(int position, View view, ViewGroup parent) { 

    LayoutInflater inflater = context.getLayoutInflater(); 
    View rowView= inflater.inflate(R.layout.list_single, null, true); 


    TextView place = (TextView) rowView.findViewById(R.id.place_name); 
    place.setText(placeName[position]); 
    TextView txtTitle = (TextView) rowView.findViewById(R.id.txt); 
    txtTitle.setText(web[position]); 

    ImageView imageView = (ImageView) rowView.findViewById(R.id.img); 

    Bitmap bitmap = decodeSampledBitmapFromResource(context.getResources(), imageId[position], 150, 150); 
    if (bitmap != null) { 
     bitmap.recycle(); //<====== ERROR here: java.lang.IllegalArgumentException: Cannot draw recycled bitmap. 
    } 
    imageView.setImageBitmap(bitmap); 
    return rowView; 
} 

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) { 

     final int halfHeight = height/2; 
     final int halfWidth = width/2; 

     // Calculate the largest inSampleSize value that is a power of 2 and keeps both 
     // height and width larger than the requested height and width. 
     while ((halfHeight/inSampleSize) >= reqHeight 
       && (halfWidth/inSampleSize) >= reqWidth) { 
      inSampleSize *= 2; 
     } 
    } 

    return inSampleSize; 
} 

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, 
                int reqWidth, int reqHeight) { 

    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeResource(res, resId, options); 
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 
    options.inJustDecodeBounds = false; 

    return BitmapFactory.decodeResource(res, resId, options); 

} 

}

0

vielen dank für eine schnelle antwort. Ich bin mir nicht sicher, ob ich das richtig machen kann. Bitte überprüfen Sie meine Änderungen.

@Override 
public View getView(int position, View view, ViewGroup parent) { 
    final ImageView myImageView; 

    LayoutInflater inflater = context.getLayoutInflater(); 
    View rowView= inflater.inflate(R.layout.list_single, null, true); 

    TextView place = (TextView) rowView.findViewById(R.id.place_name); 
    place.setText(placeName[position]); 
    TextView txtTitle = (TextView) rowView.findViewById(R.id.txt); 
    txtTitle.setText(web[position]); 

    ImageView imageView = (ImageView) rowView.findViewById(R.id.img); 

    if (view == null) { 
     myImageView = (ImageView) inflater.inflate(imageView, parent, false); 
    } else { 
     myImageView = (ImageView) recycled; 
    } 


    Glide 
     .with(context) 
     .load(nswImageId[position]) 
     .into(myImageView);  

    return myImageView; 
} 
0

Schreib diese Linie in der Anwendung Tag in Ihrer Manifest-Datei

android:largeHeap="true"