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

  • SEARCH
  • Home
  • 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 8355091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:45:26+00:00 2026-06-09T09:45:26+00:00

I am learning android so I wrote this code just to toggle phone ringer

  • 0

I am learning android so I wrote this code just to toggle phone ringer mode. The code compiles with no problem, I made entry in Android Manifest, set content view to the required Layout but I run this app, I get Force close error. Can somebody tell me why Force Close errors occur so that in future I should be to figure out the problem myself.Here is the code:

package com.umer.practice2;

import android.R.bool;
import android.app.Activity;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;

public class RingerMode extends Activity implements View.OnClickListener {

ToggleButton tb;
ImageView Riv;
TextView tv;
AudioManager mRing;
boolean silent;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ringermode);

    tb.setOnClickListener(this);
    InitializeShit();

    mRing=(AudioManager) getSystemService(AUDIO_SERVICE);
}

private void InitializeShit() {
    // TODO Auto-generated method stub
    tb= (ToggleButton) findViewById(R.id.ringTB);
    tv= (TextView) findViewById(R.id.ringTV);
    Riv= (ImageView) findViewById(R.id.ringIV);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    checkMode();
    if(silent)
    {
        Riv.setImageResource(R.drawable.mysplash);
    }else
        {
            Riv.setImageResource(R.drawable.myscreen);
        }
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    checkMode();
}

private void checkMode() {
    // TODO Auto-generated method stub
    int temp = mRing.getRingerMode();
    if(temp==AudioManager.RINGER_MODE_SILENT)
    {
        tv.setText("Silent");
        Riv.setImageResource(R.drawable.mysplash);
        silent= true;
    }else
        if(temp==AudioManager.RINGER_MODE_NORMAL)
        {
            tv.setText("Normal");
            Riv.setImageResource(R.drawable.myscreen);
            silent= false;
        }
    }

Many Thanks

  • 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-09T09:45:29+00:00Added an answer on June 9, 2026 at 9:45 am

    You need to take a look at the logcat to see what happens. See Logcat | Android Developers.

    Find the stacktrace of the crash, which points to your problem. If you can’t figure it out yourself, please copy/paste the logcat in your question.

    In this very case, you are referencing tb before initializing it:

    tb.setOnClickListener(this);
    

    At this point, tb is still null, so a NullPointerException occurs. To resolve this, change your code like this:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ringermode);
    
    
        InitializeShit();
        tb.setOnClickListener(this);
    
        mRing=(AudioManager) getSystemService(AUDIO_SERVICE);
    }
    

    Also, I recommend using Java’s conventions regarding methods and variable naming:

    • Classes start with a capital: e.g. MyClass
    • Variables start with a lowercase: e.g. myVariable
    • Methods start with a lowercase: e.g. myMethod()

    This will save you from confusion later on.

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

Sidebar

Related Questions

I am learning android and so I wrote the following code for learning ScrollView
I'm just learning Android. I have working piece of code that uses ContentValues: My
I am currently learning how to develop applications for Android mobile devices. I wrote
I am learning Android and java so this is probably an easy question. I
I have just started learning to write apps for android using Eclipse. Where can
I'm familiar with C++ but never used Java. I've just started learning Android development.
I've just started learning Android development and as a little project, I'm building a
I have been learning android development and I am still new to this. I
I'm learning Android by writing a game and have a problem with the graphics
Am learning android and am struggling to get my head around this particular layout

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.