Question

I finished the code for the Silent Mode Toggle Application (android apps for dummies). My code is identical with the one in the book, with little exceptions because of the newer android. Everything works fine, but if I start the app, it crashes with the error message: Unfortunately, Silent Mode Toggle has stopped.

I found out, that the problem is something with the setOnClickListener method.

There are no little compile errors, eclipse says everything is cool.

The relevant part of the Code:

 package com.dummies.android.silentmodetoggle;

 import android.support.v7.app.ActionBarActivity;
 import android.support.v7.app.ActionBar;
 import android.support.v4.app.Fragment;
 import android.graphics.drawable.Drawable;
 import android.media.AudioManager;
 import android.os.Bundle;
 import android.view.LayoutInflater;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.Button;
 import android.widget.ImageView;
 import android.os.Build;

 public class MainActivity extends ActionBarActivity {

private AudioManager mAudioManager;
private boolean mPhoneIsSilent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ////////////////////////////////////////////////////////////////////////////
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
    /////////////////////////////////////////////////////////////////////////////

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

}

    private void setButtonClickListener(){

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

            public void onClick (View v){

            if (mPhoneIsSilent) {
            mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                mPhoneIsSilent=false;
            } else {
            mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                mPhoneIsSilent=true;
            }
 //             toggleUi();
        }
    });
}

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

If i comment the body of the setButtonClickListener method with // out, so that it has just the first line left: Button toggleButton = (Button) findViewById(R.id.toggleButton); than the application doesn't crash. So, somehow the setOnClickListener (with the inherited onClick) makes the trouble.

I checked the other system files, but as mentioned, there are no compile errors, I checked your site too, but I haven't found this question. So I have no idea now.

I would appreciate any ideas and help. Sorry for the probably very amateur question. Thank you very much! Z


hi, thank you for your reply

hello, thanks for your reply!

Android 4.4.2 in the manifest:

i hope this is the stack trace you mentioned:

04-07 12:58:32.560: D/AndroidRuntime(2370): Shutting down VM

04-07 12:58:32.560: W/dalvikvm(2370): threadid=1: thread exiting with uncaught exception (group=0xb2a7aba8)

04-07 12:58:32.580: E/AndroidRuntime(2370): FATAL EXCEPTION: main

04-07 12:58:32.580: E/AndroidRuntime(2370): Process: com.dummies.android.silentmodetoggle, PID: 2370

04-07 12:58:32.580: E/AndroidRuntime(2370): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dummies.android.silentmodetoggle/com.dummies.android.silentmodetoggle.MainActivity}: java.lang.NullPointerException

04-07 12:58:32.580: E/AndroidRuntime(2370): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)

04-07 12:58:32.580: E/AndroidRuntime(2370): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)

04-07 12:58:32.580: E/AndroidRuntime(2370): at android.app.ActivityThread.access$800(ActivityThread.java:135)

04-07 12:58:32.580: E/AndroidRuntime(2370): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)

04-07 12:58:32.580: E/AndroidRuntime(2370): at android.os.Handler.dispatchMessage(Handler.java:102)

04-07 12:58:32.580: E/AndroidRuntime(2370): at android.os.Looper.loop(Looper.java:136)

04-07 12:58:32.580: E/AndroidRuntime(2370): at android.app.ActivityThread.main(ActivityThread.java:5017)

04-07 12:58:32.580: E/AndroidRuntime(2370): at java.lang.reflect.Method.invokeNative(Native Method)

04-07 12:58:32.580: E/AndroidRuntime(2370): at java.lang.reflect.Method.invoke(Method.java:515)

04-07 12:58:32.580: E/AndroidRuntime(2370): at

com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)

04-07 12:58:32.580: E/AndroidRuntime(2370): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)

04-07 12:58:32.580: E/AndroidRuntime(2370): at dalvik.system.NativeStart.main(Native Method)

04-07 12:58:32.580: E/AndroidRuntime(2370): Caused by: java.lang.NullPointerException

04-07 12:58:32.580: E/AndroidRuntime(2370): at com.dummies.android.silentmodetoggle.MainActivity.setButtonClickListener(MainActivity.java:43)

04-07 12:58:32.580: E/AndroidRuntime(2370): at com.dummies.android.silentmodetoggle.MainActivity.onCreate(MainActivity.java:36)

04-07 12:58:32.580: E/AndroidRuntime(2370): at android.app.Activity.performCreate(Activity.java:5231)

04-07 12:58:32.580: E/AndroidRuntime(2370): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)

04-07 12:58:32.580: E/AndroidRuntime(2370): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)

04-07 12:58:32.580: E/AndroidRuntime(2370): ... 11 more

Was it helpful?

Solution

I have solved the problem (just in time before my laptop would have flown out of my window).

However the solution made up some other questions.

So first, if somebody has the same problem, i did this:

I have changed the extended class to Activity. You will have to import it.

You will have to delete or comment the 'if' in the onCreate method out.

and change the parameter of the setContentView to: R.layout.fragment_main (i have the ImageView and the Button in the fragment_main)

Well, it works now, but it doesnt mean that I really undertsand why, and what was the problem, as the 'extends ActionBarAcitivity' is a default setting, you would expect that it will work.

But I am new to this, so I have no clue...

I will do my research on this topic further, but if you guys could comment on this, what was the reason, what is exactly changed now, etc, I would really appreciate it!

Thank you

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top