java - 无法解析方法 findViewById ("int")

标签 java android android-studio findviewbyid

我想更改按钮的文本,但我无法这样做,android studio 向我显示:无法解析方法 findViewById("int")。这是我的代码

package example.hudixts.app;
import android.media.AudioRecord;
import android.media.MediaRecorder.AudioSource;
import android.media.AudioFormat;
import android.widget.Button;



public class recorderThread extends Thread {
    public boolean recording;  //variable to start or stop recording
    public int frequency; //the public variable that contains the frequency value "heard", it is updated continually while the thread is running.
    public recorderThread () {
    }

    @Override
    public void run() {
        AudioRecord recorder;
        int numCrossing,p;
        short audioData[];
        int bufferSize;

        bufferSize=AudioRecord.getMinBufferSize(8000,AudioFormat.CHANNEL_CONFIGURATION_MONO,
                AudioFormat.ENCODING_PCM_16BIT)*3; //get the buffer size to use with this audio record

        recorder = new AudioRecord (AudioSource.MIC,8000,AudioFormat.CHANNEL_CONFIGURATION_MONO,
                AudioFormat.ENCODING_PCM_16BIT,bufferSize); //instantiate the AudioRecorder

        recording=true; //variable to use start or stop recording
        audioData = new short [bufferSize]; //short array that pcm data is put into.


        while (recording) {  //loop while recording is needed
            if (recorder.getState()==android.media.AudioRecord.STATE_INITIALIZED) // check to see if the recorder has initialized yet.
                if (recorder.getRecordingState()==android.media.AudioRecord.RECORDSTATE_STOPPED)
                    recorder.startRecording();  //check to see if the Recorder has stopped or is not recording, and make it record.

                else {

                    recorder.read(audioData,0,bufferSize); //read the PCM audio data into the audioData array

                    //Now we need to decode the PCM data using the Zero Crossings Method

                    numCrossing=0; //initialize your number of zero crossings to 0
                    for (p=0;p<bufferSize/4;p+=4) {
                        if (audioData[p]>0 && audioData[p+1]<=0) numCrossing++;
                        if (audioData[p]<0 && audioData[p+1]>=0) numCrossing++;
                        if (audioData[p+1]>0 && audioData[p+2]<=0) numCrossing++;
                        if (audioData[p+1]<0 && audioData[p+2]>=0) numCrossing++;
                        if (audioData[p+2]>0 && audioData[p+3]<=0) numCrossing++;
                        if (audioData[p+2]<0 && audioData[p+3]>=0) numCrossing++;
                        if (audioData[p+3]>0 && audioData[p+4]<=0) numCrossing++;
                        if (audioData[p+3]<0 && audioData[p+4]>=0) numCrossing++;
                    }//for p

                    for (p=(bufferSize/4)*4;p<bufferSize-1;p++) {
                        if (audioData[p]>0 && audioData[p+1]<=0) numCrossing++;
                        if (audioData[p]<0 && audioData[p+1]>=0) numCrossing++;
                    }



                    frequency=(8000/bufferSize)*(numCrossing/2);  // Set the audio Frequency to half the number of zero crossings, times the number of samples our buffersize is per second.

                    Button p1_button = (Button)findViewById(R.id.submits);
                    p1_button.setText(Integer.toString(frequency));
                }//else recorder started

        } //while recording

        if (recorder.getState()==android.media.AudioRecord.RECORDSTATE_RECORDING) recorder.stop(); //stop the recorder before ending the thread
        recorder.release(); //release the recorders resources
        recorder=null; //set the recorder to be garbage collected.

    }//run

}//recorderThread

我在这一行中收到此错误:

 Button p1_button = (Button)findViewById(R.id.submits);

请帮助我,因为我是 Android 初学者,对此了解不多。提前致谢

最佳答案

因为通过 id 查找 View 是 activity class 中的方法 在 Activity 中写入您的线程对象 请记住,如果您想对 ui 进行一些更改,请确保将其放在 runOnUiThread 方法中检查 here

