引起:java.lang.OutOfMemoryError(启动新的Activity)

时间:2016-09-02 21:56:55

标签: java android android-studio out-of-memory

我的应用程序包含5个以上的后台服务活动。当我运行应用程序并单击按钮创建第二个活动时,应用程序崩溃。

感谢您的帮助。

我的xml布局: enter image description here

这是我的onCreate:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.button1);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                startActivity(intent);
                finish();
            }
        });
    }

logcat:

E/AndroidRuntime: FATAL EXCEPTION: main 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.apollo.arrroo/com.example.apollo.arrroo.Main2Activity}: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2099)
                                                                           at android.app.ActivityThread.access$600(ActivityThread.java:138)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                           at android.os.Looper.loop(Looper.java:137)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:4929)
                                                                           at java.lang.reflect.Method.invokeNative(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:511)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
                                                                           at dalvik.system.NativeStart.main(Native Method)
                                                                        Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
                                                                           at android.view.LayoutInflater.createView(LayoutInflater.java:613)
                                                                           at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
                                                                           at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
                                                                           at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
                                                                           at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
                                                                           at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                           at com.example.apollo.arrroo.Main2Activity.onCreate(Main2Activity.java:11)
                                                                           at android.app.Activity.performCreate(Activity.java:5254)
                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2038)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2099) 
                                                                           at android.app.ActivityThread.access$600(ActivityThread.java:138) 
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205) 
                                                                           at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                           at android.os.Looper.loop(Looper.java:137) 
                                                                           at android.app.ActivityThread.main(ActivityThread.java:4929) 
                                                                           at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                           at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798) 
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565) 
                                                                           at dalvik.system.NativeStart.main(Native Method) 
                                                                        Caused by: java.lang.reflect.InvocationTargetException
                                                                           at java.lang.reflect.Constructor.constructNative(Native Method)
                                                                           at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
                                                                           at android.view.LayoutInflater.createView(LayoutInflater.java:587)
                                                                           at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 
                                                                           at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660) 
                                                                           at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685) 
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:466) 
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
                                                                           at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280) 
                                                                           at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
                                                                           at com.example.apollo.arrroo.Main2Activity.onCreate(Main2Activity.java:11) 
                                                                           at android.app.Activity.performCreate(Activity.java:5254) 
                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082) 
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2038) 
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2099) 
                                                                           at android.app.ActivityThread.access$600(ActivityThread.java:138) 
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205) 
                                                                           at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                           at android.os.Looper.loop(Looper.java:137) 
                                                                           at android.app.ActivityThread.main(ActivityThread.java:4929) 
                                                                           at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                           at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798) 
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565) 
                                                                           at dalvik.system.NativeStart.main(Native Method) 
                                                                        Caused by: java.lang.OutOfMemoryError
                                                                           at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                                                                           at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:500)
                                                                           at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:353)
                                                                           at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:781)
                                                                           at android.content.res.Resources.loadDrawable(Resources.java:1969)
                                                                           at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
                                                                           at android.view.View.<init>(View.java:3348)
                                                                           at android.view.View.<init>(View.java:3285)
                                                                           at android.view.ViewGroup.<init>(ViewGroup.java:421)
                                                                           at android.widget.RelativeLayout.<init>(RelativeLayout.java:187)
                                                                           at java.lang.reflect.Constructor.constructNative(Native Method) 
                                                                           at java.lang.reflect.Constructor.newInstance(Constructor.java:417) 
                                                                           at android.view.LayoutInflater.createView(LayoutInflater.java:587) 
                                                                           at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 
                                                                           at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660) 
                                                                           at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685) 
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:466) 
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
                                                                           at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280) 
                                                                           at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
                                                                           at com.example.apollo.arrroo.Main2Activity.onCreate(Main2Activity.java:11) 
 // even more

2 个答案:

答案 0 :(得分:0)

您的背景图片的文件太大。我不知道你的结构是什么样的,但gogo1可能根据屏幕尺寸有几种不同的分辨率。不仅如此,它应该是移动优化的,以便可以有效地加载(并避免内存不足)。

如果您必须加载图像并且无法压缩文件大小,那么您唯一的选择是在将drawable压缩到不会导致内存不足异常的大小之后以编程方式设置背景。

我的建议是修复gogo1,因此图片针对不同的屏幕尺寸进行了优化,同时确保其文件大小不超过~100kb(即使是过度杀伤)。

答案 1 :(得分:0)

如果由于大图像(jpeg)导致内存不足,我解决了这个问题,你必须使用FormatFactory软件。首先你在左边看到:(图片)点击它并选择:all to jpg ---&amp; -click on - output setting&amp;选择(最大宽度1024最大高度768)。 &安培;最后点击添加文件从桌面选择你的图像,然后单击确定。然后单击开始。