试图去codeStream时出现错误充气类 [英] Error inflating class when trying to decodeStream

查看:212
本文介绍了试图去codeStream时出现错误充气类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时exausted。我整天都在做这个。在我的应用程序,我有100 ImageViews,但我发现了一个java.outofmemory错误,所以我决定去code和调整文件,但我不能管理它的工作。 有人可以采取在code看看,并建议我什么?

MainActivity code:     公共类MainActivity扩展活动实现OnClickListener {

  ImageView的显示;
INT toPhone;
私人上下文的背景下;

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    System.gc()的;
    上下文=这一点;
    toPhone = R.drawable.wal1;
    显示器=(ImageView的)findViewById(R.id.WPdisplay);
    ImageView的此搜索=(ImageView的)findViewById(R.id.WPimg1);
    ImageView的IMAGE2 =(ImageView的)findViewById(R.id.WPimg2);
    ImageView的的Image3 =(ImageView的)findViewById(R.id.WPimg3);
    ImageView的为image4 =(ImageView的)findViewById(R.id.WPimg4);
    ImageView的image5 =(ImageView的)findViewById(R.id.WPimg5);
    按钮setWall =(按钮)findViewById(R.id.BsetWall);
    image1.setOnClickListener(本);
    image2.setOnClickListener(本);
    image3.setOnClickListener(本);
    image4.setOnClickListener(本);
    image5.setOnClickListener(本);
    setWall.setOnClickListener(本);
    }

@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
    //充气菜单;这增加了项目操作栏,如果它是present。
    。getMenuInflater()膨胀(R.menu.main,菜单);
    返回true;
}

公共位图德codeAndResizeFile(INT渣油){
     尝试 {
            //德code图像尺寸
            BitmapFactory.Options O =新BitmapFactory.Options();
            o.inJustDe codeBounds = TRUE;
 

我认为在未来的行我有一个问题,因为它说的不使用局部变量BMP的值

  BMP位= BitmapFactory.de codeResource(context.getResources(),渣油,O);
           // BitmapFactory.de codeStream(新的FileInputStream(f)项,空,O);

            //我们希望新的大小扩展到
            最终诠释REQUIRED_SIZE = 70;

            //找到正确的比例值。它应该是2的幂。
            INT width_tmp = o.outWidth,height_tmp = o.outHeight;
            int标= 1;
            而(真){
                如果(width_tmp / 2'; REQUIRED_SIZE
                        || height_tmp / 2版; REQUIRED_SIZE)
                    打破;
                width_tmp / = 2;
                height_tmp / = 2;
                规模* = 2;
            }
            BitmapFactory.Options O2 =新BitmapFactory.Options();
            o2.inSampleSize =规模;
            返回BitmapFactory.de codeResource(context.getResources(),渣油,O2);

     } 最后 {

        }
    }

@覆盖
公共无效的onClick(视图v){
    位图BMPP;
    开关(v.getId()){
    案例R.id.WPimg1:
         display.setImageResource(R.drawable.wal1);
         toPhone = R.drawable.wal1;
 

在下一行我试图去code它(我这样做是为每个图像)

  BMPP =去codeAndResizeFile(toPhone);
         //位图BMPP =去codeAndResizeFile(文件);
         打破;
    案例R.id.WPimg2:
         display.setImageResource(R.drawable.wal2);
         toPhone = R.drawable.wal2;
         BMPP =去codeAndResizeFile(toPhone);
        display.setImageBitmap(BMPP);
         打破;
    案例R.id.WPimg3:
         display.setImageResource(R.drawable.wal3);
         toPhone = R.drawable.wal3;
         BMPP =去codeAndResizeFile(toPhone);
         打破;
    案例R.id.WPimg4:
         display.setImageResource(R.drawable.wal4);
         toPhone = R.drawable.wal4;
         BMPP =去codeAndResizeFile(toPhone);
         打破;
    案例R.id.WPimg5:
         display.setImageResource(R.drawable.wal5);
         toPhone = R.drawable.wal5;
         BMPP =去codeAndResizeFile(toPhone);
         打破;
    案例R.id.BsetWall:
         尝试{
             WallpaperManager.getInstance(getApplicationContext())setResource(toPhone)。
             Toast.makeText(getApplicationContext(),壁纸设置!,Toast.LENGTH_SHORT).show();
         }赶上(IOException异常E){
             e.printStackTrace();
             Toast.makeText(getApplicationContext(),无特权!,Toast.LENGTH_SHORT).show();
         }
         打破;
    }
    }
    }
 

