片段活动进入后台时Android应用程序崩溃

时间:2015-12-01 13:02:35

标签: android android-fragments

在我的片段类中,片段活动在活动进入后台时崩溃,并且在前台应用程序中返回时会崩溃。

这是我的班级: -

public class HomeFragment extends Fragment implements View.OnClickListener {

    public static final String KEY_IS_POPPED = "KEY_IS_POPPED";
     public boolean mPopped;

    /* Slider Varible */

    private SliderLayout mDemoSlider;
    private HashMap<String,String> url_maps = new HashMap<>();
    private List<String> image_path = new ArrayList<>();

    /* Category Grid  */

    private HomeFragmentAdapter adaptergridview;
    private ArrayList<Cateogary> productsList = new ArrayList<>();
    private ImageView category1,category2,category3,category4,category5,category6;

    /* Banners   */

    private LinearLayout linearLayoutline3;
    private List<Banners> bannersList = new ArrayList<>();

    /* Footer   */

    private TextView cart_botem_home,cart_botem_cart,cart_bottem_search,buy_now_wishlist_bottem;
    private UserSessionManager session;


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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_home, container, false);

        mDemoSlider = (SliderLayout)rootView.findViewById(R.id.slider);
        adaptergridview = new HomeFragmentAdapter(getContext(),R.layout.home_fragment_grid,productsList);
        linearLayoutline3 = (LinearLayout)rootView.findViewById(R.id.line3);

        category1 = (ImageView)rootView.findViewById(R.id.category1);
        category2 = (ImageView)rootView.findViewById(R.id.category2);
        category3 = (ImageView)rootView.findViewById(R.id.category3);
        category4 = (ImageView)rootView.findViewById(R.id.category4);
        category5 = (ImageView)rootView.findViewById(R.id.category5);
        category6 = (ImageView)rootView.findViewById(R.id.category6);

        cart_botem_home = (TextView)rootView.findViewById(R.id.cart_botem_home);
        cart_botem_cart = (TextView)rootView.findViewById(R.id.cart_botem_cart);
        cart_bottem_search = (TextView)rootView.findViewById(R.id.cart_bottem_search);
        buy_now_wishlist_bottem = (TextView)rootView.findViewById(R.id.buy_now_wishlist_bottem);

        cart_botem_home.setOnClickListener(this);
        cart_botem_cart.setOnClickListener(this);
        cart_bottem_search.setOnClickListener(this);
        buy_now_wishlist_bottem.setOnClickListener(this);

        session = new UserSessionManager(getContext());


        category1.setOnClickListener(this);
        category2.setOnClickListener(this);
        category3.setOnClickListener(this);
        category4.setOnClickListener(this);
        category5.setOnClickListener(this);
        category6.setOnClickListener(this);



        new ImageSlider().execute("http://www.shoppingmazza.com/?route=feed/web_api/slider&key=test123$");
        new Printcategory().execute("http://www.shoppingmazza.com/?route=feed/web_api/fmenu&key=test123$");
        new PrintBanners().execute("http://shoppingmazza.com/?route=feed/web_api/homeapp&key=test123$");

        return rootView;
    }
    public void onClick(View v){
        switch (v.getId()){
            case R.id.category1:
                Intent intent = new Intent(getContext(),SubCategory.class);
                intent.putExtra("menu_id", productsList.get(0).getCid());
                startActivity(intent);
                break;
            case R.id.category2:
                Intent intent1 = new Intent(getContext(),SubCategory.class);
                intent1.putExtra("menu_id",productsList.get(1).getCid());
                startActivity(intent1);
                break;
            case R.id.category3:
                Intent intent2 = new Intent(getContext(),SubCategory.class);
                intent2.putExtra("menu_id",productsList.get(2).getCid());
                startActivity(intent2);
                break;
            case R.id.category4:
                Intent intent3 = new Intent(getContext(),SubCategory.class);
                intent3.putExtra("menu_id",productsList.get(3).getCid());
                startActivity(intent3);
                break;
            case R.id.category5:
                Intent intent4 = new Intent(getContext(),SubCategory.class);
                intent4.putExtra("menu_id",productsList.get(4).getCid());
                startActivity(intent4);
                break;
            case R.id.category6:
                Intent intent5 = new Intent(getContext(),SubCategory.class);
                intent5.putExtra("menu_id",productsList.get(5).getCid());
                startActivity(intent5);
                break;
            case R.id.cart_botem_home:
                Intent intent6 = new Intent(getContext(),MainActivity.class);
                startActivity(intent6);
                break;
            case R.id.cart_botem_cart:
                Intent intent7 = new Intent(getContext(),CheckOut.class);
                startActivity(intent7);
                break;
            case R.id.cart_bottem_search:
                Intent intent8 = new Intent(getContext(),SearchActivity.class);
                startActivity(intent8);
                break;
            case R.id.buy_now_wishlist_bottem:
                if(session.checkLoginWishlist()){
                    Intent wishlistIntent = new Intent(getContext(),WishList.class);
                    startActivity(wishlistIntent);
                }
                else {
                   Toast.makeText(getContext(),"Login first to see wishlist",Toast.LENGTH_LONG).show();
                }
                break;

        }
    }
    public class ImageSlider extends AsyncTask<String ,Void,Void>{
        Boolean result = true;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(String... params) {
            try {
                HttpClient client = new DefaultHttpClient();
                HttpPost post = new HttpPost(params[0]);
                HttpResponse response = client.execute(post);

                int sattus = response.getStatusLine().getStatusCode();

                if(sattus==200){
                    HttpEntity entity = response.getEntity();
                    String data = EntityUtils.toString(entity);

                    JSONObject jsonObject = new JSONObject(data);
                    JSONArray jsonArray = jsonObject.getJSONArray("banners");

                    for(int i=0;i<jsonArray.length();i++){
                        JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                        image_path.add(jsonObject1.getString("sliderimage"));
                    }
                }
            }catch (IOException |JSONException e){
                result = false;
                Log.e("Error ",e.getMessage());
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            //super.onPostExecute(aVoid);
            if(result){
                for(int i=0;i<image_path.size();i++){
                    url_maps.put(i+"", image_path.get(i));
                }

                for(String name : url_maps.keySet()){
                    TextSliderView textSliderView = new TextSliderView(getContext());
                    textSliderView
                            .description("")
                            .image(url_maps.get(name));
                    mDemoSlider.addSlider(textSliderView);
                }
                //LinearLayout.LayoutParams layoutParams =
                Display display = getActivity().getWindowManager().getDefaultDisplay();
                Point size = new Point();
                display.getSize(size);
                int width = size.x;
                int height = size.y;
                mDemoSlider.getLayoutParams().height=(height*30)/100;
                mDemoSlider.requestLayout();
                //mDemoSlider.
             /*   LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, heigth1);
                mDemoSlider.setLayoutParams(layoutParams);
                mDemoSlider.requestLayout();*/
                mDemoSlider.setPresetTransformer(SliderLayout.Transformer.Accordion);
                mDemoSlider.setCustomAnimation(new DescriptionAnimation());
                mDemoSlider.setDuration(4000);
            }
            else {
                Toast.makeText(getContext(),"Somthing is bad happen",Toast.LENGTH_LONG).show();
            }
        }
    }
    public class Printcategory extends AsyncTask<String,Void,Boolean>{
        ProgressDialog dialog;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog = new ProgressDialog(getContext());
            dialog.setCancelable(false);
            dialog.show();
        }
        @Override
        protected Boolean doInBackground(String... params) {
            try{

                HttpClient client = new DefaultHttpClient();
                HttpPost post = new HttpPost(params[0]);
                HttpResponse response = client.execute(post);

                int status = response.getStatusLine().getStatusCode();
                if(status==200){
                    HttpEntity entity = response.getEntity();
                    String data = EntityUtils.toString(entity);

                    JSONObject jsonObject = new JSONObject(data);
                    JSONArray jsonArray = jsonObject.getJSONArray("categories");

                    for(int i=0;i<jsonArray.length();i++){

                        JSONObject jsonObject1 = jsonArray.getJSONObject(i);

                        Cateogary product = new Cateogary();

                        product.setName(jsonObject1.getString("name"));
                        product.setCid(jsonObject1.getString("cid"));
                        product.setImageview(jsonObject1.getString("image"));

                        productsList.add(product);
                    }
                    return true;
                }
            }catch (IOException | JSONException e){

                Log.e("Error:",e.getMessage());
            }

            return null;
        }
        @Override
        protected void onPostExecute(Boolean aVoid) {
            //super.onPostExecute(aVoid);
            dialog.dismiss();
            adaptergridview.notifyDataSetChanged();
            if(!aVoid){
                Toast.makeText(getContext(), "Data is not Parsed", Toast.LENGTH_LONG).show();
            }
            else {
                Log.d("pod",productsList.size()+"");
                Log.d("array",productsList+"");
                int cnt =0;

               // for(int i=0;i<productsList.size();i++){

                Display display = getActivity().getWindowManager().getDefaultDisplay();
                Point size = new Point();
                display.getSize(size);
                int width = size.x;
                int height = size.y;

                    Picasso.with(getContext()).load(productsList.get(0).getImageview()).resize(width/2,(height*40)/100).into(category1);
                    Picasso.with(getContext()).load(productsList.get(1).getImageview()).resize(width/2,(height*40)/100).into(category2);
                    Picasso.with(getContext()).load(productsList.get(2).getImageview()).resize(width/2,(height*40)/100).into(category3);
                    Picasso.with(getContext()).load(productsList.get(3).getImageview()).resize(width/2,(height*40)/100).into(category4);
                    Picasso.with(getContext()).load(productsList.get(4).getImageview()).resize(width/2,(height*40)/100).into(category5);
                    Picasso.with(getContext()).load(productsList.get(5).getImageview()).resize(width/2,(height*40)/100).into(category6);

                  /*   LinearLayout   linearLayout = new LinearLayout(getContext());
                        linearLayout.setOrientation(LinearLayout.HORIZONTAL);

                    for(int j=2*i;j<=(2*i)+1;j++){

                        Display display = getActivity().getWindowManager().getDefaultDisplay();
                        Point size = new Point();
                        display.getSize(size);
                        int width = size.x;
                        int height = size.y;

                        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                        layoutParams.weight = 0.5f;
                        final ImageView imageView = new ImageView(getContext());
                        imageView.setLayoutParams(layoutParams);
                        imageView.setId(i);

                            imageView.setPadding(10, 10, 10, 10);

                        Picasso.with(getContext()).load(productsList.get(j).getImageview()).resize(width/2,300).into(imageView);

                        linearLayout.addView(imageView);

                        imageView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(getContext(),SubCategory.class);
                                intent.putExtra("menu_id",productsList.get(v.getId()).getCid());
                                startActivity(intent);
                            }
                        });
                    }*/
                    //linearLayoutline3.addView(linearLayout);
               // }
            }
        }
    }
    public class PrintBanners extends AsyncTask<String,Void,Void>{
        Boolean result =true;
        ProgressDialog dialog;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog = new ProgressDialog(getContext());
            dialog.setCancelable(false);
            dialog.show();
        }
        @Override
        protected Void doInBackground(String... params) {
            try {
                HttpClient client = new DefaultHttpClient();
                HttpPost post = new HttpPost(params[0]);
                HttpResponse response = client.execute(post);

                int status = response.getStatusLine().getStatusCode();
                if(status==200){
                    HttpEntity entity = response.getEntity();
                    String data = EntityUtils.toString(entity);

                    JSONObject jsonObject = new JSONObject(data);
                    JSONArray jsonArray = jsonObject.getJSONArray("homebanner");

                    for(int i=0;i<jsonArray.length();i++){

                        JSONObject jsonObject1 = jsonArray.getJSONObject(i);

                        Banners banners = new Banners();

                        banners.setName(jsonObject1.getString("name"));
                        banners.setCid(jsonObject1.getString("cid"));
                        banners.setImage(jsonObject1.getString("image"));

                        bannersList.add(banners);
                    }
                }
            }
            catch (IOException | JSONException e){
                result = false;
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            //super.onPostExecute(aVoid);
            dialog.dismiss();
            if(result){

                for(int i=0;i<bannersList.size();i++){

                    final ImageView imageView = new ImageView(getContext());
                    imageView.setId(i);
                    Display display = getActivity().getWindowManager().getDefaultDisplay();
                    Point size = new Point();
                    display.getSize(size);
                    int width = size.x;
                    int height = size.y;



                    Picasso.with(getContext()).load(bannersList.get(i).getImage()).resize(width,(height*30)/100).centerInside().into(imageView);

                 //   Picasso.with(getContext()).load("http://shoppingmazza.com/image/data/app/5.jpg").into(imageView);
                    imageView.setPadding(20, 0, 20, 0);

                   // imageView.setPadding();

                    TextView textView = new TextView(getContext());
                    textView.setText(bannersList.get(i).getName());
                    textView.setPadding(20, 0, 0, 0);
                    textView.setTextSize(24.0f);
                    textView.setTextColor(Color.parseColor("#5c5c5c"));

                    linearLayoutline3.addView(textView);
                    linearLayoutline3.addView(imageView);

                    imageView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            Intent intent = new Intent(getContext(),ProductsCategory.class);
                            intent.putExtra("category",bannersList.get(v.getId()).getCid());
                            startActivity(intent);
                        }
                    });
                }
            }
            else {
                Toast.makeText(getContext(),"Somthing is bad happen try again later",Toast.LENGTH_LONG).show();
            }
        }
    }

            @Override
            public void onAttach(Activity activity) {
                super.onAttach(activity);
            }

            @Override
            public void onDetach() {
                super.onDetach();
            }

            public class MalibuCountDownTimer extends CountDownTimer {
                public MalibuCountDownTimer(long millisInFuture, long countDownInterval) {
                    super(millisInFuture, countDownInterval);
                }

                @Override
                public void onTick(long millisUntilFinished) {
                    Toast.makeText(getContext(), "hello look time is running", Toast.LENGTH_LONG).show();

                }
                @Override
                public void onFinish() {

                }
            }
}

