gpt4 book ai didi

java - 内存不足错误 - 优化?

转载 作者:行者123 更新时间:2023-12-01 09:18:40 25 4
gpt4 key购买 nike

当我尝试导航到其他 Activity 时,我的应用程序崩溃了。我相信这是由于我的绘图中的图片数量和我的应用程序设置所致。我设置了 ListView 和 ArrayAdapter,所以我不确定为什么它使用这么多内存。有什么想法吗?

10-29 20:56:52.287 18391-18391/? E/AndroidRuntime:致命异常:main进程:com.example.android.tourguide,PID:18391java.lang.OutOfMemoryError:无法分配具有 16777216 可用字节和 24MB 的 366378124 字节分配,直到 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);
}

}

fragment

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);
}

}
}

最佳答案

尝试一下,在您的drawable文件夹所在的位置(app->src->main->main->res),创建一个名为drawable-nodpi的文件夹,把你所有的图片放在那里。让我知道它是否适合您

关于java - 内存不足错误 - 优化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40325202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com