这是我的logcat

  ---德codeR->德code返回false
关闭虚拟机
主题ID = 1:螺纹未捕获异常退出(组= 0xb3adbba8)
致命异常:主要
工艺:app.technozed.testwall,PID:1250
java.lang.RuntimeException的:无法启动的活动ComponentInfo {app.technozed.testwall / app.technozed.testwall.MainActivity}:android.view.InflateException:二进制XML文件中的行#304:错误充气类<未知>
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
在android.app.ActivityThread.access $ 800(ActivityThread.java:135)
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196)
在android.os.Handler.dispatchMessage(Handler.java:102)
在android.os.Looper.loop(Looper.java:136)
在android.app.ActivityThread.main(ActivityThread.java:5017)
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect.Method.invoke(Method.java:515)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
在dalvik.system.NativeStart.main(本机方法)
致:android.view.InflateException:二进制XML文件中的行#304:错误充气类<未知>
在android.view.LayoutInflater.createView(LayoutInflater.java:620)
在com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
在android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
在android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
在android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
在android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
在android.view.LayoutInflater.inflate(LayoutInflater.java:492)
在android.view.LayoutInflater.inflate(LayoutInflater.java:397)
在android.view.LayoutInflater.inflate(LayoutInflater.java:353)
在com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
在android.app.Activity.setContentView(Activity.java:1929)
在app.technozed.testwall.MainActivity.onCreate(MainActivity.java:28)
在android.app.Activity.performCreate(Activity.java:5231)
在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
... 11更多
java.lang.reflect.InvocationTargetException:产生的原因
在java.lang.reflect.Constructor.constructNative(本机方法)
在java.lang.reflect.Constructor.newInstance(Constructor.java:423)
在android.view.LayoutInflater.createView(LayoutInflater.java:594)
... 26更多
java.lang.OutOfMemoryError:产生的原因
在android.graphics.BitmapFactory.nativeDe $ C $检测板(本机方法)
在android.graphics.BitmapFactory.de codeStream(BitmapFactory.java:587)
在android.graphics.BitmapFactory.de codeResourceStream(BitmapFactory.java:422)
在android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
在android.content.res.Resources.loadDrawable(Resources.java:2110)
在android.content.res.TypedArray.getDrawable(TypedArray.java:602)
在android.widget.ImageView< INIT>(ImageView.java:129)
在android.widget.ImageView< INIT>(ImageView.java:119)
... 29更多
 

解决方案

如果你有预警德codeAndResizeFile()'局部变量的BMP未使用的价值',你可以删除'BMP位图='的一部分,并把它作为:

  BitmapFactory.de codeResource(context.getResources(),渣油,O);
 

这是因为这个调用是o.inJustDe codeBounds = TRUE。当是这样的话,只有尺寸被返回对象的'o'和空的位图则返回(因此没有点存储它)。

在你的switch语句,更改每一种情况是这样的:

 案例R.id.WPimg1:
    toPhone = R.drawable.wal1;
    BMPP =去codeAndResizeFile(toPhone);
    display.setImageBitmap(BMPP);
    打破;
 

所以在这里,你去code中的资源变成一个按比例缩小的位图,然后分配位图到ImageView的。你不想要或需要做的:

  display.setImageResource(R.drawable.wal1);
 

......,因为你冒着一个OutOfMemoryException,你去上设置缩放位图显示反正。在作出这些改变,你的code对我的作品。我可以看到壁纸preVIEW图像,然后设置壁纸(不要忘了在清单中SET_WALLPAPER权限)。

