第二项活动没有开放

时间:2015-03-02 12:25:44

标签: android eclipse button android-activity

当用户点击任何听起来音乐的按钮时,我有一个带有几个按钮的应用程序。问题是当活动打开时程序关闭。

只有使用此活动时,eclipse错误才会显示未发生的事情。

这是代码:

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

public class GranjaActivity extends Activity {


    Button atras, vaca, cerdo, caballo, oveja, gallina, perro ;

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

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_granja);

        atras = (Button) findViewById(R.id.btnatras);
        vaca = (Button) findViewById(R.id.btnvaca);
        cerdo = (Button) findViewById(R.id.btncerdo);
        caballo = (Button) findViewById(R.id.btncaballo);
        oveja = (Button) findViewById(R.id.btnoveja);
        gallina = (Button) findViewById(R.id.btngallina);
        perro = (Button) findViewById(R.id.btnperro);

        //boton atras
        atras.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();

            }
        });

        //boton vaca
        vaca.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                MediaPlayer vaca = MediaPlayer.create(getApplicationContext(), R.raw.vaca);

                vaca.start();

            }
        });

        //boton cerdo
        cerdo.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                MediaPlayer cerdo = MediaPlayer.create(getApplicationContext(), R.raw.cerdo);

                cerdo.start();

            }
        });

        //boton caballo
        caballo.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                MediaPlayer caballo = MediaPlayer.create(getApplicationContext(), R.raw.caballo);

                caballo.start();

            }
        });

        //boton oveja
        oveja.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                MediaPlayer oveja = MediaPlayer.create(getApplicationContext(), R.raw.oveja);

                oveja.start();

            }
        });

        //boton gallina
        gallina.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                MediaPlayer gallina = MediaPlayer.create(getApplicationContext(), R.raw.gallina);

                gallina.start();

            }
        });

        //boton perro
        perro.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                MediaPlayer perro = MediaPlayer.create(getApplicationContext(), R.raw.perro);

                perro.start();

            }
        });
    }


}

这是de 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="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/fondogranja"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="es.quovo.learninganimals.GranjaActivity" >

    <Button
        android:id="@+id/btnatras"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="14dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/back" />

    <Button
        android:id="@+id/btnvaca"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_alignLeft="@+id/button1"
        android:layout_alignParentBottom="true"
        android:background="@drawable/botonvaca" />

    <Button
        android:id="@+id/btncerdo"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_alignTop="@+id/btnvaca"
        android:layout_toRightOf="@+id/btnvaca"
        android:background="@drawable/botoncerdo" />

    <Button
        android:id="@+id/btnperro"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_alignTop="@+id/btncerdo"
        android:layout_toRightOf="@+id/btncerdo"
        android:background="@drawable/botonperro"/>

    <Button
        android:id="@+id/btnoveja"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_alignTop="@+id/btnperro"
        android:layout_toRightOf="@+id/btnperro"
        android:background="@drawable/botonoveja"/>

        <Button
        android:id="@+id/btncaballo"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_alignTop="@+id/btnoveja"
        android:layout_toRightOf="@+id/btnoveja"
        android:background="@drawable/botonhorse" />

    <Button
        android:id="@+id/btngallina"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_alignTop="@+id/btncaballo"
        android:layout_toRightOf="@+id/btncaballo"
        android:background="@drawable/botongallina" />

</RelativeLayout>

Android清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="es.quovo.learninganimals"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="landscape" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".GranjaActivity"
            android:label="@string/title_activity_granja"
            android:screenOrientation="landscape" >
        </activity>

        <activity
            android:name=".SelvaActivity"
            android:label="@string/title_activity_selva"
            android:screenOrientation="landscape" >
        </activity>

        <activity
            android:name=".OceanoActivity"
            android:label="@string/title_activity_oceano"
            android:screenOrientation="landscape" >
        </activity>
        <activity
            android:name=".CieloActivity"
            android:label="@string/title_activity_cielo"
            android:screenOrientation="landscape" >
        </activity>
        <activity
            android:name=".SabanaActivity"
            android:label="@string/title_activity_sabana"
            android:screenOrientation="landscape" >
        </activity>
        <activity
            android:name=".CasaActivity"
            android:label="@string/title_activity_casa"
            android:screenOrientation="landscape" >
        </activity>

    </application>

</manifest>

2 个答案:

答案 0 :(得分:0)

requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                            WindowManager.LayoutParams.FLAG_FULLSCREEN);

在super.oncreate

之前设置这些行

答案 1 :(得分:0)

在活动顶部将媒体播放器初始化如下,然后在整个项目中使用该媒体播放器

MediaPlayer mpa=new MediaPlayer();

mpa = MediaPlayer.create(getApplicationContext(), R.raw.perro);