- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 fragment 来显示图像/页面。我有一个包含所有 fragment 的 Activity(Main)。
package com.example.hscroll.demo;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.ListFragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.crittercism.app.Crittercism;
import com.example.hscroll.customer.BitmapWeakReference;
import com.example.hscroll.customer.PromotionalPriceListAdditionsDataAdaptor;
import com.example.hscroll.customer.PromotionalPriceListDataAdaptor;
import com.example.hscroll.library.imagezoom.ImageViewTouch;
public class MainAct extends FragmentActivity{
private ImageViewTouch mImageView;
MyPagerAdapter mPagerAdapter;
ViewPager viewPager;
static final int NUM_ITEMS = 58;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Crittercism.init(getApplicationContext(), "4f391d0fb09315319d00048d");
PreferenceManager.setDefaultValues(this, R.xml.preference, false);
setContentView(R.layout.viewpager_layout);
mPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
viewPager = (ViewPager)findViewById(R.id.viewpager);
viewPager.setAdapter(mPagerAdapter);
}
public static class MyPagerAdapter extends FragmentStatePagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return NUM_ITEMS;
}
@Override
public Fragment getItem(int position) {
return newInstance(position);
}
}
static Fragment newInstance(int position) {
Fragment data = null;
switch(position){
case 0 :
data = new Fragment1();
break;
case 1 :
data = new FragmentSignupForm();
break;
case 2 :
data = new Fragment2();
break;
case 3 :
data = new Fragment3();
break;
case 4 :
data = new Fragment4();
break;
case 5:
data = new Fragment5();
break;
case 6 :
data = new Fragment6();
break;
case 7 :
data = new Fragment7();
break;
case 8 :
data = new Fragment8();
break;
case 9 :
data = new Fragment9();
break;
case 10 :
data = new MyMapFragment();
break;
case 11:
data = new FragmentQuestionaire();
break;
case 12:
data = new Fragment10();
break;
case 13:
data = new Fragment11();
break;
case 14:
data = new Fragment12();
break;
case 15:
data = new Fragment13();
break;
case 16:
data = new Fragment14();
break;
case 17:
data = new Fragment15();
break;
case 18:
data = new Fragment16();
break;
case 19:
data = new Fragment17();
break;
case 20:
data = new Fragment18();
break;
case 21:
data = new Fragment19();
break;
case 22:
data = new Fragment20();
break;
case 23:
data = new Fragment21();
break;
case 24:
data = new Fragment22();
break;
case 25:
data = new Fragment23();
break;
case 26:
data = new Fragment24();
break;
case 27:
data = new Fragment25();
break;
case 28:
data = new Fragment26();
break;
case 29:
data = new Fragment27();
break;
case 30:
data = new Fragment28();
break;
case 31:
data = new Fragment29();
break;
case 32:
data = new Fragment30();
break;
case 33:
data = new Fragment31();
break;
case 34:
data = new Fragment32();
break;
case 35:
data = new Fragment33();
break;
case 36:
data = new Fragment34();
break;
case 37:
data = new Fragment35();
break;
case 38:
data = new Fragment36();
break;
case 39:
data = new Fragment37();
break;
case 40:
data = new Fragment38();
break;
case 41:
data = new Fragment39();
break;
case 42:
data = new Fragment40();
break;
case 43:
data = new Fragment41();
break;
case 44:
data = new Fragment42();
break;
case 45:
data = new Fragment43();
break;
case 46:
data = new Fragment44();
break;
case 47:
data = new Fragment45();
break;
case 48:
data = new Fragment46();
break;
case 49:
data = new Fragment47();
break;
case 50:
data = new Fragment48();
break;
case 51:
data = new Fragment49();
break;
case 52:
data = new Fragment50();
break;
case 53:
data = new Fragment51();
break;
case 54:
data = new Fragment52();
break;
case 55:
data = new Fragment53();
break;
case 56:
data = new Fragment54();
break;
case 57:
data = new Fragment55();
break;
default :
data = new FragmentQuestionaire();
break;
}
return data;
}
@Override
protected void onDestroy() {
super.onDestroy();
FragmentSignupForm.CUSTOMER_ADDRESS = "";
FragmentSignupForm.CUSTOMER_ADDRESS1 = "";
FragmentSignupForm.CUSTOMER_ADDRESS2 = "";
FragmentSignupForm.CUSTOMER_EMAIL = "";
FragmentSignupForm.CUSTOMER_ID = "";
FragmentSignupForm.CUSTOMER_POSTCODE = "";
FragmentSignupForm.CUSTOMER_NAME = "";
FragmentSignupForm.Telephone = "";
Fragment50.Total = "0";
Fragment47.amount2 = 0;
Fragment47.systemSize = "";
PromotionalPriceListAdditionsDataAdaptor.TotalAdditionalPrice = 0;
PromotionalPriceListDataAdaptor.TotalPrice = 0;
PromotionalPriceListDataAdaptor.Capacity = "";
PromotionalPriceListDataAdaptor.NumberOfPanels = "";
PromotionalPriceListDataAdaptor.PanelSize = "";
PromotionalPriceListDataAdaptor.PanelCapacity = "";
PromotionalPriceListDataAdaptor.SurfaceAreaReuired = "";
PromotionalPriceListDataAdaptor.Price = "";
PromotionalPriceListDataAdaptor.PromotionalOffer = "";
PromotionalPriceListDataAdaptor.TotalOfferPrice = "";
}
}
这是 fragment 类中的代码 -
package com.example.hscroll.demo;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
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.LinearLayout;
import com.example.hscroll.customer.BitmapWeakReference;
import com.example.hscroll.library.imagezoom.ImageViewTouch;
public class Fragment2 extends Fragment{
ImageViewTouch imgview ;
LayoutInflater inflater;
FileInputStream in;
BufferedInputStream buf;
BitmapWeakReference bitmap;
ViewGroup con;
private final String PATH = "/mnt/sdcard/Ideal Solar/Layout_1.png";
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance)
{
this.inflater = inflater;
if(container == null)return null;
container = (LinearLayout)inflater.inflate(R.layout.fragment0_layout, container, false);
con = container;
imgview= (ImageViewTouch)container.findViewById(R.id.imageView1);
bitmap = new BitmapWeakReference(imgview.selectImage(inflater.getContext(), PATH));
if(bitmap!=null)
imgview.setImageBitmapReset( bitmap.get(), true );
return container;
}
/* @Override
public void onResume() {
if(bitmap == null)
{
bitmap = SelectImageFunctions.selectImage(inflater.getContext(), PATH);
}
super.onResume();
}*/
@Override
public void onDestroyView() {
super.onDestroyView();
imgview.setImageBitmap(null);
bitmap.clear();
bitmap = null;
Fragment1.unbindDrawables(con.findViewById(R.id.ll));
System.gc();
}
}
每次当我移动到下一个 fragment 时,前一个 fragment 的 onDestroyView() 被调用并且我正在删除位图和 ImageView 的所有引用但仍然没有释放位图或可绘制对象的内存并且内存不断增加。最后它在大约 50MB 时崩溃。
有人可以帮忙吗?我正在为 galaxy tab 开发这个应用程序。
更新 - 动态页面代码 -
package com.example.hscroll.demo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;
public class FragmentSignupForm extends Fragment {
LayoutInflater inflater;
String result = null;
InputStream is = null;
StringBuilder sb = null;
JSONArray jArray;
EditText c_nameText;
EditText c_addressText;
EditText c_postcodeText;
EditText c_emailText;
EditText s_nameText;
EditText c_addressLine2Text;
EditText telephone_Text;
public static String CUSTOMER_NAME = "";
public static String CUSTOMER_ADDRESS = "";
public static String CUSTOMER_ADDRESS1 = "";
public static String CUSTOMER_ADDRESS2 = "";
public static String CUSTOMER_POSTCODE = "";
public static String CUSTOMER_ID;
public static String CUSTOMER_EMAIL = "";
public static String SALES_CONTACT = "";
public static String Telephone = "";
String output = null;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
ViewGroup con;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance)
{
this.inflater = inflater;
if(container == null)return null;
container = (ScrollView)inflater.inflate(R.layout.signup_form_layout, container, false);
con = container;
ScrollView sv = (ScrollView)container.findViewById(R.id.SV);
sv.setBackgroundDrawable(AssestUtil.getBitmapFromAsset(inflater.getContext(), "bg.jpg"));
LinearLayout ll = (LinearLayout)container.findViewById(R.id.ll);
ll.setBackgroundDrawable(AssestUtil.getBitmapFromAsset(inflater.getContext(), "formbg2.png"));
c_nameText = (EditText)container.findViewById(R.id.cust_name);
c_nameText.setText(CUSTOMER_NAME);
c_addressText = (EditText)container.findViewById(R.id.cust_address);
c_addressText.setText(CUSTOMER_ADDRESS1);
c_addressLine2Text = (EditText)container.findViewById(R.id.cust_Address2);
c_addressLine2Text.setText(CUSTOMER_ADDRESS2);
c_postcodeText = (EditText)container.findViewById(R.id.postcode);
c_postcodeText.setText(CUSTOMER_POSTCODE);
c_emailText = (EditText)container.findViewById(R.id.cust_email);
c_emailText.setText(CUSTOMER_EMAIL);
s_nameText = (EditText)container.findViewById(R.id.sales_name);
s_nameText.setText(SALES_CONTACT);
telephone_Text = (EditText)container.findViewById(R.id.telephone);
telephone_Text.setText(Telephone);
Button save = (Button)container.findViewById(R.id.save_btn);
Button Reset = (Button)container.findViewById(R.id.reset_btn);
final Context context = inflater.getContext();
//Send form data to server on click action
save.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
CUSTOMER_NAME = c_nameText.getText().toString();
System.out.println(CUSTOMER_NAME);
CUSTOMER_ADDRESS = c_addressText.getText().toString() + ","+ c_addressLine2Text.getText().toString()+","+c_postcodeText.getText().toString();;
CUSTOMER_POSTCODE = c_postcodeText.getText().toString();
Telephone = telephone_Text.getText().toString();
new SendDataToServer().execute(c_nameText.getText().toString(),c_addressText.getText().toString(),c_addressLine2Text.getText().toString(),c_postcodeText.getText().toString(),c_emailText.getText().toString(),s_nameText.getText().toString(),telephone_Text.getText().toString());
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(s_nameText.getWindowToken(), 0);
}
});
Reset.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
c_nameText.setText("");
c_addressText.setText("");
c_addressLine2Text.setText("");
c_postcodeText.setText("");
c_emailText.setText("");
s_nameText.setText("");
telephone_Text.setText("");
}
});
return container;
}
private class SendDataToServer extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String... params) {
try
{
nameValuePairs.add(new BasicNameValuePair("cust_name",params[0]));
nameValuePairs.add(new BasicNameValuePair("cust_address",params[1]+","+params[2]));
nameValuePairs.add(new BasicNameValuePair("cust_postcode",params[3]));
nameValuePairs.add(new BasicNameValuePair("cust_email",params[4]));
nameValuePairs.add(new BasicNameValuePair("sales_name",params[5]));
nameValuePairs.add(new BasicNameValuePair("pvgis_data", params[6]));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://ideal.contrastgroup.info/idealsolar/initializeCustomer.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
JSONObject myAway = new JSONObject(read(is));
CUSTOMER_ID = myAway.getString("custID");
output = myAway.getString("message");
}
catch(Exception e)
{
output = "null";
}
return output;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(result.equalsIgnoreCase("success"))
{
Toast.makeText(inflater.getContext(), "Database updated", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(inflater.getContext(), "Error in Connection", Toast.LENGTH_SHORT).show();
}
}
}
public static String read(InputStream in) throws IOException {
StringBuilder sb = new StringBuilder();
BufferedReader r = new BufferedReader(new InputStreamReader(in));
for (String line = r.readLine(); line != null; line = r.readLine()) {
sb.append(line);
}
in.close();
System.out.println(sb.toString());
return sb.toString();
}
@Override
public void onDestroyView() {
super.onDestroyView();
Fragment1.unbindDrawables(con.findViewById(R.id.SV));
System.gc();
}
public static void unbindDrawables(View view) {
if (view.getBackground() != null) {
view.getBackground().setCallback(null);
}
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
unbindDrawables(((ViewGroup) view).getChildAt(i));
}
((ViewGroup) view).removeAllViews();
}
最佳答案
来自谷歌:
Fragment State Pager Support Demonstrates the use of the support class ViewPager with a FragmentStatePagerAdapter to build a user interface where the user can fling left or right to switch between fragments. This versions of the adapter doesn't keep around the fragment instances that ViewPager has destroyed.
与普通分页器的不同之处在于它扩展了 FragmentStatePagerAdapter。
编辑:在谷歌代码中,你在适配器中有这个:
public static class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return NUM_ITEMS;
}
@Override
public Fragment getItem(int position) {
return ArrayListFragment.newInstance(position);
}
}
注意 getItem() 的实现。我们按需获得一个新 fragment ,因此我们不会在需要之前创建 fragment 。这与您创建所有 fragment 并将它们添加到矢量的方法形成对比。 (java 更喜欢 arraylists btw。)使用您的方法, fragment 不会从内存中删除,因为总会有对 fragment 的引用。
编辑 2:再具体一点。在以下内容中:
@Override
public Fragment getItem(int position) {
return ArrayListFragment.newInstance(position);
}
你可以这样做:
@Override
public Fragment getItem(int position) {
if(position == 1) {
return Fragment1.newInstance(position);
} else {
return Fragment2.newInstance(position);
}
}
关于android - 显示图像的 fragment 中的内存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9257952/
我正在尝试学习 Knockout 并尝试创建一个照片 uploader 。我已成功将一些图像存储在数组中。现在我想回帖。在我的 knockout 码(Javascript)中,我这样做: 我在 Jav
我正在使用 php 编写脚本。我的典型问题是如何在 mysql 中添加一个有很多替代文本和图像的问题。想象一下有机化学中具有苯结构的描述。 最有效的方法是什么?据我所知,如果我有一个图像,我可以在数据
我在两个图像之间有一个按钮,我想将按钮居中到图像高度。有人可以帮帮我吗? Entrar
下面的代码示例可以在这里查看 - http://dev.touch-akl.com/celebtrations/ 我一直在尝试做的是在 Canvas 上绘制 2 个图像(发光,然后耀斑。这些图像的链接
请检查此https://jsfiddle.net/rhbwpn19/4/ 图像预览对于第一篇帖子工作正常,但对于其他帖子则不然。 我应该在这里改变什么? function readURL(input)
我对 Canvas 有疑问。我可以用单个图像绘制 Canvas ,但我不能用单独的图像绘制每个 Canvas 。- 如果数据只有一个图像,它工作正常,但数据有多个图像,它不工作你能帮帮我吗? va
我的问题很简单。如何获取 UIImage 的扩展类型?我只能将图像作为 UIImage 而不是它的名称。图像可以是静态的,也可以从手机图库甚至文件路径中获取。如果有人可以为此提供一点帮助,将不胜感激。
我有一个包含 67 个独立路径的 SVG 图像。 是否有任何库/教程可以为每个路径创建单独的光栅图像(例如 PNG),并可能根据路径 ID 命名它们? 最佳答案 谢谢大家。我最终使用了两个答案的组合。
我想将鼠标悬停在一张图片(音乐专辑)上,然后播放一张唱片,所以我希望它向右移动并旋转一点,当它悬停时我希望它恢复正常动画片。它已经可以向右移动,但我无法让它随之旋转。我喜欢让它尽可能简单,因为我不是编
Retina iOS 设备不显示@2X 图像,它显示 1X 图像。 我正在使用 Xcode 4.2.1 Build 4D502,该应用程序的目标是 iOS 5。 我创建了一个测试应用(主/细节)并添加
我正在尝试从头开始以 Angular 实现图像 slider ,并尝试复制 w3school基于图像 slider 。 下面我尝试用 Angular 实现,谁能指导我如何使用 Angular 实现?
我正在尝试获取图像的图像数据,其中 w= 图像宽度,h = 图像高度 for (int i = x; i imageData[pos]>0) //Taking data (here is the pr
我的网页最初通过在 javascript 中动态创建图像填充了大约 1000 个缩略图。由于权限问题,我迁移到 suPHP。现在不用标准 标签本身 我正在通过这个 php 脚本进行检索 $file
我正在尝试将 python opencv 图像转换为 QPixmap。 我按照指示显示Page Link我的代码附在下面 img = cv2.imread('test.png')[:,:,::1]/2
我试图在这个 Repository 中找出语义分割数据集的 NYU-v2 . 我很难理解图像标签是如何存储的。 例如,给定以下图像: 对应的标签图片为: 现在,如果我在 OpenCV 中打开标签图像,
import java.util.Random; class svg{ public static void main(String[] args){ String f="\"
我有一张 8x8 的图片。 (位图 - 可以更改) 我想做的是能够绘制一个形状,给定一个 Path 和 Paint 对象到我的 SurfaceView 上。 目前我所能做的就是用纯色填充形状。我怎样才
要在页面上显示图像,你需要使用源属性(src)。src 指 source 。源属性的值是图像的 URL 地址。 定义图像的语法是: 在浏览器无法载入图像时,替换文本属性告诉读者她们失去的信息。此
**MMEditing是基于PyTorch的图像&视频编辑开源工具箱,支持图像和视频超分辨率(super-resolution)、图像修复(inpainting)、图像抠图(matting)、
我正在尝试通过资源文件将图像插入到我的程序中,如下所示: green.png other files 当我尝试使用 QImage 或 QPixm
我是一名优秀的程序员,十分优秀!