簡體   English   中英

xml布局在為Android應用加載時崩潰

[英]xml layout crashes on loading for android app

大家好,我正在Java 1課程的最后階段。 我們正在做一個記憶/集中游戲。 我的問題是,當我單擊“輕松”按鈕進行下一個活動時,應用程序崩潰了。 我已經嘗試過使用片段和活動,但似乎無法正確解決。 我還嘗試在主要活動中使用所需的布局,以查看是否可以顯示它。 即使這樣,它也只會在應用程序啟動時崩潰。 任何幫助,將不勝感激。

啟動屏幕活動。

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;


}

}

第二次活動(輕松選項)

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;



}
}

這是“簡單”選項的布局

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

好吧,這不是您發布的完整崩潰日志,但是在它的頂部,我看到了roidManifest.xml? 並且確保您沒有在androidmanifest.xml定義EasyGame Activity,因此請將此行添加到application標記中,

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

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

另外,您正在嘗試將ImageButton轉換為Button考慮修復該問題。

將以下代碼添加到AndroidManfest

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

簡單,只需將此行添加到您的AndroidManifest

<activity android:name="Activity"/>

logcat確實提到您需要在Android Manifest中聲明您的活動,而您沒有聲明。 請仔細閱讀logcat,因為它確實有助於發現問題所在。

好的,所以我嘗試了所有與我有關的建議,最后一個問題是我需要將圖像添加到drawable-xhdpi。 感謝你的幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM