【发布时间】:2014-05-31 22:05:51
【问题描述】:
嘿,伙计们,我正在编写一个应用程序,它有一个自定义 xml 通知,其中有一个用于录音的按钮。我非常接近使用广播监听器完成。我想做的最后一个功能是在录制时将按钮文本颜色简单地更改为红色,这被证明是困难的。
我从 createNotification() 开始
Intent buttonIntent = new Intent(this, playButtonListener.class);
PendingIntent pbuttonIntent = PendingIntent.getBroadcast(this, 1, buttonIntent,0);
contentView.setOnClickPendingIntent(R.id.button1, pbuttonIntent);
而 playButtonListener() 类看起来像
public static class playButtonListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(recording){
setNotRecording(VoiceRecorder.activity); //VoiceRecorder is the
//activity and activity = this in onCreate()
recorder.stop(); // stop recording
recorder.reset(); // reset the MediaRecorder
recording = false; // we are no longer recording
}else{
setRecording(VoiceRecorder.activity);
if (recorder == null)
recorder = new MediaRecorder(); // create MediaRecorder
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
recorder.setAudioEncodingBitRate(96000);
recorder.setAudioSamplingRate(44100);
最后我在 setRecording() 中设置了按钮
public static void setRecording(Activity activity){
//Had to be static to use with playButtonListener
//Had to use activity because static doesnt work with findViewById
Button b = (Button) activity.findViewById(R.id.button1);
b.setTextColor(activity.getResources().getColor(
R.color.red));//Crashes here where button 1 is the id on the
//button on the notification
//The error is a null pointer so I guess the id isnt found?
Button b2 = (Button) activity.findViewById(R.id.recordButton);//These buttons which are in the activity
//opposed to outside the activity and work fine when I take out the first button
b2.setText(R.string.stp);
b2.setTextColor(activity.getResources().getColor(
R.color.red));
Button b3 = (Button) activity.findViewById(R.id.viewSaved);
b3.setEnabled(false);
}
关于为什么我不能更改按钮的文本颜色的任何想法?我已经尝试过诸如制作全局静态公共按钮 = button1 之类的方法,但该按钮仍然在同一位置崩溃
【问题讨论】:
标签: android static nullpointerexception android-activity notifications