应用程序在创建视图时崩溃(android.view.InflateException)

时间:2016-04-19 17:54:52

标签: java android

public class ChildrensList extends Fragment   {

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

        View rootView = inflater.inflate(R.layout.childrens_list,container,false);
        //return rootView;

        ImageButton pigsButton = (ImageButton) rootView.findViewById(R.id.pigsButton);
        ImageButton jackButton = (ImageButton) rootView.findViewById(R.id.jacksButton);
        ImageButton hansgretButton = (ImageButton) rootView.findViewById(R.id.hansgretButton);
        ImageButton mermadButton = (ImageButton) rootView.findViewById(R.id.mermaidButton);
        ImageButton rapButton = (ImageButton) rootView.findViewById(R.id.rapunzalButton);
        ImageButton redRidButton = (ImageButton) rootView.findViewById(R.id.ridingButton);
        ImageButton threeBearsButton = (ImageButton) rootView.findViewById(R.id.bearsButton);
        ImageButton ugduckButton = (ImageButton) rootView.findViewById(R.id.duckButton);

        final MediaPlayer mp = MediaPlayer.create(getContext(), R.raw.xlophone);

        pigsButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(ChildrensList.this.getActivity(),pigs_story.class);
                mp.start();
                startActivity(i);
            }
        });

        jackButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(ChildrensList.this.getActivity(), jack_beanstalk.class);
                mp.start();
                startActivity(i);
            }
        });

        hansgretButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(ChildrensList.this.getActivity(), hansel_and_gretal.class);
                mp.start();
                startActivity(i);
            }
        });
        mermadButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(ChildrensList.this.getActivity(), mermaid.class);
                mp.start();
                startActivity(i);
            }
        });
        rapButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(ChildrensList.this.getActivity(), rapunzal.class);
                mp.start();
                startActivity(i);
            }
        });
        redRidButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(ChildrensList.this.getActivity(), RedRidingHood.class);
                mp.start();
                startActivity(i);
            }
        });
        threeBearsButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(ChildrensList.this.getActivity(), ThreeBears.class);
                mp.start();
                startActivity(i);
            }
        });
        ugduckButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(ChildrensList.this.getActivity(), UglyDuckling.class);
                mp.start();
                startActivity(i);
            }
        });


        return rootView; //return rootview

    }

}

我在控制台中出现了一些错误并且在如何解决这些问题时遇到了困难,其中一个是致命的内存异常,但是列表很长!有人可以对此有所了解并帮助摆脱错误

04-19 18:47:29.392 19101-19101/com.j2d.com.bedtimestories E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.j2d.com.bedtimestories, PID: 19101
    android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class android.widget.ScrollView
        at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
        at com.j2d.com.bedtimestories.ChildrensList.onCreateView(ChildrensList.java:25)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:158)
        at android.app.ActivityThread.main(ActivityThread.java:7224)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
     Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.widget.ScrollView
        at android.view.LayoutInflater.createView(LayoutInflater.java:657)
        at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:706)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:774)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:429) 
        at com.j2d.com.bedtimestories.ChildrensList.onCreateView(ChildrensList.java:25) 
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974) 
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252) 
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738) 
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617) 
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517) 
        at android.os.Handler.handleCallback(Handler.java:739) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:158) 
        at android.app.ActivityThread.main(ActivityThread.java:7224) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at android.view.LayoutInflater.createView(LayoutInflater.java:631)
        at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58) 
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:706) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:774) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:498) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:429) 
        at com.j2d.com.bedtimestories.ChildrensList.onCreateView(ChildrensList.java:25) 
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974) 
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252) 
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738) 
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617) 
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517) 
        at android.os.Handler.handleCallback(Handler.java:739) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:158) 
        at android.app.ActivityThread.main(ActivityThread.java:7224) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
     Caused by: java.lang.OutOfMemoryError: Failed to allocate a 22075212 byte allocation with 16777216 free bytes and 17MB until OOM
        at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
        at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
        at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:856)
        at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:675)
        at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:2228)
        at android.content.res.Resources.loadDrawableForCookie(Resources.java:4211)
        at android.content.res.Resources.loadDrawable(Resources.java:4085)
        at android.content.res.Resources.loadDrawable(Resources.java:3935)
        at android.content.res.TypedArray.getDrawable(TypedArray.java:886)
        at android.view.View.<init>(View.java:4225)
        at android.view.ViewGroup.<init>(ViewGroup.java:589)
        at android.widget.FrameLayout.<init>(FrameLayout.java:97)
        at android.widget.ScrollView.<init>(ScrollView.java:347)
        at android.widget.ScrollView.<init>(ScrollView.java:343)
        at android.widget.ScrollView.<init>(ScrollView.java:339)
        at java.lang.reflect.Constructor.newInstance(Native Method) 
        at android.view.LayoutInflater.createView(LayoutInflater.java:631) 
        at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58) 
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:706) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:774) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:498) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:429) 
        at com.j2d.com.bedtimestories.ChildrensList.onCreateView(ChildrensList.java:25) 
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974) 
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252) 
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738) 
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617) 
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517) 
        at android.os.Handler.handleCallback(Handler.java:739) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:158) 
        at android.app.ActivityThread.main(ActivityThread.java:7224) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

1 个答案:

答案 0 :(得分:1)

您发布的错误的关键部分是:

 Caused by: java.lang.OutOfMemoryError: Failed to allocate a 22075212 byte allocation with 16777216 free bytes and 17MB until OOM
    [...]
    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:856)
    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:675)
    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:2228)
    [...]
    at android.view.LayoutInflater.inflate(LayoutInflater.java:429) 
    at com.j2d.com.bedtimestories.ChildrensList.onCreateView(ChildrensList.java:25) 
    [...]

这表示您的应用内存不足。在尝试加载图片时内存不足,而ChildrensList方法中的onCreateView()类称为.inflate()

正如其他人在您的问题评论中指出的那样,您的应用中可能有非常大的图像文件。您应该将图像缩放到适当的大小以避免此问题。

Android documentation有一篇讨论图片尺寸的文章。