Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9266353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:17:54+00:00 2026-06-18T14:17:54+00:00

Java Code public class Main extends Activity { private AudioManager mAudioManager; private boolean mPhoneIsSilent;

  • 0

Java Code

public class Main extends Activity {

private AudioManager mAudioManager;
private boolean mPhoneIsSilent;
    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    checkIfPhoneIsSilent();
    setButtonOnClickListener();
    mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
}
public void setButtonOnClickListener(){
    Button b = (Button) findViewById(R.id.toggleButton);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            if (mPhoneIsSilent){
                //change back to normal mode
                mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                mPhoneIsSilent = false;
            }
            else {
                mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                mPhoneIsSilent = true;
            }
            //To toggle the UI
            toggleUI();
        }
    });
}
public void checkIfPhoneIsSilent(){
    int ringerMode = mAudioManager.getRingerMode();
    if (ringerMode == AudioManager.RINGER_MODE_SILENT){
        mPhoneIsSilent = true;
    }
    else {
        mPhoneIsSilent = false;
    }
}
public void toggleUI(){
    ImageView iv = (ImageView) findViewById(R.id.phone_icon);
    Drawable newImage;
    if (mPhoneIsSilent){
        newImage = getResources().getDrawable(R.drawable.phone_silent);
    }
    else {
        newImage = getResources().getDrawable(R.drawable.phone_on);
    }
    iv.setImageDrawable(newImage);
}
@Override
protected void onResume(){
    super.onResume();
    checkIfPhoneIsSilent();
    toggleUI();
}

}

Log Trace

02-10 20:19:58.198: D/dalvikvm(5870): GC_EXTERNAL_ALLOC freed 45K, 53% free 2544K/5379K, external 1869K/2137K, paused 86ms
02-10 20:19:58.258: D/AndroidRuntime(5870): Shutting down VM
02-10 20:19:58.258: W/dalvikvm(5870): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-10 20:19:58.278: E/AndroidRuntime(5870): FATAL EXCEPTION: main
02-10 20:19:58.278: E/AndroidRuntime(5870): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.parasmani.silentmodetoggle/com.parasmani.silentmodetoggle.Main}: java.lang.NullPointerException
02-10 20:19:58.278: E/AndroidRuntime(5870):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-10 20:19:58.278: E/AndroidRuntime(5870):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-10 20:19:58.278: E/AndroidRuntime(5870):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-10 20:19:58.278: E/AndroidRuntime(5870):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-10 20:19:58.278: E/AndroidRuntime(5870):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-10 20:19:58.278: E/AndroidRuntime(5870):     at android.os.Looper.loop(Looper.java:123)
02-10 20:19:58.278: E/AndroidRuntime(5870):     at android.app.ActivityThread.main(ActivityThread.java:3683)
02-10 20:19:58.278: E/AndroidRuntime(5870):     at java.lang.reflect.Method.invokeNative(Native Method)
02-10 20:19:58.278: E/AndroidRuntime(5870):     at java.lang.reflect.Method.invoke(Method.java:507)
02-10 20:19:58.278: E/AndroidRuntime(5870):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-10 20:19:58.278: E/AndroidRuntime(5870):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-10 20:19:58.278: E/AndroidRuntime(5870):     at dalvik.system.NativeStart.main(Native Method)
02-10 20:19:58.278: E/AndroidRuntime(5870): Caused by: java.lang.NullPointerException
02-10 20:19:58.278: E/AndroidRuntime(5870):     at com.parasmani.silentmodetoggle.Main.checkIfPhoneIsSilent(Main.java:53)
02-10 20:19:58.278: E/AndroidRuntime(5870):     at com.parasmani.silentmodetoggle.Main.onCreate(Main.java:22)
02-10 20:19:58.278: E/AndroidRuntime(5870):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-10 20:19:58.278: E/AndroidRuntime(5870):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-10 20:19:58.278: E/AndroidRuntime(5870):     ... 11 more
02-10 20:24:58.423: I/Process(5870): Sending signal. PID: 5870 SIG: 9
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:17:56+00:00Added an answer on June 18, 2026 at 2:17 pm

    Your order of assignment vs method calls in onCreate() is off. You attempt to use mAudioManager before giving it something to hold so it is null, and a NPE is thrown. It needs to be like this:

     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        checkIfPhoneIsSilent();
        setButtonOnClickListener();
       }
    

    That way mAudioManager will not be null when checkIfPhoneIsSilent() is called since we have given it something to hold ahead of time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code in Main.java : public class Main extends Activity implements
findViewById returns null for EditText Java Code: public class MainActivity extends Activity { private
Try this code - import java.io.StringReader; public class StringReaderTest { public static void main(String[]
i have executed the following code public class Activity_Threads_Handler extends Activity { private int
Here is code of my Android app. MainActivity.java public class MainActivity extends Activity {
this is my activity code public class HomeActivity extends Activity { private String html
How to Solve java.lang.StackOverflowError from following Code? My Code is:- public class main extends
Considering that simple java code which would not work: public class Bar extends AbstractBar{
That is my Java code for a Generic bubbleSorter: public class BubbleSorter<E extends Comparable<E>>
Here I found this code: import java.awt.*; import javax.swing.*; public class FunWithPanels extends JFrame

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.