Мое приложение падает, когда я пытаюсь перейти к другим действиям. Я считаю, что это связано с количеством фотографий, которые у меня есть в моих ресурсах и настройке в моем приложении. У меня есть ListView и ArrayAdapter, поэтому я не уверен, почему он использует так много памяти. Есть идеи?Ошибка памяти - Оптимизация?
10-29 20: 56: 52.287 18391-18391 /? E/AndroidRuntime: FATAL ИСКЛЮЧЕНИЕ: главный процесса: com.example.android.tourguide, PID: 18391 java.lang.OutOfMemoryError: Не удалось не выделить распределение 366378124 байт с 16777216 свободных байтов и 24MB до ООМ
Вот мой код:
Main Activity
package com.example.android.tourguide;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content of the activity to use the activity_main.xml layout file
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
CategoryAdapter adapter = new CategoryAdapter(this, getSupportFragmentManager());
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
}
Фрагмент
package com.example.android.tourguide;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RatingBar;
import java.util.ArrayList;
/**
* A simple {@link Fragment} subclass.
*/
public class RestaurantsFragment extends Fragment {
public RestaurantsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.word_list, container, false);
final ArrayList<Information> words = new ArrayList<Information>();
words.add(new Information(R.drawable.restaurant_1, "Rashid's House", "Description", R.drawable.five_star_rating));
words.add(new Information(R.drawable.restaurant_2, "Moo Moo Cow", "Description", R.drawable.five_star_rating));
words.add(new Information(R.drawable.restaurant_3, "Burger 7", "Description", R.drawable.five_star_rating));
words.add(new Information(R.drawable.restaurant_4, "Mini Horse", "Description", R.drawable.five_star_rating));
words.add(new Information(R.drawable.restaurant_5, "Prime Prime!", "Description", R.drawable.five_star_rating));
words.add(new Information(R.drawable.restaurant_6, "Halal Cart 96", "Description", R.drawable.five_star_rating));
words.add(new Information(R.drawable.restaurant_7, "Half Moon", "Description", R.drawable.five_star_rating));
words.add(new Information(R.drawable.restaurant_8, "Mourice's Steakhouse", "Description", R.drawable.five_star_rating));
words.add(new Information(R.drawable.restaurant_9, "The Rudeboy", "Description", R.drawable.five_star_rating));
words.add(new Information(R.drawable.restaurant_10, "Meowth's Lair", "Description", R.drawable.five_star_rating));
InformationAdapter itemsAdapter = new InformationAdapter(getActivity(), words);
ListView listView = (ListView) rootView.findViewById(R.id.list);
listView.setAdapter(itemsAdapter);
return rootView;
}
}
InformationActivity
package com.example.android.tourguide;
import static android.R.attr.description;
import static com.example.android.tourguide.R.id.place;
/**
* Created by msanli on 10/27/2016.
*/
public class Information {
private int mImageResourceId;
private String mPlace;
private String mDescription;
private int mRatingBarResourceId;
public Information(int imageResourceId, String place, String description, int ratingBarResourceId){
mImageResourceId = imageResourceId;
mPlace = place;
mDescription = description;
mRatingBarResourceId = ratingBarResourceId;
}
public int getImageResourceId() {
return mImageResourceId;
}
public String getPlace(){
return mPlace;
}
public String getDescription(){
return mDescription;
}
public int getRatingBarResourceId(){
return mRatingBarResourceId;
}
}
InformationAdapter
package com.example.android.tourguide;
import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by msanli on 10/27/2016.
*/
public class InformationAdapter extends ArrayAdapter<Information> {
public InformationAdapter(Activity context, ArrayList<Information> words){
super(context, 0, words);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
// Get the {@link Word} object located at this position in the list
Information currentInformation = getItem(position);
ImageView imageImageView = (ImageView) listItemView.findViewById(R.id.image_place);
imageImageView.setImageResource(currentInformation.getImageResourceId());
// Find the TextView in the list_item.xml layout with the ID version_name
TextView nameTextView = (TextView) listItemView.findViewById(R.id.place);
// Get the version name from the current Word object and
// set this text on the name TextView
nameTextView.setText(currentInformation.getPlace());
// Find the TextView in the list_item.xml layout with the ID version_number
TextView descriptionTextView = (TextView) listItemView.findViewById(R.id.description);
// Get the version number from the current Word object and
// set this text on the name TextView
descriptionTextView.setText(currentInformation.getDescription());
ImageView ratingBarImageView = (ImageView) listItemView.findViewById(R.id.ratings_bar);
ratingBarImageView.setImageResource(currentInformation.getRatingBarResourceId());
View textContainer = listItemView.findViewById(R.id.text_container);
return listItemView;
}
}
CategoryAdapter
package com.example.android.tourguide;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
/**
* Created by msanli on 10/29/2016.
*/
public class CategoryAdapter extends FragmentPagerAdapter {
private Context mContext;
public CategoryAdapter(Context context, FragmentManager fm){
super(fm);
mContext = context;
}
@Override
public Fragment getItem(int position) {
if (position == 0) {
return new RestaurantsFragment();
} else if (position == 1) {
return new ParksFragment();
} else if (position == 2) {
return new EntertainmentFragment();
} else {
return new AttractionsFragment();
}
}
@Override
public int getCount() {
return 4;
}
@Override
public CharSequence getPageTitle(int position) {
if (position == 0) {
return mContext.getString(R.string.category_restaurants);
}else if (position == 1){
return mContext.getString(R.string.category_parks);
}else if (position == 2){
return mContext.getString(R.string.category_entertainment);
}else{
return mContext.getString(R.string.category_attractions);
}
}
}
Это слишком много памяти используется. У вас где-то есть утечка памяти, вам придется пройти и отладить. Отправьте некоторый код частей, о которых идет речь. – TheValyreanGroup
add android: LargeHeap = true в теге активности манифеста. –
поделитесь своим кодом –