图片下载期间Android页面崩溃

时间:2015-10-15 15:48:40

标签: java android

我是一个极端的Android / Java新手,在我的第一个Android应用程序上工作。

我有一个从php / mySQL页面下载数据的页面,并将其显示在屏幕布局上。数据将包含某些字段;标题,摘要,照片等。"照片" field是在线存储的图像的URL。

当照片链接字段为空时,页面似乎工作正常。当图像是一个非常小的图像时,页面似乎工作正常。但是当图像的文件较大时,应用程序会崩溃。

此页面的完整代码如下所示,因为我不知道此代码可能出现的问题!

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.InputStream;

public class DetailScreen extends android.support.v4.app.Fragment {
RelativeLayout relativeLayout;

ProgressDialog progressDialog;
DownloadImageTask d;
ImageView imageView;
int tab,position;

ImageView detail_image;
TextView detail_head;
TextView detail_disc;
TextView detail_date;
TextView detail_detail;
TextView detail_url;

Bitmap imageBitmap = null;

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putParcelable("imageBitmap",imageBitmap);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(savedInstanceState != null) {
        imageBitmap = savedInstanceState.getParcelable("imageBitmap");
    }

    Bundle bundle = this.getArguments();
    if(bundle != null) {
        tab = bundle.getInt(BackEnd.TAB);
        position = bundle.getInt(BackEnd.POSITION);
    }
}

void setValues()
{

    JSONArray RawData = (tab == 0) ? BackEnd.news : BackEnd.events;
    JSONObject data = null;

    try {
        data = (JSONObject) RawData.get(position);
    }
    catch (JSONException e) {
        e.printStackTrace();
    }

    try {
        assert data != null;
        if(imageBitmap == null) {
            d = new DownloadImageTask(detail_image);
            d.execute((String) data.get("photograph"));
            progressDialog = new ProgressDialog(getActivity());
            progressDialog.setMessage("Loading Image");
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    d.cancel(true);
                    Toast.makeText(getActivity(), "Unable to load Image", Toast.LENGTH_SHORT).show();
                }
            });
            progressDialog.show();

        }

        detail_head.setText((String) data.get("title"));
        detail_disc.setText((String) data.get((tab==0)?"summary":"location"));
        detail_date.setText((String) data.get("date"));
        detail_detail.setText(Html.fromHtml((String) data.get("details")));
        if (tab == 0) {
            final String url = (String) data.get("url");
            detail_url.setText(getResources().getString(R.string.urlMessage));
            detail_url.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    TextView t = (TextView) v;
                    t.setTextColor(Color.BLACK);
                    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    startActivity(browserIntent);
                }
            });
        } else {
            View view = getView();
            if(view != null) {
                TextView textView = (TextView) view.findViewById(R.id.detail_url);
                textView.setText("");
            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    relativeLayout = (RelativeLayout) getActivity().findViewById(R.id.actionBarCustom);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

    imageView = new ImageView(getActivity());
    imageView.setImageResource(R.drawable.ic_action_back);
    imageView.setLayoutParams(params);

    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            android.support.v4.app.Fragment fragment = new Page4();
            getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.mainContent, fragment).addToBackStack(null).commit();
        }

    });
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    detail_image = (ImageView) view.findViewById(R.id.detail_image);
    if(imageBitmap != null)
        detail_image.setImageBitmap(imageBitmap);

    detail_head = (TextView) view.findViewById(R.id.detail_head);
    detail_disc = (TextView) view.findViewById(R.id.detail_disc);
    detail_date = (TextView) view.findViewById(R.id.detail_date);
    detail_detail = (TextView) view.findViewById(R.id.detail_detail);
    detail_url = (TextView) view.findViewById(R.id.detail_url);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    return inflater.inflate(R.layout.fragment_detail_screen, container, false);
}

@Override
public void onResume() {
    super.onResume();
    TextView t=(TextView)relativeLayout.findViewById(R.id.actionText);
     t.setText("Details");
    relativeLayout.addView(imageView);
    setValues();
}

@Override
public void onPause() {
    super.onPause();
    relativeLayout.removeView(imageView);
}

class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;
    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
    }
    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        imageBitmap = result;
        bmImage.setImageBitmap(result);
        progressDialog.hide();
    }
}
}

