简体   繁体   中英

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. 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? . And It's sure that you didn't defined your EasyGame Activity in your androidmanifest.xml so add this line inside application tag,

<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.

Add code below to AndroidManfest

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

its simple just add this line to your AndroidManifest

<activity android:name="Activity"/>

The logcat did mentioned that you need to declare your activity within the Android Manifest which you didn't. please read the logcat carefully as it really helps to find what went wrong.

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. Thanks for all your help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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