0

I am trying to make a bgm on/off switch. I made a class storage it's volume. I want to pick this value to set the volume in others class and modify this value to control the level of volume in the setting activity. The codes are no error show, but the run-time. The way I did is reference with this one(Android global variable)

05-01 05:37:49.935: E/AndroidRuntime(2704): FATAL EXCEPTION: main
05-01 05:37:49.935: E/AndroidRuntime(2704): java.lang.RuntimeException: Unable to      instantiate activity ComponentInfo{com.group5.littlered/com.group5.littlered.MyMenu}: java.lang.NullPointerException
05-01 05:37:49.935: E/AndroidRuntime(2704):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
05-01 05:37:49.935: E/AndroidRuntime(2704):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
05-01 05:37:49.935: E/AndroidRuntime(2704):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-01 05:37:49.935: E/AndroidRuntime(2704):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
05-01 05:37:49.935: E/AndroidRuntime(2704):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-01 05:37:49.935: E/AndroidRuntime(2704):     at android.os.Looper.loop(Looper.java:137)
05-01 05:37:49.935: E/AndroidRuntime(2704):     at android.app.ActivityThread.main(ActivityThread.java:5103)
05-01 05:37:49.935: E/AndroidRuntime(2704):     at java.lang.reflect.Method.invokeNative(Native Method)
05-01 05:37:49.935: E/AndroidRuntime(2704):     at java.lang.reflect.Method.invoke(Method.java:525)
05-01 05:37:49.935: E/AndroidRuntime(2704):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-01 05:37:49.935: E/AndroidRuntime(2704):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-01 05:37:49.935: E/AndroidRuntime(2704):     at dalvik.system.NativeStart.main(Native Method)
05-01 05:37:49.935: E/AndroidRuntime(2704): Caused by: java.lang.NullPointerException
05-01 05:37:49.935: E/AndroidRuntime(2704):     at com.group5.littlered.MyMenu.<init>(MyMenu.java:24)
05-01 05:37:49.935: E/AndroidRuntime(2704):     at java.lang.Class.newInstanceImpl(Native Method)
05-01 05:37:49.935: E/AndroidRuntime(2704):     at java.lang.Class.newInstance(Class.java:1130)
05-01 05:37:49.935: E/AndroidRuntime(2704):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
05-01 05:37:49.935: E/AndroidRuntime(2704):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
05-01 05:37:49.935: E/AndroidRuntime(2704):     ... 11 more

my log cat

package com.group5.littlered;

import java.io.IOException;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;


    public class MyMenu extends Activity{

        MediaPlayer bgm;
        MediaPlayer buttonsound;
        float bvolume = ((Sound)this.getApplication()).getBGMVolume();
        float svolume = ((Sound)this.getApplication()).getSFXVolume();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState); 

        //Remove title bar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        //Remove notification bar
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,     WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.main);
        Intent intent = getIntent();

        //BGM
        bgm = MediaPlayer.create(MyMenu.this, R.raw.seethedarkness);
        bgm.setLooping(true);
       bgm.setVolume(bvolume,bvolume);
        bgm.start();

        buttonsound = MediaPlayer.create(MyMenu.this, R.raw.button);
        buttonsound.setLooping(false);
       buttonsound.setVolume(svolume,svolume);

        Button start = (Button) findViewById(R.id.button1);
        start.setOnClickListener(new View.OnClickListener() {

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

                try {
                    buttonsound.prepare();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                buttonsound.start();
                Intent intent = new Intent(MyMenu.this, Scene1.class);
                startActivity(intent);

            }
        });


        Button system = (Button) findViewById(R.id.button2);
        system.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                buttonsound.start();

                Intent intent = new Intent(MyMenu.this, system.class);
                startActivity(intent);
            }
        });

        Button credit = (Button) findViewById(R.id.button3);
        credit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                buttonsound.start();

                Dialog credit = new Dialog(MyMenu.this);
                credit.requestWindowFeature(Window.FEATURE_NO_TITLE);
                credit.getWindow().setBackgroundDrawable(new         ColorDrawable(android.graphics.Color.TRANSPARENT));
                credit.setContentView(R.layout.creditdialog);
                credit.show();

            }
        });





    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        bgm.release();
        buttonsound.release();
        finish();
    }

    protected void onResume() {
        // TODO Auto-generated method stub
    onRestart();

    }





    }