任何人都可以对这个问题提出任何想法,或者看到我上面代码有任何问题吗?

崩溃日志:

10-15 16:59:35.436 6137-6538/com E/dalvikvm-heap: Out of memory on a 25137936-byte allocation.
10-15 16:59:35.436 6137-6538/com I/dalvikvm: "AsyncTask #4" prio=5 tid=15 RUNNABLE
10-15 16:59:35.436 6137-6538/com I/dalvikvm:   | group="main" sCount=0 dsCount=0 obj=0x44106838 self=0x5fcb1a80
10-15 16:59:35.436 6137-6538/com I/dalvikvm:   | sysTid=6538 nice=10 sched=0/0 cgrp=apps/bg_non_interactive handle=1606718640
10-15 16:59:35.436 6137-6538/com I/dalvikvm:   | state=R schedstat=( 101296084 23520127 71 ) utm=7 stm=2 core=1
10-15 16:59:35.436 6137-6538/com I/dalvikvm:     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
10-15 16:59:35.441 6137-6538/com I/dalvikvm:     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:623)
10-15 16:59:35.441 6137-6538/com I/dalvikvm:     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:696)
10-15 16:59:35.441 6137-6538/com I/dalvikvm:     at com.DetailScreen$DownloadImageTask.doInBackground(DetailScreen.java:198)
10-15 16:59:35.441 6137-6538/com I/dalvikvm:     at com.DetailScreen$DownloadImageTask.doInBackground(DetailScreen.java:188)
10-15 16:59:35.441 6137-6538/com I/dalvikvm:     at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-15 16:59:35.441 6137-6538/com I/dalvikvm:     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-15 16:59:35.441 6137-6538/com I/dalvikvm:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-15 16:59:35.441 6137-6538/com I/dalvikvm:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-15 16:59:35.441 6137-6538/com I/dalvikvm:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-15 16:59:35.441 6137-6538/com I/dalvikvm:     at java.lang.Thread.run(Thread.java:841)
10-15 16:59:35.446 6137-6146/com D/AbsListView: [unregisterDoubleTapMotionListener]
10-15 16:59:35.446 6137-6538/com D/skia: --- decoder->decode returned false
10-15 16:59:35.446 6137-6538/com W/dalvikvm: threadid=15: thread exiting with uncaught exception (group=0x41b7c700)
10-15 16:59:35.446 6137-6146/com I/MotionRecognitionManager:   .unregisterListener : / listener count = 0->0,  
10-15 16:59:35.446 6137-6146/com D/AbsListView: unregisterIRListener() is called 
10-15 16:59:35.451 6137-6538/com E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #4
10-15 16:59:35.451 6137-6538/com E/AndroidRuntime: java.lang.RuntimeException: An error occured while executing doInBackground()
10-15 16:59:35.451 6137-6538/com E/AndroidRuntime:     at android.os.AsyncTask$3.done(AsyncTask.java:299)
10-15 16:59:35.451 6137-6538/com E/AndroidRuntime:     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
10-15 16:59:35.451 6137-6538/com E/AndroidRuntime:     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
10-15 16:59:35.451 6137-6538/com E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
10-15 16:59:35.451 6137-6538/com E/AndroidRuntime:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-15 16:59:35.451 6137-6538/com E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-15 16:59:35.451 6137-6538/com E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-15 16:59:35.451 6137-6538/com E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:841)
10-15 16:59:35.451 6137-6538/com E/AndroidRuntime:  Caused by: java.lang.OutOfMemoryError
10-15 16:59:35.451 6137-6538/com E/AndroidRuntime:     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
10-15 16:59:35.451 6137-6538/com E/AndroidRuntime:     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:623)
10-15 16:59:35.451 6137-6538/com E/AndroidRuntime:     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:696)
10-15 16:59:35.451 6137-6538/com E/AndroidRuntime:     at com.DetailScreen$DownloadImageTask.doInBackground(DetailScreen.java:198)
10-15 16:59:35.451 6137-6538/com E/AndroidRuntime:     at com.DetailScreen$DownloadImageTask.doInBackground(DetailScreen.java:188)
10-15 16:59:35.451 6137-6538/com E/AndroidRuntime:     at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-15 16:59:35.451 6137-6538/com E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-15 16:59:35.451 6137-6538/com E/AndroidRuntime:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
10-15 16:59:35.451 6137-6538/com E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
10-15 16:59:35.451 6137-6538/com E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
10-15 16:59:35.451 6137-6538/com E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:841) 
10-15 16:59:35.456 6137-6146/com D/AbsListView: [unregisterDoubleTapMotionListener]
10-15 16:59:35.456 6137-6146/com I/MotionRecognitionManager:   .unregisterListener : / listener count = 0->0,  
10-15 16:59:35.456 6137-6146/com D/AbsListView: unregisterIRListener() is called 
10-15 16:59:35.456 6137-6146/com D/AbsListView: [unregisterDoubleTapMotionListener]
10-15 16:59:35.456 6137-6146/com I/MotionRecognitionManager:   .unregisterListener : / listener count = 0->0,  
10-15 16:59:35.456 6137-6146/com D/AbsListView: unregisterIRListener() is called 
10-15 16:59:35.456 6137-6146/com D/AbsListView: [unregisterDoubleTapMotionListener]
10-15 16:59:35.461 6137-6146/com I/MotionRecognitionManager:   .unregisterListener : / listener count = 0->0,  
10-15 16:59:35.461 6137-6146/com D/AbsListView: unregisterIRListener() is called 
10-15 16:59:42.706 6137-6137/com I/Choreographer: Skipped 422 frames!  The application may be doing too much work on its main thread.
10-15 16:59:43.536 6137-6137/com D/AbsListView: onDetachedFromWindow
10-15 16:59:43.566 6137-6137/com E/WindowManager: Activity com.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{44128b10 G.E..... R.....ID 0,0-684,192} that was originally added here
10-15 16:59:43.566 6137-6137/com E/WindowManager: android.view.WindowLeaked: Activity com.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{44128b10 G.E..... R.....ID 0,0-684,192} that was originally added here
10-15 16:59:43.566 6137-6137/com E/WindowManager:     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:452)
10-15 16:59:43.566 6137-6137/com E/WindowManager:     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:258)
10-15 16:59:43.566 6137-6137/com E/WindowManager:     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:73)
10-15 16:59:43.566 6137-6137/com E/WindowManager:     at android.app.Dialog.show(Dialog.java:287)
10-15 16:59:43.566 6137-6137/com E/WindowManager:     at com.DetailScreen.setValues(DetailScreen.java:95)
10-15 16:59:43.566 6137-6137/com E/WindowManager:     at com.DetailScreen.onResume(DetailScreen.java:179)
10-15 16:59:43.566 6137-6137/com E/WindowManager:     at android.support.v4.app.Fragment.performResume(Fragment.java:2005)
10-15 16:59:43.566 6137-6137/com E/WindowManager:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
10-15 16:59:43.566 6137-6137/com E/WindowManager:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1207)
10-15 16:59:43.566 6137-6137/com E/WindowManager:     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
10-15 16:59:43.566 6137-6137/com E/WindowManager:     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572)
10-15 16:59:43.566 6137-6137/com E/WindowManager:     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:493)
10-15 16:59:43.566 6137-6137/com E/WindowManager:     at android.os.Handler.handleCallback(Handler.java:730)
10-15 16:59:43.566 6137-6137/com E/WindowManager:     at android.os.Handler.dispatchMessage(Handler.java:92)
10-15 16:59:43.566 6137-6137/com E/WindowManager:     at android.os.Looper.loop(Looper.java:176)
10-15 16:59:43.566 6137-6137/com E/WindowManager:     at android.app.ActivityThread.main(ActivityThread.java:5419)
10-15 16:59:43.566 6137-6137/com E/WindowManager:     at java.lang.reflect.Method.invokeNative(Native Method)
10-15 16:59:43.566 6137-6137/com E/WindowManager:     at java.lang.reflect.Method.invoke(Method.java:525)
10-15 16:59:43.566 6137-6137/com E/WindowManager:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
10-15 16:59:43.566 6137-6137/com E/WindowManager:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
10-15 16:59:43.566 6137-6137/com E/WindowManager:     at dalvik.system.NativeStart.main(Native Method)
10-15 16:59:43.576 6137-6137/com E/WindowManager: Activity com.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{441be848 G.E..... R.....ID 0,0-684,192} that was originally added here
10-15 16:59:43.576 6137-6137/com E/WindowManager: android.view.WindowLeaked: Activity com.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{441be848 G.E..... R.....ID 0,0-684,192} that was originally added here
10-15 16:59:43.576 6137-6137/com E/WindowManager:     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:452)
10-15 16:59:43.576 6137-6137/com E/WindowManager:     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:258)
10-15 16:59:43.576 6137-6137/com E/WindowManager:     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:73)
10-15 16:59:43.576 6137-6137/com E/WindowManager:     at android.app.Dialog.show(Dialog.java:287)
10-15 16:59:43.576 6137-6137/com E/WindowManager:     at com.DetailScreen.setValues(DetailScreen.java:95)
10-15 16:59:43.576 6137-6137/com E/WindowManager:     at com.DetailScreen.onResume(DetailScreen.java:179)
10-15 16:59:43.576 6137-6137/com E/WindowManager:     at android.support.v4.app.Fragment.performResume(Fragment.java:2005)
10-15 16:59:43.576 6137-6137/com E/WindowManager:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
10-15 16:59:43.576 6137-6137/com E/WindowManager:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1207)
10-15 16:59:43.576 6137-6137/com E/WindowManager:     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
10-15 16:59:43.576 6137-6137/com E/WindowManager:     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572)
10-15 16:59:43.576 6137-6137/com E/WindowManager:     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:493)
10-15 16:59:43.576 6137-6137/com E/WindowManager:     at android.os.Handler.handleCallback(Handler.java:730)
10-15 16:59:43.576 6137-6137/com E/WindowManager:     at android.os.Handler.dispatchMessage(Handler.java:92)
10-15 16:59:43.576 6137-6137/com E/WindowManager:     at android.os.Looper.loop(Looper.java:176)
10-15 16:59:43.576 6137-6137/com E/WindowManager:     at android.app.ActivityThread.main(ActivityThread.java:5419)
10-15 16:59:43.576 6137-6137/com E/WindowManager:     at java.lang.reflect.Method.invokeNative(Native Method)
10-15 16:59:43.576 6137-6137/com E/WindowManager:     at java.lang.reflect.Method.invoke(Method.java:525)
10-15 16:59:43.576 6137-6137/com E/WindowManager:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
10-15 16:59:43.576 6137-6137/com E/WindowManager:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
10-15 16:59:43.576 6137-6137/com E/WindowManager:     at dalvik.system.NativeStart.main(Native Method)
10-15 16:59:43.586 6137-6137/com E/WindowManager: Activity com.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{44252ad8 G.E..... R.....ID 0,0-684,192} that was originally added here
10-15 16:59:43.586 6137-6137/com E/WindowManager: android.view.WindowLeaked: Activity com.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{44252ad8 G.E..... R.....ID 0,0-684,192} that was originally added here
10-15 16:59:43.586 6137-6137/com E/WindowManager:     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:452)
10-15 16:59:43.586 6137-6137/com E/WindowManager:     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:258)
10-15 16:59:43.586 6137-6137/com E/WindowManager:     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:73)
10-15 16:59:43.586 6137-6137/com E/WindowManager:     at android.app.Dialog.show(Dialog.java:287)
10-15 16:59:43.586 6137-6137/com E/WindowManager:     at com.DetailScreen.setValues(DetailScreen.java:95)
10-15 16:59:43.586 6137-6137/com E/WindowManager:     at com.DetailScreen.onResume(DetailScreen.java:179)
10-15 16:59:43.586 6137-6137/com E/WindowManager:     at android.support.v4.app.Fragment.performResume(Fragment.java:2005)
10-15 16:59:43.586 6137-6137/com E/WindowManager:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
10-15 16:59:43.586 6137-6137/com E/WindowManager:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1207)
10-15 16:59:43.586 6137-6137/com E/WindowManager:     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
10-15 16:59:43.586 6137-6137/com E/WindowManager:     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572)
10-15 16:59:43.586 6137-6137/com E/WindowManager:     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:493)
10-15 16:59:43.586 6137-6137/com E/WindowManager:     at android.os.Handler.handleCallback(Handler.java:730)
10-15 16:59:43.586 6137-6137/com E/WindowManager:     at android.os.Handler.dispatchMessage(Handler.java:92)
10-15 16:59:43.586 6137-6137/com E/WindowManager:     at android.os.Looper.loop(Looper.java:176)
10-15 16:59:43.586 6137-6137/com E/WindowManager:     at android.app.ActivityThread.main(ActivityThread.java:5419)
10-15 16:59:43.586 6137-6137/com E/WindowManager:     at java.lang.reflect.Method.invokeNative(Native Method)
10-15 16:59:43.586 6137-6137/com E/WindowManager:     at java.lang.reflect.Method.invoke(Method.java:525)
10-15 16:59:43.586 6137-6137/com E/WindowManager:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
10-15 16:59:43.586 6137-6137/com E/WindowManager:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
10-15 16:59:43.586 6137-6137/com E/WindowManager:     at dalvik.system.NativeStart.main(Native Method)
10-15 16:59:43.591 6137-6137/com E/WindowManager: Activity com.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{442e5b90 G.E..... R.....ID 0,0-684,192} that was originally added here
10-15 16:59:43.591 6137-6137/com E/WindowManager: android.view.WindowLeaked: Activity com.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{442e5b90 G.E..... R.....ID 0,0-684,192} that was originally added here
10-15 16:59:43.591 6137-6137/com E/WindowManager:     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:452)
10-15 16:59:43.591 6137-6137/com E/WindowManager:     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:258)
10-15 16:59:43.591 6137-6137/com E/WindowManager:     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:73)
10-15 16:59:43.591 6137-6137/com E/WindowManager:     at android.app.Dialog.show(Dialog.java:287)
10-15 16:59:43.591 6137-6137/com E/WindowManager:     at com.DetailScreen.setValues(DetailScreen.java:95)
10-15 16:59:43.591 6137-6137/com E/WindowManager:     at com.DetailScreen.onResume(DetailScreen.java:179)
10-15 16:59:43.591 6137-6137/com E/WindowManager:     at android.support.v4.app.Fragment.performResume(Fragment.java:2005)
10-15 16:59:43.591 6137-6137/com E/WindowManager:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
10-15 16:59:43.591 6137-6137/com E/WindowManager:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1207)
10-15 16:59:43.591 6137-6137/com E/WindowManager:     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
10-15 16:59:43.591 6137-6137/com E/WindowManager:     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572)
10-15 16:59:43.591 6137-6137/com E/WindowManager:     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:493)
10-15 16:59:43.591 6137-6137/com E/WindowManager:     at android.os.Handler.handleCallback(Handler.java:730)
10-15 16:59:43.591 6137-6137/com E/WindowManager:     at android.os.Handler.dispatchMessage(Handler.java:92)
10-15 16:59:43.591 6137-6137/com E/WindowManager:     at android.os.Looper.loop(Looper.java:176)
10-15 16:59:43.591 6137-6137/com E/WindowManager:     at android.app.ActivityThread.main(ActivityThread.java:5419)
10-15 16:59:43.591 6137-6137/com E/WindowManager:     at java.lang.reflect.Method.invokeNative(Native Method)
10-15 16:59:43.591 6137-6137/com E/WindowManager:     at java.lang.reflect.Method.invoke(Method.java:525)
10-15 16:59:43.591 6137-6137/com E/WindowManager:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
10-15 16:59:43.591 6137-6137/com E/WindowManager:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
10-15 16:59:43.591 6137-6137/com E/WindowManager:     at dalvik.system.NativeStart.main(Native Method)
10-15 16:59:43.596 6137-6137/com E/WindowManager: Activity com.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{4437be38 V.E..... R......D 0,0-684,192} that was originally added here
10-15 16:59:43.596 6137-6137/com E/WindowManager: android.view.WindowLeaked: Activity com.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{4437be38 V.E..... R......D 0,0-684,192} that was originally added here
10-15 16:59:43.596 6137-6137/com E/WindowManager:     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:452)
10-15 16:59:43.596 6137-6137/com E/WindowManager:     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:258)
10-15 16:59:43.596 6137-6137/com E/WindowManager:     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:73)
10-15 16:59:43.596 6137-6137/com E/WindowManager:     at android.app.Dialog.show(Dialog.java:287)
10-15 16:59:43.596 6137-6137/com E/WindowManager:     at com.DetailScreen.setValues(DetailScreen.java:95)
10-15 16:59:43.596 6137-6137/com E/WindowManager:     at com.DetailScreen.onResume(DetailScreen.java:179)
10-15 16:59:43.596 6137-6137/com E/WindowManager:     at android.support.v4.app.Fragment.performResume(Fragment.java:2005)
10-15 16:59:43.596 6137-6137/com E/WindowManager:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
10-15 16:59:43.596 6137-6137/com E/WindowManager:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1207)
10-15 16:59:43.596 6137-6137/com E/WindowManager:     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
10-15 16:59:43.596 6137-6137/com E/WindowManager:     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572)
10-15 16:59:43.596 6137-6137/com E/WindowManager:     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:493)
10-15 16:59:43.596 6137-6137/com E/WindowManager:     at android.os.Handler.handleCallback(Handler.java:730)
10-15 16:59:43.596 6137-6137/com E/WindowManager:     at android.os.Handler.dispatchMessage(Handler.java:92)
10-15 16:59:43.596 6137-6137/com E/WindowManager:     at android.os.Looper.loop(Looper.java:176)
10-15 16:59:43.596 6137-6137/com E/WindowManager:     at android.app.ActivityThread.main(ActivityThread.java:5419)
10-15 16:59:43.596 6137-6137/com E/WindowManager:     at java.lang.reflect.Method.invokeNative(Native Method)
10-15 16:59:43.596 6137-6137/com E/WindowManager:     at java.lang.reflect.Method.invoke(Method.java:525)
10-15 16:59:43.596 6137-6137/com E/WindowManager:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
10-15 16:59:43.596 6137-6137/com E/WindowManager:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
10-15 16:59:43.596 6137-6137/com E/WindowManager:     at dalvik.system.NativeStart.main(Native Method)

