1

I have made a simple android app in which the user can toggle between silent mode and the state before the silent mode in a click of a button, but while running the app on an emulator or an android device the message "the application has stopped unexpectedly" is being displayed. What is wrong with my code.

P.S.: I copied the code from Android for Dummies, with a few minor modifications like changing the name of the photos.

Here is my code: package com.example.silentmodetoggle;

import android.widget.Button;
import android.media.AudioManager;
import android.view.*;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ImageView;
import android.graphics.drawable.Drawable;


public class MainActivity extends Activity{
    private AudioManager mAudioManager;
    private boolean mPhoneIsSilent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        setButtonClickListener();
        checkIfPhoneIsSilent();

        mAudioManager=(AudioManager)getSystemService(AUDIO_SERVICE);

    }
    private void setButtonClickListener(){
        Button toggleButton=(Button)findViewById(R.id.toggleButton);
        toggleButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            if(mPhoneIsSilent){
                //change back to normal mode
                mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                mPhoneIsSilent=false;
            }
            else
            {
                //change to silent mode
                mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                mPhoneIsSilent=true;
            }

            //now toggle the UI again
            toggleUi();




            }
        });
    }

    private void checkIfPhoneIsSilent(){
        int ringerMode=mAudioManager.getRingerMode();
        if(ringerMode==AudioManager.RINGER_MODE_SILENT){
            mPhoneIsSilent=true;}
        else
            mPhoneIsSilent=false;

    }

// Toggles the UI images from silent to normal and vice versa.

private void toggleUi()
{
    ImageView imageView=(ImageView)findViewById(R.id.phone_icon);
    Drawable newPhoneImage;

    if(mPhoneIsSilent){
        newPhoneImage=getResources().getDrawable(R.drawable.silent);
    }
    else{
        newPhoneImage=getResources().getDrawable(R.drawable.ringer);
    }
    imageView.setImageDrawable(newPhoneImage);

}


@Override
protected void onResume() {
super.onResume();
checkIfPhoneIsSilent();
toggleUi();
}









}

Here is the AndroidManifest.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:background="#ffffff"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    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=".MainActivity" >

    <ImageView
        android:id="@+id/phone_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal"
        android:contentDescription="@+id/desc"
        android:src="@drawable/ringer" />

    <Button
        android:id="@+id/toggleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/phone_icon"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:text="Toggle_Silent_Mode" />

</RelativeLayout>

The following are displayed in the LogCat:

12-29 21:21:31.475: E/AndroidRuntime(821): FATAL EXCEPTION: main
    12-29 21:21:31.475: E/AndroidRuntime(821): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.silentmodetoggle/com.example.silentmodetoggle.MainActivity}: java.lang.NullPointerException
    12-29 21:21:31.475: E/AndroidRuntime(821):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
    12-29 21:21:31.475: E/AndroidRuntime(821):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    12-29 21:21:31.475: E/AndroidRuntime(821):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    12-29 21:21:31.475: E/AndroidRuntime(821):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    12-29 21:21:31.475: E/AndroidRuntime(821):  at android.os.Handler.dispatchMessage(Handler.java:99)
    12-29 21:21:31.475: E/AndroidRuntime(821):  at android.os.Looper.loop(Looper.java:123)
    12-29 21:21:31.475: E/AndroidRuntime(821):  at android.app.ActivityThread.main(ActivityThread.java:4627)
    12-29 21:21:31.475: E/AndroidRuntime(821):  at java.lang.reflect.Method.invokeNative(Native Method)
    12-29 21:21:31.475: E/AndroidRuntime(821):  at java.lang.reflect.Method.invoke(Method.java:521)
    12-29 21:21:31.475: E/AndroidRuntime(821):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    12-29 21:21:31.475: E/AndroidRuntime(821):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    12-29 21:21:31.475: E/AndroidRuntime(821):  at dalvik.system.NativeStart.main(Native Method)
    12-29 21:21:31.475: E/AndroidRuntime(821): Caused by: java.lang.NullPointerException
    12-29 21:21:31.475: E/AndroidRuntime(821):  at com.example.silentmodetoggle.MainActivity.checkIfPhoneIsSilent(MainActivity.java:58)
    12-29 21:21:31.475: E/AndroidRuntime(821):  at com.example.silentmodetoggle.MainActivity.onCreate(MainActivity.java:23)
    12-29 21:21:31.475: E/AndroidRuntime(821):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    12-29 21:21:31.475: E/AndroidRuntime(821):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

3 Answers 3

1

You call checkIfPhoneIsSilent before instantiating your AudioManager. This causes that NullPointerException. Change your onCreate method to fix this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mAudioManager=(AudioManager)getSystemService(AUDIO_SERVICE);

    setButtonClickListener();
    checkIfPhoneIsSilent();
}
Sign up to request clarification or add additional context in comments.

Comments

1

You're calling checkIfPhoneIsSilent() which uses mAudioManager before you've set mAudioManager, so it's null, and you're getting a NullPointerException.

Comments

0

Change code like this:

 mAudioManager=(AudioManager)getSystemService(AUDIO_SERVICE);
 setButtonClickListener();
 checkIfPhoneIsSilent();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.