logcat的: -

12-01 18:33:52.040 1428-1480/system_process E/KernelUidCpuTimeReader: Failed to read uid_cputime
                                                                  java.io.FileNotFoundException: /proc/uid_cputime/show_uid_stat: open failed: ENOENT (No such file or directory)
                                                                      at libcore.io.IoBridge.open(IoBridge.java:452)
                                                                      at java.io.FileInputStream.<init>(FileInputStream.java:76)
                                                                      at java.io.FileInputStream.<init>(FileInputStream.java:103)
                                                                      at java.io.FileReader.<init>(FileReader.java:66)
                                                                      at com.android.internal.os.KernelUidCpuTimeReader.readDelta(KernelUidCpuTimeReader.java:71)
                                                                      at com.android.internal.os.BatteryStatsImpl.updateCpuTimeLocked(BatteryStatsImpl.java:8031)
                                                                      at com.android.server.am.BatteryStatsService.updateExternalStats(BatteryStatsService.java:1366)
                                                                      at com.android.server.am.BatteryStatsService$BatteryStatsHandler.handleMessage(BatteryStatsService.java:125)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                      at android.os.Looper.loop(Looper.java:148)
                                                                      at android.os.HandlerThread.run(HandlerThread.java:61)
                                                                      at com.android.server.ServiceThread.run(ServiceThread.java:46)
                                                                   Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
                                                                      at libcore.io.Posix.open(Native Method)
                                                                      at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
                                                                      at libcore.io.IoBridge.open(IoBridge.java:438)
                                                                      at java.io.FileInputStream.<init>(FileInputStream.java:76) 
                                                                      at java.io.FileInputStream.<init>(FileInputStream.java:103) 
                                                                      at java.io.FileReader.<init>(FileReader.java:66) 
                                                                      at com.android.internal.os.KernelUidCpuTimeReader.readDelta(KernelUidCpuTimeReader.java:71) 
                                                                      at com.android.internal.os.BatteryStatsImpl.updateCpuTimeLocked(BatteryStatsImpl.java:8031) 
                                                                      at com.android.server.am.BatteryStatsService.updateExternalStats(BatteryStatsService.java:1366) 
                                                                      at com.android.server.am.BatteryStatsService$BatteryStatsHandler.handleMessage(BatteryStatsService.java:125) 
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                      at android.os.Looper.loop(Looper.java:148) 
                                                                      at android.os.HandlerThread.run(HandlerThread.java:61) 
                                                                      at com.android.server.ServiceThread.run(ServiceThread.java:46)

我是android开发的新手。任何人都可以帮我解决我的问题。提前谢谢!

0 个答案:

没有答案