我知道php / mySQL部分工作正常。它似乎只是某些图像上的这个页面,应用程序将在加载微调器旋转时崩溃。我唯一可以看到的可能是因为照片的尺寸较大。

3 个答案:

答案 0 :(得分:1)

您的内存不足错误。我猜您正在下载大图像。 我更喜欢你使用this库。它非常棒且易于使用。 该库下载图像并放入设备的缓存中。如果您想了解更多详细信息,请随时写信。

答案 1 :(得分:0)

正如其他人所指出的那样,这次崩溃似乎与内存管理有关。

我发现可以通过添加属性来处理:

android:hardwareAccelerated="true"

作为清单中的活动标记。

有人可以评论这是否是解决这个崩溃问题的合法方法吗?

答案 2 :(得分:0)

当您下载图像时,会出现此问题,并且在下载某些图像后没有剩余的内存分配给您的应用程序。有各种图像加载库可以让您的工作更轻松:FrescoPicassoGlide等。

  1. 我建议您查看图片管理库(Facebook)Fresco与其他图片加载库相比非常棒且成熟。

    < / LI>
  2. Fresco拥有SimpleDraweeView作为自定义图片视图,支持Rounded Corners and Circles link并支持动画(.gif,.webp)以及普通图像(.jpg,.png) )。

  3. Fresco使用3层架构(BITMAP_MEMORY_CACHEENCODED_MEMORY_CACHEDISK_CACHE)处理图像的所有缓存。它还减少了OOM(Out Of Memory)问题。当视图中的图像离开屏幕时,它会自动回收位图,从而释放内存。

  4. 您可以通过在清单中添加android:largeHeap="true"来增加分配给您的应用程序的内存。

    注意:为您的应用程序增加heap并不被视为理想的解决方案。

    以下是Google解压缩的摘录,

      

    但是,请求大堆的功能仅适用于a   一小组应用程序,可以证明需要消耗更多的RAM(例如   作为一个大型照片编辑应用程序)。永远不要简单地请求大堆   因为你的内存耗尽,需要快速修复 - 你应该这样做   只有当你确切知道所有记忆的存在时才使用它   分配以及必须保留的原因。然而,即使你有信心   你的应用程序可以证明大堆的合理性,你应该避免请求它   尽可能的。使用额外的内存将越来越多   因为垃圾而损害整体用户体验   收集将花费更长时间,系统性能可能会更慢   任务切换或执行其他常见操作。

    这是文档https://developer.android.com/training/articles/memory.html

    的完整链接