然而,这并不说明你的logcat这似乎是一个问题的onCreate()膨胀R.layout.activity_main。对你的logcat年底,这再次说明一个OutOfMemoryError。如果我给你一个大的JPG,以XML中的一个ImageView的(一个OOM结果和布局,因此不能被夸大),我可以重新创建此错误。你不能缩小对即时',当他们在XML中分配可绘,所以在XML中使用创建体积更小的可绘制。

编辑:

当您单击图像上,显示屏获得一个新的位图和旧有资格为GC。这里有一个方法来释放由旧马上使用的内存,而不必等待GC在踢:

从的onClick()移动这是一个类变量来代替:

 位图BMPP; //参照同位图显示'正在使用
 

然后在的onClick():

 位图old_bm = NULL; //引用的方式保存旧的位图
开关(v.getId()){
案例R.id.WPimg1:
    old_bm = BMPP; //设置到当前正在使用的显示器位图
    toPhone = R.drawable.wal1;
    BMPP =去codeAndResizeFile(toPhone); //'显示'现在有一个新的位图
    display.setImageBitmap(BMPP);
    如果(old_bm!= NULL)
    {
        old_bm.recycle(); // Recyle旧的位图
        old_bm = NULL; //清除参考,让GC全面清理
    }
    打破;
 

i'm exausted. I'm working on this all day. In my app i have 100 ImageViews, but i'm getting a java.outofmemory error so i decided to decode and resize file, but i can not manage it to work. Can someone take a look in the code and suggest me anything?

MainActivity code: public class MainActivity extends Activity implements OnClickListener {

ImageView display;
int toPhone;
private Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    System.gc();
    context = this;
    toPhone = R.drawable.wal1;
    display = (ImageView) findViewById(R.id.WPdisplay);
    ImageView image1 = (ImageView) findViewById(R.id.WPimg1);
    ImageView image2 = (ImageView) findViewById(R.id.WPimg2);
    ImageView image3 = (ImageView) findViewById(R.id.WPimg3);
    ImageView image4 = (ImageView) findViewById(R.id.WPimg4);
    ImageView image5 = (ImageView) findViewById(R.id.WPimg5);
    Button setWall = (Button) findViewById(R.id.BsetWall);
    image1.setOnClickListener(this);
    image2.setOnClickListener(this);
    image3.setOnClickListener(this);
    image4.setOnClickListener(this);
    image5.setOnClickListener(this);
    setWall.setOnClickListener(this);
    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public Bitmap decodeAndResizeFile(int resID) {
     try {
            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;

i think in the next line i have a problem because it says The value of the local variable bmp is not used

            Bitmap bmp= BitmapFactory.decodeResource(context.getResources(), resID, o);
           // BitmapFactory.decodeStream(new FileInputStream(f), null, o);

            // The new size we want to scale to
            final int REQUIRED_SIZE = 70;

            // Find the correct scale value. It should be the power of 2.
            int width_tmp = o.outWidth, height_tmp = o.outHeight;
            int scale = 1;
            while (true) {
                if (width_tmp / 2 < REQUIRED_SIZE
                        || height_tmp / 2 < REQUIRED_SIZE)
                    break;
                width_tmp /= 2;
                height_tmp /= 2;
                scale *= 2;
            }
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeResource(context.getResources(), resID, o2);

     } finally {

        }
    }

@Override
public void onClick(View v) {
    Bitmap bmpp;
    switch (v.getId()){
    case R.id.WPimg1:
         display.setImageResource(R.drawable.wal1);
         toPhone = R.drawable.wal1;

in the next line i'm trying to decode it(and i'm doing this for each image)

         bmpp = decodeAndResizeFile(toPhone);
         //Bitmap bmpp = decodeAndResizeFile(file);
         break;
    case R.id.WPimg2:
         display.setImageResource(R.drawable.wal2);
         toPhone = R.drawable.wal2;  
         bmpp = decodeAndResizeFile(toPhone);
        display.setImageBitmap(bmpp);
         break;
    case R.id.WPimg3:
         display.setImageResource(R.drawable.wal3);
         toPhone = R.drawable.wal3;
         bmpp = decodeAndResizeFile(toPhone);
         break;
    case R.id.WPimg4:
         display.setImageResource(R.drawable.wal4);
         toPhone = R.drawable.wal4;
         bmpp = decodeAndResizeFile(toPhone);
         break;
    case R.id.WPimg5:
         display.setImageResource(R.drawable.wal5);
         toPhone = R.drawable.wal5;
         bmpp = decodeAndResizeFile(toPhone);
         break;
    case R.id.BsetWall:
         try{
             WallpaperManager.getInstance(getApplicationContext()).setResource(toPhone);
             Toast.makeText(getApplicationContext(), "Wallpaper was set!", Toast.LENGTH_SHORT).show();
         } catch(IOException e) {
             e.printStackTrace();
             Toast.makeText(getApplicationContext(), "No privileges!", Toast.LENGTH_SHORT).show();
         }
         break;
    }
    }
    }

