Android - Recyclerview片段在设备上崩溃,而不是在模拟器

时间:2016-09-01 05:34:14

标签: android android-fragments android-recyclerview

我正在使用Recyclerview来显示文本字段和图像视图。它在模拟器上完美运行(Genymotion谷歌Nexus 4运行5.1),但我通过电子邮件将APK文件发送给我的朋友,该应用程序在任何地方都能正常工作,但是当他点击导航抽屉项目,将他带到回收站视图片段时,应用程序崩溃。他有一个运行Android 6的Galaxy Note 5.

这里是人员片段的代码:

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;


public class People extends Fragment {
PeopleAdapter adapter;

public People() {
    // Required empty public constructor
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    Home.inHome = false;


    View view = inflater.inflate(R.layout.fragment_people, container, false);
    RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.peopleRecyclerView);

    final LinearLayoutManager layoutManager = new LinearLayoutManager(this.getActivity());
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(layoutManager);
    PeopleAdapter adapter = new PeopleAdapter(this.getContext(),getData(), (MainActivity) this.getActivity());
    recyclerView.setAdapter(adapter);


    recyclerView.setHasFixedSize(true);
    return view;


}

public static ArrayList<Person> getData(){
    ArrayList<Person> people = new ArrayList<Person>();

    people = WelcomeScreen.downloadedPeople;
    return people;
}

public AssetManager getAsset(){
    return getActivity().getAssets();
}

}

这里是我用于回收站视图的适配器:

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.Rect;

import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.ArrayList;


public class PeopleAdapter extends RecyclerView.Adapter<PeopleAdapter.PeopleViewHolder> {
private LayoutInflater inflater;
private ArrayList<Person> mPeople;
private Context mContext;
MainActivity main;



public PeopleAdapter(Context context, ArrayList<Person> people, MainActivity ma){
    inflater = LayoutInflater.from(context);
    mContext = context;
    mPeople = people;
    main = ma;
}





@Override
public PeopleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = inflater.inflate(R.layout.peoplecell,parent,false);

    PeopleViewHolder viewHolder = new PeopleViewHolder(view);

    return viewHolder;
}

@Override
public void onBindViewHolder(PeopleViewHolder holder, int position) {
    holder.bindPerson(mPeople.get(position));
}

@Override
public int getItemCount() {
    return mPeople.size();
}

public class PeopleViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    TextView name;
    TextView bio;
    ImageView image;
    String personImagePath;
    public Context myContext;
    public PeopleViewHolder(View itemView) {
        super(itemView);

        name = (TextView) itemView.findViewById(R.id.nameView);
        bio = (TextView) itemView.findViewById(R.id.bioView);
        image = (ImageView) itemView.findViewById(R.id.profile_image);


    }
    public void bindPerson(Person person){
    name.setText(person.getName());
        bio.setText(person.getBio());
        for(int i=0;i<WelcomeScreen.peopleImagesFiles.size();i++){
            personImagePath =  WelcomeScreen.peopleImagesFiles.get(i).getName();
            if (personImagePath.substring(15,16).equals(person.getImage())){
                Bitmap myBitmap = BitmapFactory.decodeFile(WelcomeScreen.peopleImagesFiles.get(i).getAbsolutePath());

                image.setImageBitmap(myBitmap);
                break;
            }
        }
    }

    @Override
    public void onClick(View v) {

    }
    public Bitmap getRoundedShape(Bitmap scaleBitmapImage) {
        int targetWidth = 100;
        int targetHeight = 100;
        Bitmap targetBitmap = Bitmap.createBitmap(targetWidth,
                targetHeight,Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(targetBitmap);
        Path path = new Path();
        path.addCircle(((float) targetWidth - 1) / 2,
                ((float) targetHeight - 1) / 2,
                (Math.min(((float) targetWidth),
                        ((float) targetHeight)) / 2),
                Path.Direction.CCW);

        canvas.clipPath(path);
        Bitmap sourceBitmap = scaleBitmapImage;
        canvas.drawBitmap(sourceBitmap,
                new Rect(0, 0, sourceBitmap.getWidth(),
                        sourceBitmap.getHeight()),
                new Rect(0, 0, targetWidth, targetHeight), null);
        return targetBitmap;
    }
}
}

我很困惑为什么这在模拟器而不是设备上完美运行。你怎么看?

更新:这是我得到的错误:                                                                     java.lang.OutOfMemoryError:无法分配带有16777216个空闲字节的602997148字节分配和450MB直到OOM                                                                         at dalvik.system.VMRuntime.newNonMovableArray(Native Method)                                                                         在android.graphics.BitmapFactory.nativeDecodeAsset(本机方法)                                                                         在android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:856)                                                                         在android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:675)                                                                         在android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:2228)                                                                         在android.content.res.Resources.loadDrawableForCookie(Resources.java:4211)                                                                         在android.content.res.Resources.loadDrawable(Resources.java:4085)                                                                         在android.content.res.Resources.loadDrawable(Resources.java:3935)                                                                         在android.content.res.TypedArray.getDrawable(TypedArray.java:886)                                                                         在android.widget.ImageView。(ImageView.java:157)                                                                         在android.widget.ImageView。(ImageView.java:145)                                                                         在android.support.v7.widget.AppCompatImageView。(AppCompatImageView.java:57)                                                                         在android.support.v7.widget.AppCompatImageView。(AppCompatImageView.java:53)                                                                         在android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)                                                                         在android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1013)                                                                         在android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1072)                                                                         在android.support.v4.view.LayoutInflaterCompatHC $ FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)                                                                         在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:758)                                                                         在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)                                                                         在android.view.LayoutInflater.rInflate(LayoutInflater.java:847)                                                                         在android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)                                                                         在android.view.LayoutInflater.inflate(LayoutInflater.java:527)                                                                         在android.view.LayoutInflater.inflate(LayoutInflater.java:429)                                                                         在com.anasghanim.gws.teamsAdapter.onCreateViewHolder(teamsAdapter.java:49)                                                                         在com.anasghanim.gws.teamsAdapter.onCreateViewHolder(teamsAdapter.java:26)                                                                         在android.support.v7.widget.RecyclerView $ Adapter.createViewHolder(RecyclerView.java:5833)                                                                         在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5057)                                                                         在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:4967)                                                                         在android.support.v7.widget.LinearLayoutManager $ LayoutState.next(LinearLayoutManager.java:2029)                                                                         在android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414)                                                                         在android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)                                                                         在android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:578)                                                                         在android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3315)                                                                         在android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2843)                                                                         在android.view.View.measure(View.java:20159)                                                                         在android.widget.RelativeLayout.measureChildHorizo​​ntal(RelativeLayout.java:716)                                                                         在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:462)                                                                         在android.view.View.measure(View.java:20159)                                                                         在android.widget.RelativeLayout.measureChildHorizo​​ntal(RelativeLayout.java:716)                                                                         在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:462)                                                                         在android.view.View.measure(View.java:20159)                                                                         在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)                                                                         在android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:706)                                                                         在android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)                                                                         在android.support.design.widget.AppBarLayout $ ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1364)                                                                         在android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:771)                                                                         在android.view.View.measure(View.java:20159)                                                                         在android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1085)                                                                         在android.view.View.measure(View.java:20159)                                                                         在android.view.ViewGroup.measureChildWithMargins(ViewGroup.j

0 个答案:

没有答案