做一个Android Silent Mode App,甚至无法启动Main Activity?

时间:2013-02-14 18:28:52

标签: java android eclipse galaxy

我正在使用Donn Felker的Android应用程序开发傻瓜书。具体来说,我正在使用静音模式应用程序。

按照说明逐步操作应该很简单,但事实证明,我甚至无法在不崩溃应用程序的情况下启动主要活动。我必须已经完成了我的代码十几次,我仍然无法弄清楚出了什么问题,并且在没有像疯狂的LogCat触发错误的情况下使它工作。

调试设备是三星Galaxy SII w / Android 4.0.3。

这是MainAcitivity类

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);

}

我试过调试,但我没有发现问题。请帮忙。

2 个答案:

答案 0 :(得分:0)

public class MainActivity extends Activity {

Button m_toggleButton = (Button)findViewById(R.id.toggleButton); //<<<< here
//....your code 

您在设置Activity的布局之前尝试找到Button,因此在将setContentView设置为:

之后移动它
public class MainActivity extends Activity {

Button m_toggleButton ;
AudioManager m_audio;
boolean m_isPhoneSilent;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     m_toggleButton = (Button)findViewById(R.id.toggleButton);
    m_audio = (AudioManager)getSystemService(AUDIO_SERVICE);
   ///... your code here

答案 1 :(得分:0)

你不能这称呼

Button m_toggleButton = (Button)findViewById(R.id.toggleButton);
AudioManager m_audio = (AudioManager)getSystemService(AUDIO_SERVICE);

setContentView更改为此之前

Button m_;
AudioManager m_audio;
boolean m_isPhoneSilent;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    m_toggleButton = (Button)findViewById(R.id.toggleButton);
    m_audio = (AudioManager)getSystemService(AUDIO_SERVICE);
    ....