This is my sound.class

    package com.group5.littlered;

    import android.app.Application;

    public class Sound extends Application {

// Sound

private float bgmvolume = 0.5f;
private float sfxvolume = 1;

public float getBGMVolume() {
    return bgmvolume;
}

public void setBGMVolume(float bgmVolume) {
    this.bgmvolume = bgmvolume;
}

public float getSFXVolume() {
    return sfxvolume;
}

public void setSFXVolume(float SFXVolume) {
    this.sfxvolume = sfxvolume;
}

    }

This is my mainifest

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

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MyMain"
        android:configChanges="keyboard|keyboardHidden|orientation"
        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=".MyMenu"
        android:configChanges="keyboard|keyboardHidden|orientation"
        android:label="@string/app_name"
        android:screenOrientation="landscape" >
        <intent-filter>
            <action android:name="com.group5.littlered.SPLASH" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.group5.littlered.Scene1"
        android:configChanges="keyboard|keyboardHidden|orientation"
        android:label="@string/title_activity_scene1"
        android:screenOrientation="landscape" >
        <intent-filter>
            <action android:name="com.group5.littlered.SCENE1" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.group5.littlered.Scene2"
        android:configChanges="keyboard|keyboardHidden|orientation"
        android:label="@string/title_activity_scene2"
        android:screenOrientation="landscape" >
        <intent-filter>
            <action android:name="com.group5.littlered.SCENE2" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.group5.littlered.Scene3"
        android:configChanges="keyboard|keyboardHidden|orientation"
        android:label="@string/title_activity_scene3"
        android:screenOrientation="landscape" >
        <intent-filter>
            <action android:name="com.group5.littlered.SCENE3" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.group5.littlered.Scene4"
        android:configChanges="keyboard|keyboardHidden|orientation"
        android:label="@string/title_activity_scene4"
        android:screenOrientation="landscape" >
        <intent-filter>
            <action android:name="com.group5.littlered.SCENE4" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.group5.littlered.Scene5"
        android:configChanges="keyboard|keyboardHidden|orientation"
        android:label="@string/title_activity_scene5"
        android:screenOrientation="landscape" >
        <intent-filter>
            <action android:name="com.group5.littlered.SCENE5" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.group5.littlered.Scene6"
        android:configChanges="keyboard|keyboardHidden|orientation"
        android:label="@string/title_activity_scene6"
        android:screenOrientation="landscape" >
        <intent-filter>
            <action android:name="com.group5.littlered.SCENE6" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.group5.littlered.Scene7"
        android:configChanges="keyboard|keyboardHidden|orientation"
        android:label="@string/title_activity_scene7"
        android:screenOrientation="landscape" >
        <intent-filter>
            <action android:name="com.group5.littlered.SCENE7" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.group5.littlered.Scene8"
        android:configChanges="keyboard|keyboardHidden|orientation"
        android:label="@string/title_activity_scene8"
        android:screenOrientation="landscape" >
        <intent-filter>
            <action android:name="com.group5.littlered.SCENE8" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.group5.littlered.Scene9"
        android:configChanges="keyboard|keyboardHidden|orientation"
        android:label="@string/title_activity_scene9"
        android:screenOrientation="landscape" >
        <intent-filter>
            <action android:name="com.group5.littlered.SCENE9" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.group5.littlered.Scene10"
        android:configChanges="keyboard|keyboardHidden|orientation"
        android:label="@string/title_activity_scene10"
        android:screenOrientation="landscape" >
        <intent-filter>
            <action android:name="com.group5.littlered.SCENE10" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.group5.littlered.Scene11"
        android:configChanges="keyboard|keyboardHidden|orientation"
        android:label="@string/title_activity_scene11"
        android:screenOrientation="landscape" >
        <intent-filter>
            <action android:name="com.group5.littlered.SCENE11" />

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

    <activity
        android:name="com.group5.littlered.GameOver"
        android:configChanges="keyboard|keyboardHidden|orientation"
        android:label="@string/title_gameover"
        android:screenOrientation="landscape" >
        <intent-filter>
            <action android:name="com.group5.littlered.GameOver" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
     <activity
         android:name="com.group5.littlered.blackscreen"
         android:configChanges="keyboard|keyboardHidden|orientation"
         android:label="@string/title_blackscreen"
         android:screenOrientation="landscape" >
         <intent-filter>
             <action android:name="com.group5.littlered.blackscreen" />

             <category android:name="android.intent.category.DEFAULT" />
         </intent-filter>
     </activity>
              <activity
         android:name="com.group5.littlered.system"
         android:configChanges="keyboard|keyboardHidden|orientation"
         android:label="@string/title_system"
         android:screenOrientation="landscape" >
         <intent-filter>
             <action android:name="com.group5.littlered.system" />

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

