- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我从以下代码中得到内存泄漏。它是一个包含 mainViewPager 的 Activity 。该 viewPager 返回 4 个不同的 fragment ,每个 fragment 都有自己的 ViewPager,其中包含从同一 asyncTask 检索的图像。这是我的代码。当我运行它时,我可以滚动浏览页面,但会有一些延迟。但是,当我退出并返回 Activity 时,出现内存不足错误。所以肯定是内存泄漏。我一直盯着我的代码看,但我不明白为什么。请帮助我... PS。我正在使用 Picasso 库从服务器获取图像。
Activity
package info.nightowl.nightowl;
import info.nightowl.nightowl.LocationsGridFragment.onLocationSelectedListener;
public class NightWatch extends FragmentActivity implements onLocationSelectedListener {
public static ArrayList<String> imageUrl = new ArrayList<String>();
public static ArrayList<String> dateList = new ArrayList<String>();
public static ArrayList<String> trendList = new ArrayList<String>();
public static ArrayList<String> nameList = new ArrayList<String>();
public static ArrayList<String> imageLocationsList = new ArrayList<String>();
public static ArrayList<String> imageClubsList = new ArrayList<String>();
public static ArrayList<String> locationsList = new ArrayList<String>();
public static ArrayList<String> clubsList = new ArrayList<String>();
public static ArrayList<String> locationImagesUrl = new ArrayList<String>();
public static ArrayList<String> addressList = new ArrayList<String>();
public static ArrayList<String> hotImageUrl = new ArrayList<String>();
public static ArrayList<String> hotDateList = new ArrayList<String>();
public static ArrayList<String> hotTrendList = new ArrayList<String>();
public static ArrayList<String> hotNameList = new ArrayList<String>();
public static ArrayList<String> hotImageLocationsList = new ArrayList<String>();
public static ArrayList<String> hotImageClubsList = new ArrayList<String>();
FetchClubs fetchClubs = null;
public int width, height;
public JSONArray list;
public final String IMAGE_URL = "http://www.night-owl.info/webservice/";
SharedPreferences prefs;
static FeedFragment feedFragment;
static LocationsFragment locationsFragment;
static HotFragment hotFragment;
static ClubsFragment clubsFragment;
CustomViewPager customPager;
CustomViewPageAdapter theAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_night_watch);
//Method to set up the tabs
getSizes();
setupFragments();
updateList();
prefs = getSharedPreferences("prefs", 0);
}
//Called from the fragment which fetches image
public void loadBitmap(String image, ImageView imageView, boolean grid, TextView loadText) {
int size;
if (grid) size = (width-width/9)/3;
else size = width - width/9;
Log.d("Pager About to load", "Pager Loading bitmap picasso");
getBitmap load = new getBitmap(image, imageView, size, loadText);
load.execute(image);
}
public class getBitmap extends AsyncTask<String, Void, Bitmap> {
String loadImageUrl;
Bitmap bmp;
int sizes;
TextView loadingTextView;
private final WeakReference<ImageView> imageViewReference;
public getBitmap(String image, ImageView imageView, int size, TextView loadText){
imageViewReference = new WeakReference<ImageView>(imageView);
sizes = size;
loadingTextView = loadText;
}
@Override
protected void onPreExecute() {
Log.d("async", "async starting");
super.onPreExecute();
}
@Override
protected Bitmap doInBackground(String... arg0) {
// TODO Auto-generated method stub
try {
bmp = Picasso.with(NightWatch.this).load(IMAGE_URL+arg0[0]).resize(sizes,
sizes).get();
} catch (IOException e) {
Log.d("ioexc", "Pager IOEXCEPTION IDIOT!!!");
e.printStackTrace();
}
return bmp;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
/*RoundedCornersDrawable drawable = new RoundedCornersDrawable(getResources(), bmp);
loadImage.setImageDrawable(drawable);
loadingTextView.setVisibility(View.GONE);
loadImage.setVisibility(View.VISIBLE);*/
if(imageViewReference != null && bitmap!= null) {
final ImageView imageView = imageViewReference.get();
if(imageView != null) {
imageView.setImageBitmap(bitmap);
loadingTextView.setVisibility(View.GONE);
imageView.setVisibility(View.VISIBLE);
}
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.feed, menu);
return true;
}
//SET UP THE TWO TABS 'FEED' AND 'LOCATIONS'
//Update ArrayList of the image names
public void updateList() {
imageUrl.clear();
trendList.clear();
dateList.clear();
nameList.clear();
imageLocationsList.clear();
imageClubsList.clear();
locationsList.clear();
clubsList.clear();
locationImagesUrl.clear();
addressList.clear();
hotImageUrl.clear();
hotTrendList.clear();
hotDateList.clear();
hotNameList.clear();
hotImageLocationsList.clear();
hotImageClubsList.clear();
new getImageUrl().execute();
new getLocationsUrl().execute();
}
class getImageUrl extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... arg0) {
JSONParser parser = new JSONParser();
JSONObject json = parser.getJSONFromUrl(IMAGE_URL + "updateimages.php");
try {
list = json.getJSONArray("posts");
for(int i = 0; i < list.length(); i++) {
JSONObject c = list.getJSONObject(i);
String imgSrc = c.getString("source");
String trend = c.getString("trend");
String date = c.getString("date");
String name = c.getString("name");
String location = c.getString("location");
String club = c.getString("club");
imageUrl.add(imgSrc);
dateList.add(date);
trendList.add(trend);
nameList.add(name);
imageLocationsList.add(location);
imageClubsList.add(club);
Log.d("async trend", trendList.get(0));
}
Log.d("Got list", imageUrl.get(0) + " " + trendList.get(0));
} catch(JSONException je) {
je.printStackTrace();
}
try {
list = json.getJSONArray("hot");
for(int i = 0; i < list.length(); i++) {
JSONObject c = list.getJSONObject(i);
String imgSrc = c.getString("source");
String trend = c.getString("trend");
String date = c.getString("date");
String name = c.getString("name");
String location = c.getString("location");
String club = c.getString("club");
hotImageUrl.add(imgSrc);
hotDateList.add(date);
hotTrendList.add(trend);
hotNameList.add(name);
hotImageLocationsList.add(location);
hotImageClubsList.add(club);
Log.d("async trend", trendList.get(0));
}
Log.d("Got list","hot list" + hotImageUrl.get(0) + " " + hotTrendList.get(0));
} catch(JSONException je) {
je.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
hotFragment.updateAdapter();
feedFragment.updateAdapter();
}
}
public void scrollMainPager(int pos) {
customPager.setCurrentItem(pos, true);
}
public void scrollPager(int pos) {
feedFragment.scrollPager(pos);
}
public void setupFragments() {
feedFragment = new FeedFragment();
locationsFragment = new LocationsFragment();
hotFragment = new HotFragment();
clubsFragment = new ClubsFragment();
customPager = (CustomViewPager) findViewById(R.id.customviewpager);
customPager.setOffscreenPageLimit(3);
theAdapter = new CustomViewPageAdapter(getSupportFragmentManager());
customPager.setAdapter(theAdapter);
customPager.setCurrentItem(1);
}
//GET SIZES OF SCREEN
public void getSizes(){
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
}
public static class CustomViewPageAdapter extends FragmentStatePagerAdapter {
public CustomViewPageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int arg0) {
if(arg0 == 0) return hotFragment;
else if(arg0 == 1) return feedFragment;
else if(arg0 == 2) return locationsFragment;
else if(arg0 == 3) return clubsFragment;
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 4;
}
}
@Override
public void onLocationSelected(int position) {
}
}
提要 fragment
public class FeedFragment extends Fragment implements OnClickListener {
ToggleButton buttonGrid;
static Spinner spinnerUploadStatus;
CustomEditText et;
CustomScrollTextView tv;
int width, height;
View view;
static boolean isGrid;
ViewPager mPager;
SharedPreferences prefs;
public ImagePagerAdapter mPageAdapter;
Button buttonSwitchFragment;
Integer[] b = { R.drawable.statusunpressed, R.drawable.red,
R.drawable.green, R.drawable.blue };
List<Integer> spinnerList = new ArrayList<Integer>(Arrays.asList(b));
CustomSpinnerAdapter myAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
prefs = getActivity().getSharedPreferences("prefs", 0);
getSizes();
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_feed, container, false);
setup();
return view;
}
@SuppressLint("NewApi")
public void setup() {
isGrid = false;
mPager = (ViewPager) view.findViewById(R.id.myviewpager1);
mPager.setOffscreenPageLimit(0);
buttonGrid = (ToggleButton) view.findViewById(R.id.button_grid);
buttonGrid.setOnClickListener(this);
spinnerUploadStatus = (Spinner) view
.findViewById(R.id.buttonUploadStatus);
myAdapter = new CustomSpinnerAdapter(getActivity(),
R.layout.item_simple_list_item, spinnerList);
myAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerUploadStatus.setAdapter(myAdapter);
spinnerUploadStatus.setDropDownWidth((int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 50, getResources()
.getDisplayMetrics()));
spinnerUploadStatus.setOnItemSelectedListener(myOnItemSelectedListener);
spinnerUploadStatus.getBackground().setAlpha(0);
buttonSwitchFragment = (Button) view
.findViewById(R.id.buttonSwitchFragment);
buttonSwitchFragment.setOnClickListener(this);
Typeface font = Typeface.createFromAsset(getActivity().getAssets(),
"NotCourierSans.ttf");
et = (CustomEditText) view.findViewById(R.id.customEditText1);
et.updateTypeFace(font);
tv = (CustomScrollTextView) view
.findViewById(R.id.customscrolltextview);
tv.setText("WELCOME TO NIGHTOWL");
tv.setTypeface(font);
tv.start(width / 2500.0);
Log.d("width", "width is " + width);
}
public void getSizes() {
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_grid:
isGrid = !isGrid;
updateAdapter();
break;
case R.id.buttonSwitchFragment:
((NightWatch) getActivity()).scrollMainPager(2);
break;
}
}
public OnItemSelectedListener myOnItemSelectedListener = new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
int colour = arg2;
spinnerUploadStatus.setSelection(0, true);
et.sendText(prefs.getString("username", "username invalid"), colour);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
};
public void scrollPager(int position) {
isGrid = false;
buttonGrid.setChecked(false);
updateAdapter();
mPager.setCurrentItem(position, true);
Log.d("Pager", "PAGER UPDATED");
}
public static void disableButton(Boolean enabled) {
spinnerUploadStatus.setEnabled(enabled);
}
public void updateAdapter() {
int theSize;
if (!isGrid)
theSize = NightWatch.imageUrl.size();
else if (NightWatch.imageUrl.size() % 9 > 0)
theSize = NightWatch.imageUrl.size() / 9 + 1;
else
theSize = NightWatch.imageUrl.size() / 9;
mPageAdapter = new ImagePagerAdapter(getChildFragmentManager(), theSize);
mPager.setAdapter(mPageAdapter);
Log.d("size of viewPager Adapter", "" + theSize);
}
public static class ImagePagerAdapter extends FragmentStatePagerAdapter {
int mSize;
public ImagePagerAdapter(FragmentManager fm, int size) {
super(fm);
mSize = size;
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
Log.d("Starting imageDetailFragment", "");
Log.d("Pager", "Pager starting fragment");
Log.d("GRID", "fuck grid");
if (!isGrid)
return ImageDetailFragment.newInstance(
arg0, NightWatch.imageUrl, NightWatch.nameList,
NightWatch.trendList, NightWatch.dateList);
else
return GridFragment.newInstance(arg0, NightWatch.imageUrl);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mSize;
}
}
public class CustomSpinnerAdapter extends ArrayAdapter<Integer> {
List<Integer> resourceId;
Context c;
public CustomSpinnerAdapter(Context context, int textViewResourceId,
List<Integer> objects) {
super(context, textViewResourceId, objects);
resourceId = objects;
c = context;
}
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
return getCustomDropdownView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView,
ViewGroup parent) {
ImageView imageView = new ImageView(c);
imageView.setImageResource(resourceId.get(position));
imageView.setLayoutParams(new ListView.LayoutParams(
(int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 50, getResources()
.getDisplayMetrics()), (int) TypedValue
.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 34,
getResources().getDisplayMetrics())));
return imageView;
}
public View getCustomDropdownView(int position, View convertView,
ViewGroup parent) {
ImageView imageView = new ImageView(c);
imageView.setImageResource(resourceId.get(position));
imageView.setLayoutParams(new ListView.LayoutParams(
(int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 50, getResources()
.getDisplayMetrics()), (int) TypedValue
.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 34,
getResources().getDisplayMetrics())));
if (position == 0) {
imageView.setVisibility(View.GONE);
}
return imageView;
}
}
}
图像细节 fragment
package info.nightowl.nightowl;
import java.io.IOException;
import java.util.ArrayList;
import com.example.nightowl.R;
import com.squareup.picasso.Picasso;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class ImageDetailFragment extends Fragment implements OnClickListener {
int mImageNum;
ImageView imageView;
TextView trendTextView;
TextView nameTextView;
TextView dateTextView;
TextView loadingTextView;
Button upVoteButton, downVoteButton;
final String list = "voteList";
static SharedPreferences prefs;
SharedPreferences.Editor editor;
public ArrayList<String> imageUrl;
public ArrayList<String> trendList;
public ArrayList<String> nameList;
public ArrayList<String> dateList;
RelativeLayout.LayoutParams rParams;
int width, height;
static ImageDetailFragment newInstance(int imageNum,
ArrayList<String> imageList, ArrayList<String> tempNameList,
ArrayList<String> tempTrendList, ArrayList<String> tempDateList) {
final ImageDetailFragment f = new ImageDetailFragment();
Bundle args = new Bundle();
args.putInt("imageNum", imageNum);
args.putStringArrayList("imageList", imageList);
args.putStringArrayList("nameList", tempNameList);
args.putStringArrayList("trendList", tempTrendList);
args.putStringArrayList("dateList", tempDateList);
f.setArguments(args);
return f;
}
public ImageDetailFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mImageNum = getArguments().getInt("imageNum");
imageUrl = getArguments().getStringArrayList("imageList");
nameList = getArguments().getStringArrayList("nameList");
trendList = getArguments().getStringArrayList("trendList");
dateList = getArguments().getStringArrayList("dateList");
} else
mImageNum = -1;
prefs = getActivity().getSharedPreferences("prefs", 0);
editor = getActivity().getSharedPreferences("prefs", 0).edit();
getSizes();
rParams = new RelativeLayout.LayoutParams((width - width/9), width - width/9);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.image_detail_fragment,
container, false);
imageView = (ImageView) v.findViewById(R.id.imageView1);
imageView.setVisibility(View.INVISIBLE);
loadingTextView = (TextView) v.findViewById(R.id.loadingTextView);
loadingTextView.setVisibility(View.VISIBLE);
trendTextView = (TextView) v.findViewById(R.id.trendTextView);
nameTextView = (TextView) v.findViewById(R.id.nameTextView);
dateTextView = (TextView) v.findViewById(R.id.dateTextView);
upVoteButton = (Button) v.findViewById(R.id.buttonUpVote);
downVoteButton = (Button) v.findViewById(R.id.buttonDownVote);
rParams.addRule(RelativeLayout.BELOW, nameTextView.getId());
rParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
imageView.setLayoutParams(rParams);
upVoteButton.setOnClickListener(this);
downVoteButton.setOnClickListener(this);
if (NightWatch.class.isInstance(getActivity())) {
Typeface font = Typeface.createFromAsset(getActivity().getAssets(),
"NotCourierSans.ttf");
final String image = imageUrl.get(mImageNum);
((NightWatch) getActivity()).loadBitmap(image, imageView, false, loadingTextView);
trendTextView.setText(trendList.get(mImageNum));
nameTextView.setText(nameList.get(mImageNum));
dateTextView.setText(dateList.get(mImageNum));
updateButtonBackgrounds();
loadingTextView.setTypeface(font);
trendTextView.setTypeface(font);
nameTextView.setTypeface(font);
dateTextView.setTypeface(font);
Log.d("fragment trend", NightWatch.trendList.get(0));
}
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonUpVote:
if (prefs.getInt(imageUrl.get(mImageNum), 0) == -1) {
downVoteButton
.setBackgroundResource(R.drawable.downvoteunpressed);
upVoteButton.setBackgroundResource(R.drawable.upvotepressed);
((NightWatch) getActivity()).sendVote(1,
imageUrl.get(mImageNum));
} else if (prefs.getInt(imageUrl.get(mImageNum), 0) == 0) {
upVoteButton.setBackgroundResource(R.drawable.upvotepressed);
((NightWatch) getActivity()).sendVote(1,
imageUrl.get(mImageNum));
}
break;
case R.id.buttonDownVote:
if (prefs.getInt(imageUrl.get(mImageNum), 0) == 1) {
downVoteButton
.setBackgroundResource(R.drawable.downvotepressed);
upVoteButton.setBackgroundResource(R.drawable.upvoteunpressed);
((NightWatch) getActivity()).sendVote(-1,
imageUrl.get(mImageNum));
} else if (prefs.getInt(imageUrl.get(mImageNum), 0) == 0) {
downVoteButton
.setBackgroundResource(R.drawable.downvotepressed);
((NightWatch) getActivity()).sendVote(-1,
imageUrl.get(mImageNum));
}
break;
}
};
public void updateButtonBackgrounds() {
if (prefs.getInt(imageUrl.get(mImageNum), 0) == -1) {
downVoteButton.setBackgroundResource(R.drawable.downvotepressed);
} else if (prefs.getInt(imageUrl.get(mImageNum), 0) == 1) {
upVoteButton.setBackgroundResource(R.drawable.upvotepressed);
}
}
public static void sendVote(int vote, String imageName) {
prefs.edit().putInt(imageName, vote).commit();
}
public void getSizes() {
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
}
}
热 fragment
package info.nightowl.nightowl;
public class HotFragment extends Fragment implements OnClickListener {
ToggleButton buttonGrid;
static Spinner spinnerUploadStatus;
CustomEditText et;
CustomScrollTextView tv;
int width, height;
View view;
static boolean isGrid;
ViewPager mPager;
SharedPreferences prefs;
public ImagePagerAdapter mPageAdapter;
Button buttonSwitchFragment;
Integer[] b = { R.drawable.statusunpressed, R.drawable.red,
R.drawable.green, R.drawable.blue };
List<Integer> spinnerList = new ArrayList<Integer>(Arrays.asList(b));
CustomSpinnerAdapter myAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
prefs = getActivity().getSharedPreferences("prefs", 0);
getSizes();
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_feed, container, false);
setup();
return view;
}
@SuppressLint("NewApi")
public void setup() {
isGrid = false;
mPager = (ViewPager) view.findViewById(R.id.myviewpager1);
mPager.setOffscreenPageLimit(0);
buttonGrid = (ToggleButton) view.findViewById(R.id.button_grid);
buttonGrid.setOnClickListener(this);
spinnerUploadStatus = (Spinner) view
.findViewById(R.id.buttonUploadStatus);
myAdapter = new CustomSpinnerAdapter(getActivity(),
R.layout.item_simple_list_item, spinnerList);
myAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerUploadStatus.setAdapter(myAdapter);
spinnerUploadStatus.setDropDownWidth((int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 50, getResources()
.getDisplayMetrics()));
spinnerUploadStatus.setOnItemSelectedListener(myOnItemSelectedListener);
spinnerUploadStatus.getBackground().setAlpha(0);
buttonSwitchFragment = (Button) view
.findViewById(R.id.buttonSwitchFragment);
buttonSwitchFragment.setOnClickListener(this);
Typeface font = Typeface.createFromAsset(getActivity().getAssets(),
"NotCourierSans.ttf");
et = (CustomEditText) view.findViewById(R.id.customEditText1);
et.updateTypeFace(font);
tv = (CustomScrollTextView) view
.findViewById(R.id.customscrolltextview);
tv.setText("WELCOME TO NIGHTOWL");
tv.setTypeface(font);
tv.start(width / 2500.0);
Log.d("width", "width is " + width);
}
public void getSizes() {
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_grid:
isGrid = !isGrid;
updateAdapter();
break;
case R.id.buttonSwitchFragment:
((NightWatch) getActivity()).scrollMainPager(1);
break;
}
}
public OnItemSelectedListener myOnItemSelectedListener = new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
spinnerUploadStatus.setSelection(0, true);
et.sendText(prefs.getString("username", "username invalid"), arg2);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
};
public void scrollPager(int position) {
isGrid = false;
buttonGrid.setChecked(false);
updateAdapter();
mPager.setCurrentItem(position, true);
Log.d("Pager", "PAGER UPDATED");
}
public static void disableButton(Boolean enabled) {
spinnerUploadStatus.setEnabled(enabled);
}
public void updateAdapter() {
int theSize;
if (!isGrid)
theSize = NightWatch.imageUrl.size();
else if (NightWatch.imageUrl.size() % 9 > 0)
theSize = NightWatch.imageUrl.size() / 9 + 1;
else
theSize = NightWatch.imageUrl.size() / 9;
mPageAdapter = new ImagePagerAdapter(getChildFragmentManager(), theSize);
mPager.setAdapter(mPageAdapter);
Log.d("size of viewPager Adapter", "" + theSize);
}
public static class ImagePagerAdapter extends FragmentStatePagerAdapter {
int mSize;
public ImagePagerAdapter(FragmentManager fm, int size) {
super(fm);
mSize = size;
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
Log.d("Starting imageDetailFragment", "");
Log.d("Pager", "Pager starting fragment");
Log.d("GRID", "fuck grid");
if (!isGrid)
return ImageDetailFragment.newInstance(arg0,
NightWatch.hotImageUrl, NightWatch.hotNameList,
NightWatch.hotTrendList, NightWatch.hotDateList);
else
return GridFragment.newInstance(arg0, NightWatch.imageUrl);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mSize;
}
}
public class CustomSpinnerAdapter extends ArrayAdapter<Integer> {
List<Integer> resourceId;
Context c;
public CustomSpinnerAdapter(Context context, int textViewResourceId,
List<Integer> objects) {
super(context, textViewResourceId, objects);
resourceId = objects;
c = context;
}
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
return getCustomDropdownView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView,
ViewGroup parent) {
ImageView imageView = new ImageView(c);
imageView.setImageResource(resourceId.get(position));
imageView.setLayoutParams(new ListView.LayoutParams(
(int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 50, getResources()
.getDisplayMetrics()), (int) TypedValue
.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 34,
getResources().getDisplayMetrics())));
return imageView;
}
public View getCustomDropdownView(int position, View convertView,
ViewGroup parent) {
ImageView imageView = new ImageView(c);
imageView.setImageResource(resourceId.get(position));
imageView.setLayoutParams(new ListView.LayoutParams(
(int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 50, getResources()
.getDisplayMetrics()), (int) TypedValue
.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 34,
getResources().getDisplayMetrics())));
if (position == 0) {
imageView.setVisibility(View.GONE);
}
return imageView;
}
}
}
我看不出发布其余 fragment 有什么意义。我确定内存泄漏就在这里如果有人帮助我,我将非常感激。
最佳答案
以这种方式从 URL 加载位图将导致 IOException。使用 Picasso,您甚至不需要 asynctask。只需在 UI 线程中调用它:
Picasso.with(getActivity())
.load(url)
.into(imageView); //you can add .resize() before .into()
或者如果你真的想要位图,你可以使用这个代码:
new AsyncTask<Void, Void, Void>() {
Bitmap bitmap;
@Override
protected void onPreExecute() {
//some code
}
@Override
protected Void doInBackground(Void... arg0) {
bitmap = getBitmapFromURL(url);
return null;
}
@Override
protected void onPostExecute(Void result) {
imageView.setImageBitmap(bitmap);
}
}.execute();
和getBitmapFromURL():
public Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
return BitmapFactory.decodeStream(input);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
关于安卓。内存泄漏导致内存不足错误。使用查看寻呼机。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26665489/
我在具有 2CPU 和 3.75GB 内存 (https://aws.amazon.com/ec2/instance-types/) 的 c3.large Amazon EC2 ubuntu 机器上运
我想通过用户空间中的mmap-ing并将地址发送到内核空间从用户空间写入VGA内存(视频内存,而不是缓冲区),我将使用pfn remap将这些mmap-ed地址映射到vga内存(我将通过 lspci
在 Mathematica 中,如果你想让一个函数记住它的值,它在语法上是很轻松的。例如,这是标准示例 - 斐波那契: fib[1] = 1 fib[2] = 1 fib[n_]:= fib[n] =
我读到动态内存是在运行时在堆上分配的,而静态内存是在编译时在堆栈上分配的,因为编译器知道在编译时必须分配多少内存。 考虑以下代码: int n; cin>>n; int a[n]; 如果仅在运行期间读
我是 Python 的新手,但我之前还不知道这一点。我在 for 循环中有一个基本程序,它从站点请求数据并将其保存到文本文件但是当我检查我的任务管理器时,我发现内存使用量只增加了?长时间运行时,这对我
我正在设计一组数学函数并在 CPU 和 GPU(使用 CUDA)版本中实现它们。 其中一些函数基于查找表。大多数表占用 4KB,其中一些占用更多。基于查找表的函数接受一个输入,选择查找表的一两个条目,
读入一个文件,内存被动态分配给一个字符串,文件内容将被放置在这里。这是在函数内部完成的,字符串作为 char **str 传递。 使用 gdb 我发现在行 **(str+i) = fgetc(aFil
我需要证实一个理论。我正在学习 JSP/Java。 在查看了一个现有的应用程序(我没有写)之后,我注意到一些我认为导致我们的性能问题的东西。或者至少是其中的一部分。 它是这样工作的: 1)用户打开搜索
n我想使用memoization缓存某些昂贵操作的结果,这样就不会一遍又一遍地计算它们。 两个memoise和 R.cache适合我的需要。但是,我发现缓存在调用之间并不可靠。 这是一个演示我看到的问
我目前正在分析一些 javascript shell 代码。这是该脚本中的一行: function having() { memory = memory; setTimeout("F0
我有一种情况,我想一次查询数据库,然后再将整个数据缓存在内存中。 我得到了内存中 Elasticsearch 的建议,我用谷歌搜索了它是什么,以及如何在自己的 spring boot 应用程序中实现它
我正在研究 Project Euler (http://projecteuler.net/problem=14) 的第 14 题。我正在尝试使用内存功能,以便将给定数字的序列长度保存为部分结果。我正在
所以,我一直在做 Java 内存/注意力游戏作业。我还没有达到我想要的程度,它只完成了一半,但我确实让 GUI 大部分工作了......直到我尝试向我的框架添加单选按钮。我认为问题可能是因为我将 JF
我一直在尝试使用 Flask-Cache 的 memoize 功能来仅返回 statusTS() 的缓存结果,除非在另一个请求中满足特定条件,然后删除缓存。 但它并没有被删除,并且 Jinja 模板仍
我对如何使用 & 运算符来减少内存感到非常困惑。 我可以回答下面的问题吗? clase C{ function B(&$a){ $this->a = &$a; $thi
在编写代码时,我遇到了一个有趣的问题。 我有一个 PersonPOJO,其 name 作为其 String 成员之一及其 getter 和 setter class PersonPOJO { priv
在此代码中 public class Base { int length, breadth, height; Base(int l, int b, int h) { l
Definition Structure padding is the process of aligning data members of the structure in accordance
在 JavaScript Ninja 的 secret 中,作者提出了以下方案,用于在没有闭包的情况下内存函数结果。他们通过利用函数是对象这一事实并在函数上定义一个属性来存储过去调用函数的结果来实现这
我正在尝试找出 map 消耗的 RAM 量。所以,我做了以下事情;- Map cr = crPair.collectAsMap(); // 200+ entries System.out.printl
我是一名优秀的程序员,十分优秀!