runOnUiThread(new Runnable(){
@Override
public void run(){
    // change UI elements here
}});

这是您编写代码的方式

在 on create 方法中

Thread a=new Thread() {
public boolean recording;  //variable to start or stop recording
public int frequency; //the public variable that contains the frequency value "heard", it is updated continually while the thread is running.

@Override
public void run() {
    AudioRecord recorder;
    int numCrossing,p;
    short audioData[];
    int bufferSize;

    bufferSize=AudioRecord.getMinBufferSize(8000,AudioFormat.CHANNEL_CONFIGURATION_MONO,
            AudioFormat.ENCODING_PCM_16BIT)*3; //get the buffer size to use with this audio record

    recorder = new AudioRecord (AudioSource.MIC,8000,AudioFormat.CHANNEL_CONFIGURATION_MONO,
            AudioFormat.ENCODING_PCM_16BIT,bufferSize); //instantiate the AudioRecorder

    recording=true; //variable to use start or stop recording
    audioData = new short [bufferSize]; //short array that pcm data is put into.


    while (recording) {  //loop while recording is needed
        if (recorder.getState()==android.media.AudioRecord.STATE_INITIALIZED) // check to see if the recorder has initialized yet.
            if (recorder.getRecordingState()==android.media.AudioRecord.RECORDSTATE_STOPPED)
                recorder.startRecording();  //check to see if the Recorder has stopped or is not recording, and make it record.

            else {

                recorder.read(audioData,0,bufferSize); //read the PCM audio data into the audioData array

                //Now we need to decode the PCM data using the Zero Crossings Method

                numCrossing=0; //initialize your number of zero crossings to 0
                for (p=0;p<bufferSize/4;p+=4) {
                    if (audioData[p]>0 && audioData[p+1]<=0) numCrossing++;
                    if (audioData[p]<0 && audioData[p+1]>=0) numCrossing++;
                    if (audioData[p+1]>0 && audioData[p+2]<=0) numCrossing++;
                    if (audioData[p+1]<0 && audioData[p+2]>=0) numCrossing++;
                    if (audioData[p+2]>0 && audioData[p+3]<=0) numCrossing++;
                    if (audioData[p+2]<0 && audioData[p+3]>=0) numCrossing++;
                    if (audioData[p+3]>0 && audioData[p+4]<=0) numCrossing++;
                    if (audioData[p+3]<0 && audioData[p+4]>=0) numCrossing++;
                }//for p

                for (p=(bufferSize/4)*4;p<bufferSize-1;p++) {
                    if (audioData[p]>0 && audioData[p+1]<=0) numCrossing++;
                    if (audioData[p]<0 && audioData[p+1]>=0) numCrossing++;
                }



                frequency=(8000/bufferSize)*(numCrossing/2);  // Set the audio Frequency to half the number of zero crossings, times the number of samples our buffersize is per second.

                Button p1_button = (Button)findViewById(R.id.submits);
                p1_button.setText(Integer.toString(frequency));
            }//else recorder started

    } //while recording

    if (recorder.getState()==android.media.AudioRecord.RECORDSTATE_RECORDING) recorder.stop(); //stop the recorder before ending the thread
    recorder.release(); //release the recorders resources
    recorder=null; //set the recorder to be garbage collected.

}//run}         a.start();

关于java - 无法解析方法 findViewById ("int"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29277998/

相关文章:

java - 为什么 SocialAuth 说 "facebook is not a provider or valid OpenId URL"?

android - ListView 不显示项目

Android Zbar 无法读取二维码

android - SQLiteCantOpenDatabaseException : unknown error (code 14): Could not open database (Only when unit testing the app)

java - 字符串构建器如何管理对单个对象的多线程访问和可变性?

java - Android Studio Swictch-case 实例无法正常工作

java - 如何使用hibernate连接两个表

android - 将 http URL 插入 Android MediaStore 播放列表

android - java.nio.file.InvalidPathException : Trailing char < > at index 10: res/raw/?

java - 如何创建一个布局,其中任何小部件都不可单击?