here is my Logcat

--- decoder->decode returned false
Shutting down VM
threadid=1: thread exiting with uncaught exception (group=0xb3adbba8)
FATAL EXCEPTION: main
Process: app.technozed.testwall, PID: 1250
java.lang.RuntimeException: Unable to start activity ComponentInfo{app.technozed.testwall/app.technozed.testwall.MainActivity}: android.view.InflateException: Binary XML file line #304: Error inflating class <unknown>
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #304: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:620)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
at android.app.Activity.setContentView(Activity.java:1929)
at app.technozed.testwall.MainActivity.onCreate(MainActivity.java:28)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
... 11 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:594)
... 26 more
Caused by: java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
at android.content.res.Resources.loadDrawable(Resources.java:2110)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
at android.widget.ImageView.<init>(ImageView.java:129)
at android.widget.ImageView.<init>(ImageView.java:119)
... 29 more

解决方案

Where you have the warning 'The value of the local variable bmp is not used' in decodeAndResizeFile(), you can remove the 'Bitmap bmp =' part and leave it as:

BitmapFactory.decodeResource(context.getResources(), resID, o);

This is because this call is with o.inJustDecodeBounds = true. When that is the case, only the dimensions are returned in object 'o' and a null bitmap is returned (so there's no point storing it).

In your switch statement, change each case to be like the following:

case R.id.WPimg1:
    toPhone = R.drawable.wal1;
    bmpp = decodeAndResizeFile(toPhone);
    display.setImageBitmap(bmpp);
    break;

So here, you decode the resource into a scaled down bitmap and then assign that bitmap to the ImageView. You don't want or need to do this:

display.setImageResource(R.drawable.wal1);

...since you're risking an OutOfMemoryException and you go on to set 'display' with the scaled bitmap anyway. Having made those changes, your code works for me. I can see the wallpaper preview images and then set the wallpaper (don't forget the SET_WALLPAPER permission in the manifest).

That doesn't however explain your logcat which appears to be a problem in onCreate() inflating R.layout.activity_main. Towards the end of your logcat, it shows an OutOfMemoryError again. I can re-create this error if I assign a large jpg to an ImageView in the XML (an OOM results and the layout therefore cannot be inflated). You cannot scale down drawables 'on-the-fly' when they are assigned in XML, so create physically smaller drawables for use within the XML.

EDIT:

When you click on an image, 'display' gets a new bitmap and the old one is eligible for GC. Here is one approach for releasing the memory used by the old one right away, without waiting for GC to kick in:

Move this from onClick() to being a class variable instead:

Bitmap bmpp;  //Reference to the same bitmap that 'display' is using

Then in onClick():

Bitmap old_bm = null;  //Reference to hold the old bitmap
switch (v.getId()){
case R.id.WPimg1:
    old_bm = bmpp;  //Set to the bitmap currently being used by 'display'
    toPhone = R.drawable.wal1;
    bmpp = decodeAndResizeFile(toPhone);   //'display' now has a new bitmap
    display.setImageBitmap(bmpp);
    if (old_bm != null)
    {
        old_bm.recycle();  //Recyle the old bitmap
        old_bm = null;     //Clear the reference to allow GC to clean up fully
    }
    break;

这篇关于试图去codeStream时出现错误充气类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