1

显示自定义进度对话框时遇到错误。我创建了包含 40 个不同图像的进度对话框。它炒得很好。但是如果我尝试显示对话框超过 8-10 次应用程序崩溃。

错误日志

09-20 13:11:12.620: E/AndroidRuntime(9156): FATAL EXCEPTION: main
09-20 13:11:12.620: E/AndroidRuntime(9156): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.spec.vespa/com.spec.vespa.view.SettingsActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class <unknown>
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.app.ActivityThread.access$1500(ActivityThread.java:121)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.os.Looper.loop(Looper.java:138)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.app.ActivityThread.main(ActivityThread.java:3701)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at java.lang.reflect.Method.invokeNative(Native Method)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at java.lang.reflect.Method.invoke(Method.java:507)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at dalvik.system.NativeStart.main(Native Method)
09-20 13:11:12.620: E/AndroidRuntime(9156): Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class <unknown>
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.view.LayoutInflater.createView(LayoutInflater.java:518)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.app.Dialog.setContentView(Dialog.java:421)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at com.spec.vespa.view.SettingsActivity.onCreate(SettingsActivity.java:124)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
09-20 13:11:12.620: E/AndroidRuntime(9156):     ... 11 more
09-20 13:11:12.620: E/AndroidRuntime(9156): Caused by: java.lang.reflect.InvocationTargetException
09-20 13:11:12.620: E/AndroidRuntime(9156):     at java.lang.reflect.Constructor.constructNative(Native Method)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.view.LayoutInflater.createView(LayoutInflater.java:505)
09-20 13:11:12.620: E/AndroidRuntime(9156):     ... 23 more
09-20 13:11:12.620: E/AndroidRuntime(9156): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.graphics.Bitmap.nativeCreate(Native Method)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.graphics.Bitmap.createBitmap(Bitmap.java:477)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.graphics.Bitmap.createBitmap(Bitmap.java:444)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:349)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:521)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:496)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:359)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:715)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.content.res.Resources.loadDrawable(Resources.java:1721)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.content.res.Resources.getDrawable(Resources.java:585)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:267)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:805)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.graphics.drawable.Drawable.createFromXml(Drawable.java:746)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.content.res.Resources.loadDrawable(Resources.java:1706)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.widget.ProgressBar.<init>(ProgressBar.java:215)
09-20 13:11:12.620: E/AndroidRuntime(9156):     at android.widget.ProgressBar.<init>(ProgressBar.java:170)
09-20 13:11:12.620: E/AndroidRuntime(9156):     ... 26 more

我还做了一些内存管理,如下所述:

public 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));
            }
            try{
                ((ViewGroup) view).removeAllViews();
            }catch (UnsupportedOperationException e) {}
        }
    }

我错过了什么?

进度条xml:

<RelativeLayout android:layout_width="fill_parent"
android:layout_marginLeft="40dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/progress_context">
<ProgressBar android:layout_height="100dp"
           android:layout_width="100dp"
           android:id="@+id/prg"
           android:layout_centerHorizontal="true"
           android:layout_marginTop="10dp"
           android:drawingCacheQuality="low"
           style="@android:style/Widget.ProgressBar.Large.Inverse"
           android:indeterminateDrawable="@drawable/custom_progress_drawable"/>

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/prg"
    android:layout_marginTop="15dp"
    android:textSize="20sp"
    android:textColor="#ffffff"
    android:text="@string/loading"
    android:paddingBottom="10dp"
    android:id="@+id/txtProgressMessage"
    android:layout_centerHorizontal="true"/>
</RelativeLayout>

4

2 回答 2

0

我知道了..

我的代码:

public class CustomProgressDialog extends ProgressDialog{
    private ImageView imageView;
    public CustomProgressDialog(Context context) {      
        super(context);
    }

 @Override
    public void onWindowFocusChanged(boolean hasFocus){
        imageView = (ImageView) findViewById(R.id.loadingImageView);
        AnimationDrawable myAnimation = (AnimationDrawable) imageView.getBackground();
        myAnimation.start();
    }

    public static CustomProgressDialog createDialog(Context context,String title,String message){
        CustomProgressDialog dialog=null;
        if(title.length()==0||title==null)
            dialog=new CustomProgressDialog(context);
        else{
            dialog=new CustomProgressDialog(context);
            dialog.setTitle(title);
        }
        dialog.getWindow().getAttributes().gravity=Gravity.CENTER;
        return dialog;
    }

 public void setImageCallBack(){
     if(imageView != null)
         imageView.getBackground().setCallback(null);
 }

}

于 2012-09-28T11:11:51.780 回答
0

您的应用程序崩溃,因为您的 JVM 内存不足并且无法创建/加载另一个位图。可能的解决方案是:

  1. 使用已创建且未使用的位图对象,例如使用“recycle()”方法,关于在 Android 中使用位图请阅读:http: //developer.android.com/reference/android/graphics/Bitmap.html

  2. 压缩位图,不要创建大量位图并重用已创建的位图而不是创建新的位图。

PS如果您提供更多代码,您可以获得解决您问题的代码。

于 2012-09-20T09:27:18.210 回答