当我尝试导航到其他 Activity 时,我的应用程序崩溃。我相信这是由于我的绘画中包含的图片数量以及在应用程序中设置的图片数量所致。我有一个ListView和一个ArrayAdapter设置,所以我不确定为什么要使用这么多的内存。有任何想法吗?
10-29 20:56:52.287 18391-18391 /? E / AndroidRuntime:致命异常:main
流程:com.example.android.tourguide,PID:18391
java.lang.OutOfMemoryError:未能分配366378124字节分配,其中包含16777216可用字节和24MB,直到OOM
这是我的代码:
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;
}
}
信息 Activity
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;
}
}
信息适配器
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;
}
}
分类适配器
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);
}
}
}
最佳答案
尝试此操作,在可绘制文件夹所在的位置(app->src->main->main->res
),创建一个名为drawable-nodpi
的文件夹,然后将所有图像放在此处。
请让我知道这对你有没有用