关于构造函数:如何将列表列表通过bundle传递给android中的片段意味着List<List<Bitmap>>
how to pass the list of lists means List<List<Bitmap>> through bundle to the fragment in android
基本上我使用片段,我通过创建构造函数将列表传递给片段,但是当我尝试生成签名的 apk 文件时,我得到了如下错误
(这个片段应该有一个默认的构造函数)搜索后我找到了创建 newInstance 的方法,以通过捆绑将数据传递给另一个片段
像这样...
public static Fragment newInstance(String arg1, int arg2) {
Fragment result = new MyFragment(); Bundle args = new Bundle(); args.putString("arg1_key", arg1); args.putInt("arg2_key", arg2); result.setArguments(args); return result; } |
我的代码在这里请任何建议.......
import android.app.ProgressDialog;
import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.BitmapShader; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.Shader; import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.os.Parcelable; import android.support.annotation.Nullable; import android.support.design.internal.ParcelableSparseArray; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.ImageView; import com.google.android.gms.ads.AdView; import java.util.ArrayList; /** public class splashFragment extends Fragment { private int[] HandImages = {R.drawable.hands_1, R.drawable.hands_2, R.drawable.hands_3, R.drawable.hands_4, R.drawable.hands_5,
private int[] FootImages = {R.drawable.foot_1, R.drawable.foot_2, R.drawable.foot_3, R.drawable.foot_4, R.drawable.foot_5,
private int[] TatooImages = {R.drawable.tatoos_1, R.drawable.tatoos_2, R.drawable.tatoos_3, R.drawable.tatoos_4, R.drawable.tatoos_5, private int[] NailImages = {R.drawable.nail_1, R.drawable.nail_2, R.drawable.nail_3, R.drawable.nail_4, R.drawable.nail_5,
ArrayList<Bitmap> HandsBitmapArray = new ArrayList<Bitmap>(); ImageView imageView;
@Nullable
View view = inflater.inflate(R.layout.splash_fragment, container, false); Picasso.with(getActivity()).load(R.drawable.design_12).transform(new CircleTransform()).into(imageView); final AdView mAdView = (AdView) getActivity().findViewById(R.id.adView1); if (HandImages != null && HandImages.length > 0) { return view; public class CircleTransform implements Transformation { final Paint paint = new Paint(); Bitmap output = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); if (source != output) { return output; @Override
public class BackGroundTask extends AsyncTask<Object, Object, List<List<Bitmap>>> { List<List<Bitmap>> arrayOfLists = new ArrayList<List<Bitmap>>(); @Override /*@Override progressBar.setVisibility(View.VISIBLE);
@Override if (isAdded() == true) { for (int i = 0; i < HandImages.length; i++) { Bitmap handsImaes = BitmapFactory.decodeResource(getActivity().getResources(), HandImages[i]); byte[] array = buffer.array();*/ /* Bitmap b1 = BitmapFactor y.decodeByteArray(b, 0, b.length);
//Bitmap bitmap = Bitmap.createScaledBitmap(largeIcon, 80, 80, true); //Bitmap bitmap = Bitmap.createScaledBitmap(largeIcon,(int)(largeIcon.getWidth()*0.1), (int)(largeIcon.getHeight()*0.1), true);
}
for (int i = 0; i < FootImages.length; i++){ } for (int i = 0; i < TatooImages.length; i++){ for (int i = 0; i < NailImages.length; i++){ return arrayOfLists;
@Override
//progressDialog.dismiss(); public void onTick(long millisUntilFinished) { } public void onFinish() {*/ //MainFragment mainFragment = newInstance(result); MainFragment mainFragment = new MainFragment(result); /*CustomAdapter adapter = new CustomAdapter(getActivity(), R.layout.custom_grid_row, result); AlphaInAnimationAdapter swingRightInAnimationAdapter = new AlphaInAnimationAdapter(adapter);
public static MainFragment newInstance(List<List<Bitmap>> bitmapArray) public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) { /* BitmapFactory.Options o = new BitmapFactory.Options(); int width = bm.getWidth(); //"RECREATE" THE NEW BITMAP @Override } |
我在这个函数中的错误......
public static MainFragment newInstance(List<List<Bitmap>> bitmapArray)
{ MainFragment fragment = new MainFragment(); Bundle bundle = new Bundle(); bundle.putParcelableArrayList("array", bitmapArray); fragment.setArguments(bundle); return fragment; } |
编辑我喜欢的 getterSetter 类...
public class GetterSetterForBitmaps {
public List<List<Bitmap>> HandsList; public List<List<Bitmap>> getHandsList() { public void setHandsList(List<List<Bitmap>> handsList) { public List<List<Bitmap>> getFootsList() { public void setFootsList(List<List<Bitmap>> footsList) { public List<List<Bitmap>> getTatoosLists() { public void setTatoosLists(List<List<Bitmap>> tatoosLists) { public List<List<Bitmap>> getNailsLists() { public void setNailsLists(List<List<Bitmap>> nailsLists) { |
并像这样在 GetterSetter 中设置列??表..
GetterSetterForBitmaps getterSetterForBitmaps = new GetterSetterForBitmaps();
getterSetterForBitmaps.setHandsList(arrayOfLists); |
但是当像这样从 GetterSetter 获取列表时...
GetterSetterForBitmaps getterSetterForBitmaps = new GetterSetterForBitmaps();
List<List<Bitmap>> list = getterSetterForBitmaps.getHandsList(); |
它返回null???
相关讨论
- 也许您的列表位图太大,您不能将它们放入位图中。我认为您应该将列表可绘制资源放到 MainFragment 中。
Don't do this, you will be wasting great amount of memory in
serializing and de-serializing bitmaps as it will create two
additional copies of same object and will cost you in longer run.
我建议创建一个 DataLayer 用于在
之间传递数据
例如
class MyImageCache
{ static List<List<BitMap>> list; public static getList(){ return list; } public static setList(List<List<BitMap>> list1){ list=list1; } } |
这将使您免于在应用程序中创建同一对象的多个副本。
You can modify"ImageCache" class's behavior to have more control over data sharing.
相关讨论
- 感谢您的回复
- 这对你的案子有帮助吗?
- 是的,我会采纳你的建议。我认为这会对我有所帮助。
- 这里需要注意的一件微不足道但重要的事情是,如果 android 系统内存不足,您的静态变量可能会变为 null。在这种情况下,您需要设计一种机制来重新初始化缓存。看到这个
- @Nayan Srivastava 请查看我更新的问题....我使用吸气剂二传手,但我面临问题
- 变量和方法需要是静态的,这样你就可以在不创建多个对象的情况下共享它们
- @Nayan Srivastava 你能回答这个问题吗?