2013-02-14 52 views
0

我正在與Donn Felker一起編寫Android應用程序開發傻瓜書。具體來說,我正在使用靜音模式應用程序。做Android安靜模式應用程序,甚至不能啓動主要活動?

它應該很簡單,按照指示一步一步,但事實證明,我甚至無法啓動主要活動,而不會崩潰的應用程序。我一定會忘記我的代碼十幾次,但我仍然無法弄清楚什麼是錯誤的,並且在沒有LogCat發佈錯誤的情況下工作就像瘋了一樣。

調試設備是Samsung 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); 

} 

我試圖調試,但我還沒有發現問題。請幫忙。

+0

logcat顯示什麼錯誤?你在哪裏初始化你的按鈕? – Ryan 2013-02-14 18:31:01

回答

0
public class MainActivity extends Activity { 

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

你想爲活動設置佈局之前找到按鈕,將它設置爲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 
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); 
    .... 
相關問題