</application>

<application android:name="Sound" android:icon="@drawable/ic_launcher"  android:label="@string/sound"></application>

This is my string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">littlered</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="title_activity_scene1">Scene1</string>
<string name="title_activity_scene2">Scene2</string>
<string name="title_activity_scene3">Scene3</string>
<string name="title_activity_scene4">Scene4</string>
<string name="title_activity_scene5">Scene5</string>
<string name="title_activity_scene6">Scene6</string>
<string name="title_activity_scene7">Scene7</string>
<string name="title_activity_scene8">Scene8</string>
<string name="title_activity_scene9">Scene9</string>
<string name="title_activity_scene10">Scene10</string>
<string name="title_activity_scene11">Scene11</string>
<string name="title_gameover">GameOver</string>
<string name="title_blackscreen">blackscreen</string>
<string name="title_system">system</string>
<string name="sound">sound</string>

<string-array name="scene1">
    <item>There was a legend that every single full moon night</item>
    <item>There will be a strange kind of creature hunting the people.</item>
    <item>Every month, when the night comes.</item>
    <item>Every people in the village locked on the door and isolate themselves from the outside.</item>
    <item>Grandma:"My dear grand-daughter, tonight is a full moon night, remember to check the doors and windows were locked."</item>
    <item>Little Red Riding Hood: "OK!"</item>
</string-array>
<string-array name="scene2">
    <item>when the girl wake up, she found something stange with her grandma.</item>
    <item>Little Red Riding Hood:"Something wrong with my gram, what happened?!"</item>
    <item>Grandma became wolf totally</item>
    <item>Little Red Riding Hood trying to fight, but the wolf is too strong.</item>
    <item>Little Red Riding Hood feels a sence of danger</item>
    <item>She knows something is wrong with gram, so she runs away</item>
</string-array>
<string-array name="scene3">
    <item>Boy:  Little Red Riding Hood ! Where are you going ? Your look very uncomfortable.It makes me worried.</item>
    <item>Little Red Riding Hood: My grandma turned to a wolf and try to kill me.   </item>
    <item>Little Red Riding Hood: Luckily I survived, but I need to find out all the mysteries. Why did my grandma turn to a wolf.</item>
    <item>Little Red Riding Hood: Also, I decided to figure everything out by exploring the forest, where all the werewolves are from.</item>
    <item>Boy:Let me go with you. The forest is very dangerous. I will protect you.</item>
    <item>Little Red Riding Hood:Okay, let’s go together.</item>
    <item>Boy:Let us go!</item>
</string-array>
<string-array name="scene4">
    <item>Little Red Riding Hood: I see someone there,let’s go.</item>
    <item>Explorer: Hello.</item>
    <item>Little Red Riding Hood: Hi, do you know anything about the werewolves?</item>
    <item>Explorer:  I know they hunt at full moon night according to the legend, they have no mercy when killing people but they can behave like a human without being suspeted. </item>
    <item>Explorer:If you guys come here for the werewolves, we’re together.</item>
</string-array>
<string-array name="scene5">
    <item>Little Red Riding Hood: Hold on, I heard something.</item>
    <item>Explorer: We’re surrouded!</item>
    <item>Boy: Little Red Riding Hood, watch out!</item>
    <item>Little Red Riding Hood: Why don’t you just kill them? You let them go away!</item>
    <item>Hunter: I didn’t think too much , you know, it’s emergency, I wanted to save you.</item>
    <item>Little Red Riding Hood: Do you know anything about the werewolves?</item>
    <item>Hunter: Nope. But we’re already in a dangerous place, let me go with you.</item>
</string-array>
<string-array name="scene6">
    <item>Explorer: It’s getting darker, we have to find some place for rest.</item>
    <item>Hunter: I heard some voice over there, let’s go.</item>
    <item>Little Red Riding Hood: Hello guys, can we stay up nigh here?</item>
    <item>Worker: No problem, it’s dangerous here in the forest, watch out.</item>
