简体   繁体   English

无法解析方法findViewById(“ int”)

[英]Cannot resolve method findViewById(“int”)

I want to change text of my button but i am not able to do so, the android studio is showing me this: Cannot resolve method findViewById("int") . 我想更改按钮的文本,但无法执行操作,Android Studio向我显示了此信息: Cannot resolve method findViewById("int") Here is my code 这是我的代码

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

I am getting this error in this line: 我在此行中收到此错误:

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

Please help me as i am beginner to android and don't know much about. 请帮助我,因为我是android的初学者,所以对它的了解不多。 Thanks in advance 提前致谢

becuse find view by id is method inside the activity class write ur thread object inside the activity and remmber if you wanna make some changes to the ui make sure to make it inside the runOnUiThread method check here 因为通过id查找视图是活动类中的方法,所以请在活动类中写入您的线程对象,如果想要对ui进行一些更改,请确保将其放在runOnUiThread方法中,请检查这里

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

here's how you possiable could write your code 这是您可能的编写代码的方式

inside the on create method 在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();

findViewById应该在Activity

You should find the name of your rootView, and use your rootView to call findViewById method. 您应该找到rootView的名称,并使用rootView调用findViewById方法。 Below is an example: 下面是一个示例:

View rootView = inflater.inflate(R.layout.fragment_main, container, false);
ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM