2016-05-18 56 views
-1

enter image description here嘿家伙我在我的Java 1类的工作结束我的项目。我们正在制作记忆/集中游戏。我的问题是,当我点击下一个活动的简单按钮时,应用程序崩溃。我尝试过使用片段和活动,但似乎无法正确使用它。我也尝试过在我的主要活动中使用我需要的布局,以查看是否可以显示它。即使这样,它只是在应用程序启动时崩溃。任何帮助,将不胜感激。xml布局崩溃加载Android应用程序

启动屏幕活动。

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> 
+0

'应用程序崩溃'意味着每个人都搜索崩溃日志。没有人会检查代码。发布堆栈跟踪。 –

+0

发布你的Logcat –

+0

找到崩溃,请更新你的logcat。 – Ankur1994a

回答

3

好这不是你发布的完整的崩溃日志,但在它的顶部,我看到roidManifest.xml?。而且这是确保你没有定义在你的androidmanifest.xmlEasyGame活动,以便里面添加application标签这一行,

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

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

另外你想投你ImageButtonButton考虑固定这一点。

+0

我之前就有过,并拿出来。我已经添加了它,并从上面的评论中提出建议,但仍然崩溃。 –

+0

确定然后通过复制粘贴它来发布完整的崩溃日志...不要张贴半截图。 –

+0

我试图复制/粘贴LogCat,但它不会让我。我所能做的只是发布它的更新截图。 –

1

添加下面AndroidManfest

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

其简单的只是代码将此行添加到您的AndroidManifest

<activity android:name="Activity"/> 

logcat也提到,您需要将Android清单,你没有内声明的活动。请仔细阅读logcat,因为它确实有助于发现哪里出了问题。

0

好的,所以我尝试了所有你们的建议,我曾经是问题的一部分,最后的问题证明我需要将我的图像添加到drawable-xhdpi。感谢你的帮助。