setContentView()中的活动崩溃,布局有问题吗?

时间:2018-01-30 18:16:10

标签: android xml android-layout

我正在创建一个在我的播放器死亡时出现的新菜单。活动未加载,应用程序在下面指示的行崩溃。我所有的其他活动都非常好,我尽可能地遵循其他活动,但它仍然无法正常工作。

活动类:

public class DeathScreen extends Activity implements OnClickListener {
    //define buttons
    private Button btn_replay;
    private Button btn_menu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Hide title
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        //Hide top bar
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.deathscreen);

        btn_replay = findViewById(R.id.btn_replay);
        btn_menu = findViewById(R.id.btn_menu);
    }

    //invoke callback event in view
    @Override
    public void onClick(View v) {
        Intent intent = new Intent();

        switch (v.getId())
        {
        case R.id.btn_replay:
            intent.setClass(this, GamePage.class);
            break;

        case R.id.btn_menu:
            intent.setClass(this, Mainmenu.class);
            break;
        }

        startActivity(intent);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}

Logcat:

 projectname.companyname.com.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
 Process: projectname.companyname.com.myapplication, PID: 12025
 java.lang.RuntimeException: Unable to start activity ComponentInfo{projectname.companyname.com.myapplication/projectname.companyname.com.myapplication.DeathScreen}: android.view.InflateException: Binary XML file line #0: Error inflating class <unknown>
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
      at android.app.ActivityThread.access$800(ActivityThread.java:151)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:135)
      at android.app.ActivityThread.main(ActivityThread.java:5258)
      at java.lang.reflect.Method.invoke(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:372)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class <unknown>
      at android.view.LayoutInflater.createView(LayoutInflater.java:633)
      at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
      at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
      at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378)
      at android.app.Activity.setContentView(Activity.java:2145)
      at projectname.companyname.com.myapplication.DeathScreen.onCreate(DeathScreen.java:29)
      at android.app.Activity.performCreate(Activity.java:5990)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
      at android.app.ActivityThread.access$800(ActivityThread.java:151) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5258) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
 Caused by: java.lang.reflect.InvocationTargetException
      at java.lang.reflect.Constructor.newInstance(Native Method)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
      at android.view.LayoutInflater.createView(LayoutInflater.java:607)
      at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55) 
      at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682) 
      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741) 
      at android.view.LayoutInflater.inflate(LayoutInflater.java:482) 
      at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
      at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
      at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378) 
      at android.app.Activity.setContentView(Activity.java:2145) 
      at projectname.companyname.com.myapplication.DeathScreen.onCreate(DeathScreen.java:29) 
      at android.app.Activity.performCreate(Activity.java:5990) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
      at android.app.ActivityThread.access$800(ActivityThread.java:151) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5258) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
 Caused by: java.lang.OutOfMemoryError: Failed to allocate a 58286092 byte allocation with 16777168 free bytes and 52MB until OOM
      at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
      at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
      at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
      at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
      at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:988)
      at android.content.res.Resources.loadDrawableForCookie(Resources.java:2474)
      at android.content.res.Resources.loadDrawable(Resources.java:2381)
      at android.content.res.TypedArray.getDrawable(TypedArray.java:749)
      at android.view.View.<init>(View.java:3737)
      at android.view.ViewGroup.<init>(ViewGroup.java:497)
      at android.widget.RelativeLayout.<init>(RelativeLayout.java:248)
      at android.widget.RelativeLayout.<init>(RelativeLayout.java:244)
      at android.widget.RelativeLayout.<init>(RelativeLayout.java:240)
      at java.lang.reflect.Constructor.newInstance(Native Method) 
      at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
      at android.view.LayoutInflater.createView(LayoutInflater.java:607) 
      at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55) 
      at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682) 
      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741) 
      at android.view.LayoutInflater.inflate(LayoutInflater.java:482) 
      at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
      at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
      at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378) 
      at android.app.Activity.setContentView(Activity.java:2145) 
      at projectname.companyname.com.myapplication.DeathScreen.onCreate(DeathScreen.java:29) 
      at android.app.Activity.performCreate(Activity.java:5990) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
      at android.app.ActivityThread.access$800(ActivityThread.java:151) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5258) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/death_screen"
    tools:context="projectname.companyname.com.myapplication.DeathScreen">

    <Button
       android:id="@+id/btn_replay"
       android:layout_width="150dip"
       android:layout_height="40dip"
       android:layout_alignParentBottom="true"
       android:layout_marginBottom="150dip"
       android:layout_centerHorizontal="true"
       android:onClick="onClick"
       android:background="@drawable/replay_button" />

   <Button
       android:id="@+id/btn_menu"
       android:layout_width="150dip"
       android:layout_height="40dip"
       android:layout_alignParentBottom="true"
       android:layout_marginBottom="100dip"
       android:layout_centerHorizontal="true"
       android:onClick="onClick"
       android:background="@drawable/mainmenu_button" />

</RelativeLayout>

任何帮助或提示指出我正确的方向将不胜感激!感谢

1 个答案:

答案 0 :(得分:2)

在堆栈跟踪中,原始问题是

  

引起:java.lang.OutOfMemoryError:无法分配带有16777168个空闲字节的58286092字节分配和52MB直到OOM

这听起来像是一个如此大的图像,你的设备没有足够的内存来创建它。尝试从活动布局的根视图中删除此行:

android:background="@drawable/death_screen"

如果您在删除此属性后可以成功运行应用程序,您就会知道问题是此图像占用了太多内存。那时,这个网站上有很多关于图像和内存消耗的问题/答案。