简体   繁体   English

xml布局在为Android应用加载时崩溃

[英]xml layout crashes on loading for android app

enter image description here Hey guys I'm at the end of my Java 1 class working on my project. 大家好,我正在Java 1课程的最后阶段。 We are making a memory/concentration game. 我们正在做一个记忆/集中游戏。 My issue is that when I click the easy button for the next activity the app crashes. 我的问题是,当我单击“轻松”按钮进行下一个活动时,应用程序崩溃了。 I have tried using fragments and activities and just can't seem to get it right. 我已经尝试过使用片段和活动,但似乎无法正确解决。 I have also tried using the layout I need on my main activity just to see if I could get it to display. 我还尝试在主要活动中使用所需的布局,以查看是否可以显示它。 Even then it just crashes on startup of the app. 即使这样,它也只会在应用程序启动时崩溃。 Any help would be appreciated. 任何帮助,将不胜感激。

Startup Screen activity. 启动屏幕活动。

package com.bignerdranch.android.memory;


import android.app.Activity;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;



public class MemoryActivity extends Activity {

private Button mEasy;
private Button mMedium;
private Button mHard;
private CheckBox mSilence;
public MediaPlayer player;


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


    player = new MediaPlayer();
    player = MediaPlayer.create(this, R.raw.mkstartmusic);
    player.setAudioStreamType(AudioManager.STREAM_MUSIC);
    player.setLooping(true);
    player.start();



    mEasy = (Button)findViewById(R.id.easy);
    mEasy.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent easy = new Intent(getApplicationContext(), EasyGame.class);
            startActivity(easy);

        }
    });

    mMedium = (Button)findViewById(R.id.medium);
    mMedium.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });

    mHard = (Button)findViewById(R.id.hard);
    mHard.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });

    mSilence = (CheckBox)findViewById(R.id.silence);
    mSilence.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if(mSilence.isChecked()) {
                player.pause();
            } else if(mSilence.isChecked() == false) {
                player.start();
            }

        }
    });

}

@Override
protected void onStop() {
    super.onPause();
    if (player != null){
        player.stop();
        if (isFinishing()){
        player.stop();
        player.release();
        }
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.layout.activity_memory, menu);
    return true;


}

}

Second Activity (Easy option) 第二次活动(轻松选项)

package com.bignerdranch.android.memory;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;

public class EasyGame extends Activity {

private ImageButton buttOne;
private ImageButton buttTwo;
private ImageButton buttThree;
private ImageButton buttFour;

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

    buttOne = (ImageButton)findViewById(R.id.ImageButton01);
    buttOne.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


        }
    });

    buttTwo = (ImageButton)findViewById(R.id.ImageButton02);
    buttTwo.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


        }
    });

    buttThree = (ImageButton)findViewById(R.id.ImageButton03);
    buttThree.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


        }
    });

    buttFour = (ImageButton)findViewById(R.id.ImageButton04);
    buttFour.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.layout.activity_easy, menu);
    return true;



}
}

This is the layout for the Easy option 这是“简单”选项的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ImageView
    android:id="@+id/easyback"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/easyback"
    android:clickable="false"
    android:duplicateParentState="false"
    android:longClickable="false"
    android:scaleType="centerCrop" />

<ImageButton
    android:id="@+id/ImageButton04"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_below="@+id/ImageButton01"
    android:layout_toRightOf="@+id/ImageButton01"
    android:layout_toEndOf="@+id/ImageButton01"
    android:maxHeight="25dp"
    android:maxWidth="25dp"
    android:scaleType="fitXY"
    android:src="@drawable/dragonemb" />

<ImageButton
    android:id="@+id/ImageButton02"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_above="@+id/ImageButton04"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"

    android:layout_marginRight="22dp"
    android:layout_marginEnd="22dp"
    android:maxHeight="25dp"
    android:maxWidth="25dp"
    android:scaleType="fitXY"
    android:src="@drawable/dragonemb" />

<ImageButton
    android:id="@+id/ImageButton03"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_alignTop="@+id/ImageButton04"
    android:layout_toLeftOf="@+id/ImageButton04"
    android:layout_toStartOf="@+id/ImageButton04"
    android:maxHeight="25dp"
    android:maxWidth="25dp"
    android:scaleType="fitXY"
    android:src="@drawable/dragonemb" />

<ImageButton
    android:id="@+id/ImageButton01"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_alignParentTop="true"
    android:layout_marginTop="152dp"
    android:layout_toLeftOf="@+id/ImageButton02"
    android:maxHeight="25dp"
    android:maxWidth="25dp"
    android:scaleType="fitXY"
    android:src="@drawable/dragonemb" />

</RelativeLayout>

OK It's not the full crash log you posted but at the top of it I saw roidManifest.xml? 好吧,这不是您发布的完整崩溃日志,但是在它的顶部,我看到了roidManifest.xml? . And It's sure that you didn't defined your EasyGame Activity in your androidmanifest.xml so add this line inside application tag, 并且确保您没有在androidmanifest.xml定义EasyGame Activity,因此请将此行添加到application标记中,

<manifest package="com....." . . . >
     <application . . . >

         <activity 
           android:name=".EasyGame" 
           android:label="easygame">
        </activity>
         . . .
     </application> 
</manifest> 

In addition you are trying to cast your ImageButton into Button consider fixing that as well. 另外,您正在尝试将ImageButton转换为Button考虑修复该问题。

Add code below to AndroidManfest 将以下代码添加到AndroidManfest

  <activity
        android:name=".EasyGame"
       />

its simple just add this line to your AndroidManifest 简单,只需将此行添加到您的AndroidManifest

<activity android:name="Activity"/>

The logcat did mentioned that you need to declare your activity within the Android Manifest which you didn't. logcat确实提到您需要在Android Manifest中声明您的活动,而您没有声明。 please read the logcat carefully as it really helps to find what went wrong. 请仔细阅读logcat,因为它确实有助于发现问题所在。

OK, so I had tried all of your guys suggestions which I had were part of the issue, the final issue turned out to that I needed to add my images to the drawable-xhdpi. 好的,所以我尝试了所有与我有关的建议,最后一个问题是我需要将图像添加到drawable-xhdpi。 Thanks for all your help. 感谢你的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM