Note that there are some explanatory texts on larger screens.

plurals
  1. PODoing an Android Silent Mode App, can't even start Main Activity?
    text
    copied!<p>I'm working with the Android Application Development for Dummies book by Donn Felker. Specifically, I'm working with the Silent Mode App. </p> <p>It should be simple to follow the instructions step by step, but as it turns out I can't even start the main activity without crashing the app. I must've gone over my code a dozen times and I can't still figure out what's wrong and make it work without LogCat firing errors like crazy. </p> <p>Debug device is a Samsung Galaxy SII w/ Android 4.0.3.</p> <p>Here's the MainAcitivity class</p> <pre><code>public class MainActivity extends Activity { Button m_toggleButton = (Button)findViewById(R.id.toggleButton); AudioManager m_audio = (AudioManager)getSystemService(AUDIO_SERVICE); boolean m_isPhoneSilent; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); checkMode(); toggleImage(); generateClick(); } @Override protected void onResume() { super.onResume(); checkMode(); toggleImage(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } private void checkMode() { m_audio = (AudioManager)getSystemService(Context.AUDIO_SERVICE); switch(m_audio.getRingerMode()) { case AudioManager.RINGER_MODE_NORMAL: m_isPhoneSilent = false; break; case AudioManager.RINGER_MODE_SILENT: m_isPhoneSilent = true; break; } } private void generateClick() { m_toggleButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (m_isPhoneSilent == true) { m_audio.setRingerMode (AudioManager.RINGER_MODE_NORMAL); m_isPhoneSilent = false; } else { m_audio.setRingerMode (AudioManager.RINGER_MODE_SILENT); m_isPhoneSilent = true; } toggleImage(); } }); } private void toggleImage() { ImageView imageView = (ImageView) findViewById(R.id.phone_icon); Drawable newAsset; if (m_isPhoneSilent == true) { newAsset = getResources().getDrawable(R.drawable.phone_silent); } else { newAsset = getResources().getDrawable(R.drawable.phone_on); } imageView.setImageDrawable(newAsset); } </code></pre> <p>I've tried to debug, but I haven't found the problem. Please help.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload