【问题标题】:Android: Making a record button in the notifications barAndroid:在通知栏中制作记录按钮
【发布时间】: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


    【解决方案1】:
    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?
    

    你是对的——没有找到按钮。这是因为findViewById 将查看您在setContentView 中指定的xml 文件

    您需要在包含按钮的视图实例上使用findViewById

    Button btn = contentView.findViewById(R.id.button1);
    btn.setTextColor(activity.getResources().getColor(R.color.red));
    

    【讨论】:

    • 嗯,这个想法是文本在不录制时为白色,在录制时为红色。
    猜你喜欢
    • 2015-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 2020-01-30
    • 1970-01-01
    相关资源
    最近更新 更多