我正在關注Android Development for Dummies中的示例應用程序,這是一個簡單的應用程序,可將鈴聲模式從靜音切換到正常。出於某種原因,當我打電話的checkIfPhoneIsSilent方法,它強調了紅色與錯誤消息「checkIfPhoneIsSilent是未定義類型主要方法」這是到目前爲止的代碼:checkIfPhoneIsSilent()方法undefined
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
public class Main extends Activity {
private AudioManager mAudioManager;
private boolean mPhoneIsSilent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mAudioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
checkIfPhoneIsSilent();
setButtonClickListener();
}
private void setButtonClickListener() {
Button toggleButton = (Button)findViewById(R.id.toggleButton);
toggleButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (mPhoneIsSilent) {
//change back to normal
mAudioManager
.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
}
});
}
}
沒有'checkIfPhoneIsSilent'方法,它是否在本書前面定義? –
我認爲你缺少checkIfPhoneIsSilent()的代碼; –