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

谢谢你的帮助。

我的xml布局:
java - 引起原因:java.lang.OutOfMemoryError(开始新的 Activity )-LMLPHP

这是我的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();
            }
        });
    }


日志猫:

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

最佳答案

背景图像的文件大小太大。我不知道您的结构是什么样子,但是gogo1可能应该根据屏幕大小具有几种不同的分辨率。不仅如此,还应该对其进行移动优化,以便可以高效地加载它(并避免出现内存不足的异常)。

如果必须加载图像而不能压缩文件大小,则唯一的选择是将可绘制对象压缩到不会导致内存不足异常的大小后,以编程方式设置背景。

我的建议是修复gogo1,以便针对不同的屏幕尺寸对图像进行优化,并确保文件大小不超过〜100kb(甚至过大)。

09-27 23:59