</string-array>
<string-array name="scene7">
    <item>Explorer: So this is your place?</item>
    <item>Workers: Yeah, we do lumbering here.</item>
    <item>Explorer: OK.</item>
    <item>Little Red Riding Hood: Oh, I forget one thing. Do you know anything about the werewolves?</item>
    <item>(Some workers’ getting embarrasing)</item>
    <item>Workers: NO...NO… Why..Why do you want to know about them?</item>
    <item>Little Red Riding Hood: Something happened to my family, and I know it’s about the werewolves, I have to figure out what’s happenning!</item>
    <item>Workers:Oh..</item>
</string-array>
<string-array name="scene8">
    <item>Little Red Riding Hood: Oh no! Worker 1 died!</item>
    <item>Worker2, Worker3: Wait ! You! Back here! One of you must be the murderer, we must find out and kill him.</item>
</string-array>
<string-array name="scene9">
    <item>Explorer: No, I don’t have any weapon, it coudn’t be me!</item>
    <item>Hunter : I just have to help out when the wolves attacked , how could I be a killer?</item>
    <item>Worker2: Ugly as I am, but this couldn’t be the reason to kill my brother!</item>
    <item>Worker3: Worker1 is my brother, he was dead is the most sad for me, why would I do ?</item>
</string-array>
<string-array name="scene10">
    <item>Little Red Riding Hood: Finally I know, the real killers are workers. They are werewolves. I must get out of the forest. </item>
    <item>Workers:You know the true identity of us. We will not let you leave easily ! </item>
    <item>The boy:Little Red Riding Hood ! Be Careful! I need to kill all those hateful wolf.</item>
    <item>You dare kill our partners. Brothers! Let’s turn to werewolf. Kill all of these stupid human beings.</item>
    <item>Hunter:You guys go, leave this to me.</item>
    <item>Boy:Little Red Riding Hood ! Sit on me.</item>
    <item>Little Red Riding Hood: How how how would you turn to be….</item>
    <item>Boy:I cannot explain now. Sit carefully on my back.Fast!</item>
    <item>Little Red Riding Hood:Oh no! They are chasing us</item>
    <item>Boy:Don’t be afraid. Never mind ,I can run faster than they do a lot . Catch me!</item>
</string-array>
<string-array name="scene11">
    <item>Little Red Riding Hood: You are really fast. Cannot see them already. There’s something bright in front of us. It is the exit of forest .</item>
    <item>Boy:I am the werewolf. My father is wolf and my mother is human.Therefore, I have half wolf lineage.</item>
    <item>Boy:But I haven’t been cold-blooded. In the village, I pretend to a human with fear of being discriminated agains.</item>
    <item>Boy:I worry about your safety and therefore I come with you .I love you. May I protect you forever.</item>
    <item>Little Red Riding Hood:Yes.You can.</item>
    <item>Little Red Riding Hood: I grew up with you, but I don’t know you are werewolf! It’s OK I can accept that, but you can’t lie to me anymore!</item>
    <item>Explorer:I know what actually happend to your grandma. She’s actually an werewolf and she ate your parents. </item>
    <item>Explorer: But you were still baby, her only remainning mercy keeps you alive. As for these workers, they wanted to kill all of us , they had to do this to keep the secrets.</item>
</string-array>
<string-array name="getkilled">
    <item>Little Red Riding Hood: Finally I know, the real killers are workers. They are werewolves. I must get out of the forest.</item>
    <item>Workers: You know the true identity of us. We will not let you leave easily ! </item>
    <item>Boy: Little Red Riding Hood ! Be Careful! I need to kill all those hateful wolf.</item>
    <item>Workers:You dare kill our partners. Brothers! Let’s turn to werewolf. Kill all of these stupid human beings.</item>
    <item>Little Red Riding Hood:No!!! I should believe the hunter!</item>
</string-array>

</resources>
Community
  • 1
  • 1
  • nullpointer here means that you variable is not initialized before you try to use it. Try to see if you have assigned some object to the global variable before you sue it. – Nazgul May 01 '14 at 10:03
  • `float bvolume = ((Sound)this.getApplication()).getBGMVolume();' 'float svolume = ((Sound)this.getApplication()).getSFXVolume();` Put in `onCreate`. – vjdhama May 01 '14 at 10:04

1 Answers1

1

Move this lines to onCreate():

float bvolume;
float svolume;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState); 

 bvolume = ((Sound)this.getApplication()).getBGMVolume();
 svolume = ((Sound)this.getApplication()).getSFXVolume();
Sergey Neskoromny
  • 1,188
  • 8
  • 15