java - android开发-注册和取消注册onClick,无法解析方法

标签 java android

考虑这段代码:

package com.example.savag.bcapplication;

import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.media.Image;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.HandlerThread;
import android.provider.Settings;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements SensorEventListener{
    //define global variable types
    int counter=0;
    String strcounter;
    int buttoncounter=0;
    String strbcounter;
    TextView dynamicheader;
    //Define the sensor Manager
    SensorManager sm;

    //Define the Motion Sensor objects
    Sensor accelerometer;
    Sensor gravity;
    Sensor gyroscope;
    Sensor uncalgyro;
    Sensor lineaccel;
    Sensor rotatevector;
    Sensor sigmotion;
    Sensor stepcounter;
    Sensor stepdetector;
    //Define the changing Motion Sensor text values
    TextView gyrosense;
    TextView uncalgyrosense;
    TextView acceleration;
    TextView gravitysense;
    TextView lineaccelsense;
    TextView rotatesense;
    TextView sigmotionsense; //In order to make use of this, use onTrigger event
    TextView stepstaken;
    TextView stepdetected; //In order to make use of this, use onTrigger event

    //Define the position sensor objects
    Sensor gamerotatevector;

    //Define the changing Position sensor objects
    TextView gamerotatesense;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // variables final static
        final ImageButton button1 = (ImageButton)findViewById(R.id.motbut_unpressed);
        final ImageView topbar2 = (ImageView)findViewById(R.id.topbarmain);
        final ImageButton button2 = (ImageButton)findViewById(R.id.posbut_unpressed);
        final ImageButton button3 = (ImageButton)findViewById(R.id.envbut_unpressed);
        final LinearLayout babcockandfield = (LinearLayout)findViewById(R.id.babcocklayout);
        final ImageButton arrowup = (ImageButton)findViewById(R.id.uparrow);
        final ImageButton arrowdown = (ImageButton)findViewById(R.id.downarrow);
        final ImageView navbar = (ImageView)findViewById(R.id.infospace);
        //Global counter variables
        counter = 0;
        strcounter = Integer.toString(counter);
        buttoncounter = 0;
        strbcounter = Integer.toString(buttoncounter);
        //Set Nav bar invisible for now
        arrowup.setVisibility(View.GONE);
        arrowdown.setVisibility(View.GONE);
        navbar.setVisibility(View.GONE);
        sm = (SensorManager)getSystemService(SENSOR_SERVICE);
        HandlerThread mHandlerThread = new HandlerThread("sensorThread");

        mHandlerThread.start();

        final Handler handler = new Handler(mHandlerThread.getLooper());


        button1.setOnClickListener( //This is for the motion button
                new ImageButton.OnClickListener() {
                    public void onClick(View v) {
                        buttoncounter=1;
                        strbcounter = Integer.toString(buttoncounter); // maybe need and on counter listener
                        //change colour of pressed button
                        button1.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.motionbutton_pressed, null));
                        topbar2.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.topbarmotion, null));
                        bcandf.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.logoandbarmotion, null));
                        //make sure the other two buttons are normal again
                        button2.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.positionbutton_normal, null));
                        button3.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.envirobutton_normal, null));
                        //make the arrows and infospace appear
                        arrowup.setVisibility(View.VISIBLE);
                        arrowdown.setVisibility(View.VISIBLE);
                        navbar.setVisibility(View.VISIBLE);
                        //Dynamic text appearance
                        //set the text inside infospace
                        //dynamicheader.setVisibility(View.VISIBLE);

                    }
                }
        );
        button2.setOnClickListener( //This is for the position button
                new ImageButton.OnClickListener(){
                    public void onClick(View v){
                        buttoncounter=2;
                        strbcounter = Integer.toString(buttoncounter); // maybe need and on counter listener
                        //Change colour of pressed button
                        button2.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.positionbutton_pressed, null));
                        topbar2.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.topbarposition, null));
                        bcandf.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.logoandbaralternate, null));
                        //make sure the other two buttons are normal again
                        button1.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.motionbutton_normal, null));
                        button3.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.envirobutton_normal, null));
                        //make the arrows and the infospace appear
                        arrowup.setVisibility(View.VISIBLE);
                        arrowdown.setVisibility(View.VISIBLE);
                        navbar.setVisibility(View.VISIBLE);
                        //Dynamic text appearance
                        //set the text inside infospace
                        //dynamicheader.setVisibility(View.VISIBLE);
                    }
                }
        );
        button3.setOnClickListener( //This is for the environment button
                new ImageButton.OnClickListener(){
                    public void onClick(View v){
                        buttoncounter=3;
                        strbcounter = Integer.toString(buttoncounter); // maybe need and on counter listener
                        //change colour of pressed button
                        button3.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.envirobutton_pressed, null));
                        topbar2.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.topbarenviro, null));
                        bcandf.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.logoandbaralternate, null));
                        //make sure the other two buttons are normal again
                        button1.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.motionbutton_normal, null));
                        button2.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.positionbutton_normal, null));
                        //make the arrows and the infospace appear
                        arrowup.setVisibility(View.VISIBLE);
                        arrowdown.setVisibility(View.VISIBLE);
                        navbar.setVisibility(View.VISIBLE);
                        //Dynamic text appearance
                        //set the text inside infospace
                       // dynamicheader.setVisibility(View.VISIBLE);
                    }
                }
        );
        arrowup.setOnClickListener( //This is for the up arrow
                new ImageButton.OnClickListener(){
                    public void onClick(View v){
                        if (strcounter.equals("0")||(strcounter.equals("10")))
                        {
                            //do nothing if strcounter is 0
                        }
                        else
                        {
                            counter--; //how many times
                            strcounter = Integer.toString(counter);
                            //et1.setText(strcounter);
                            //now lets set text accordingly
                            //First we need to set in sequential order and also check button we are on
                            if (strcounter.equals("1")) {
                                ////////
                                if (strbcounter.equals("1")) { // Check which button 1 is motion {
                                    //Motion button going on
                                    gravitysense.setVisibility(View.GONE);
                                    acceleration.setVisibility(View.VISIBLE);
                                    //set the text inside infospace
                                    dynamicheader.setText("Acceleration:");
                                    //Now we need to register the sensors and whatnot
                                    accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
                                    sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL, handler);
                                    //acceleration.setText("X: " + event.values[0] + "\nY: " + event.values[1] + "\nZ: " + event.values[2]);
                                }
                            }
                            if (strcounter.equals("2")) {
                                ////////
                                if (strbcounter.equals("1")) { // Check which button 1 is motion {
                                    //Get rid of the last values and set the one we are using
                                    acceleration.setVisibility(View.GONE);
                                    gravitysense.setVisibility(View.VISIBLE);
                                    //set the text inside infospace
                                    dynamicheader.setText("Gravity Sensor:");
                                    //gravitysense.setText("X: " + event.values[0] + "\nY: " + event.values[1] + "\nZ: " + event.values[2]);
                                }
                            }
                            if (strcounter.equals("3")) {
                                ////////
                                if (strbcounter.equals("1")) { // Check which button 1 is motion
                                    //Get rid of the last values and set the one we are using
                                    gravitysense.setVisibility(View.GONE);
                                    gyrosense.setVisibility(View.VISIBLE);
                                    //set the text inside infospace
                                    dynamicheader.setText("Gyroscope:");
                                    //gyrosense.setText("X: " + event.values[0] + "\nY: " + event.values[1] + "\nZ: " + event.values[2]);
                                }
                            }
                            if (strcounter.equals("4")) {
                                ////////
                                if (strbcounter.equals("1")) { // Check which button 1 is motion
                                    //Get rid of the last values and set the one we are using
                                    gyrosense.setVisibility(View.GONE);
                                    uncalgyrosense.setVisibility(View.VISIBLE);
                                    //set the text inside infospace
                                    dynamicheader.setText("Raw Gyroscope:");
                                    //uncalgyrosense.setText("X: " + event.values[0] + "\nY: " + event.values[1] + "\nZ: " + event.values[2] +
                                    //        "\nX: " + event.values[3] + "\nY: " + event.values[4] + "\nZ: " + event.values[5]);
                                }

                            }
                            if (strcounter.equals("5")) {
                                ////////
                                if (strbcounter.equals("1")) { // Check which button 1 is motion
                                    uncalgyrosense.setVisibility(View.GONE);
                                    lineaccelsense.setVisibility(View.VISIBLE);
                                    //set the text inside infospace
                                    dynamicheader.setText("Linear Sensor:");
                                    //lineaccelsense.setText("X: " + event.values[0] + "\nY: " + event.values[1] + "\nZ: " + event.values[2]);
                                }
                            }
                            if (strcounter.equals("6")) {
                                ////////
                                if (strbcounter.equals("1")) { // Check if motion has been pressed
                                    lineaccelsense.setVisibility(View.GONE);
                                    rotatesense.setVisibility(View.VISIBLE);
                                    //set the text inside infospace
                                    dynamicheader.setText("Rotation Sensors");
                                    //rotatesense.setText("X: " + event.values[0] + "\nY: " + event.values[1] + "\nZ: " + event.values[2]);
                                }
                            }
                            if (strcounter.equals("7")) {
                                ////////
                                if (strbcounter.equals("1")) { // Check which button 1 is motion
                                    rotatesense.setVisibility(View.GONE);
                                    sigmotionsense.setVisibility(View.VISIBLE);
                                    //set the text inside infospace
                                    dynamicheader.setText("Significant Motion Sensor:");
                                   // sigmotionsense.setText(event.values[0] + "");
                                }

                            }
                            if (strcounter.equals("8")) {
                                ////////
                                if (strbcounter.equals("1")) { // Check which button 1 is motion
                                    sigmotionsense.setVisibility(View.GONE);
                                    stepstaken.setVisibility(View.VISIBLE);
                                    //set the text inside infospace
                                    dynamicheader.setText("Step Counter:");
                                }
                            }
                            if (strcounter.equals("9")) {
                                ////////
                                if (strbcounter.equals("1")) { // Check which button 1 is motion
                                    stepstaken.setVisibility(View.GONE);
                                    stepdetected.setVisibility(View.VISIBLE);
                                    //set the text inside infospace
                                    dynamicheader.setText("Step Detector:");
                                }
                            }
                        }

                        }
                    }
        );
        arrowdown.setOnClickListener( //This is for the down arrow
                new ImageButton.OnClickListener(){
                    public void onClick(View v){
                        if (strcounter.equals("9")||(strcounter.equals("0"))) // might be that we need to add a second variable for mot,pos,env
                        {
                            //Do nothing as we have reached the bottom
                        }
                        else
                        {
                            counter++; //how many times
                            strcounter = Integer.toString(counter);
                            //et1.setText(strcounter);
                            //now lets set text accordingly
                            if (strcounter.equals("8")) {
                                ////////
                                stepstaken.setVisibility(View.VISIBLE);
                                stepdetected.setVisibility(View.GONE);
                                //set the text inside infospace
                                dynamicheader.setText("Steps Taken:");

                            }
                            if (strcounter.equals("7")) {
                                ////////
                                sigmotionsense.setVisibility(View.VISIBLE);
                                stepstaken.setVisibility(View.GONE);
                                //set the text inside infospace
                                dynamicheader.setText("Significant Motion:");
                            }
                            if (strcounter.equals("6")) {
                                ////////
                                rotatesense.setVisibility(View.VISIBLE);
                                sigmotionsense.setVisibility(View.GONE);
                                //set the text inside infospace
                                dynamicheader.setText("Rotation Sensor:");
                            }
                            if (strcounter.equals("5")) {
                                ////////
                                lineaccelsense.setVisibility(View.VISIBLE);
                                rotatesense.setVisibility(View.GONE);
                                //set the text inside infospace
                                dynamicheader.setText("Linear Acceleration:");
                            }
                            if (strcounter.equals("4")) {
                                ////////
                                uncalgyrosense.setVisibility(View.VISIBLE);
                                lineaccelsense.setVisibility(View.GONE);
                                //set the text inside infospace
                                dynamicheader.setText("Raw Gyroscope:");

                            }
                            if (strcounter.equals("3")) {
                                ////////
                                gyrosense.setVisibility(View.VISIBLE);
                                uncalgyrosense.setVisibility(View.GONE);
                                //set the text inside infospace
                                dynamicheader.setText("Gyroscope:");

                            }
                            if (strcounter.equals("2")) {
                                ////////
                                gravitysense.setVisibility(View.VISIBLE);
                                gyrosense.setVisibility(View.GONE);
                                //set the text inside infospace
                                dynamicheader.setText("Gravity Sensor:");
                            }
                            if (strcounter.equals("1")) {
                                ////////
                                acceleration.setVisibility(View.VISIBLE);
                                gravitysense.setVisibility(View.GONE);
                                //set the text inside infospace
                                dynamicheader.setText("Acceleration:");
                            }
                        }

                    }
                });
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

        @Override
        public void onSensorChanged(SensorEvent event) {

        }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
}

该应用基本上监听一堆按钮,并设置一些可见和不可见等,用于基于按钮的自适应 UI。
接下来是为了节省内存,这样 SensorEventListener 就不会同时监听每个传感器,它包含在 OnClick 方法中。

目的是在点击时注册和注销。
第一个箭头注册加速度计,然后下一个箭头将取消注册加速度计并注册另一个传感器。
然后在 SensorChanged 中获取要显示的事件信息。

首先,我在没有任何处理程序的情况下尝试过,只是这样写的:

sm.registerListener.(this, accelerometer,   SensorManager.SENSOR_DELAY_NORMAL);

这标记了以下错误:

Cannot resolve method 'registerListener(anonymous  
android.view.View.OnClickListener, android.hardware.Sensor, int)'  

所以我认为它可能需要一个处理程序,所以更新后的代码显示我添加了一个处理程序,这需要设置为 Final,因为 android studio 标记了一个错误。所以我添加了您可以在 OnCreate 方法下看到的处理程序代码。插入处理程序带来了这个:

Cannot resolve method 'registerListener(anonymous 
android.view.View.OnClickListener, android.hardware.Sensor, int, 
android.os.Handler)'  

我不确定为什么这很难做到。我希望传感器管理器只听我需要的传感器,当我选择一个时。任何有关此事的帮助或澄清将不胜感激。

最佳答案

使用 MainActivity.this 而不是 this 作为 registerListenerunregisterListener 中的第一个参数。例如:

sm.registerListener.(MainActivity.this, accelerometer, 
                               SensorManager.SENSOR_DELAY_NORMAL);

原因在 onClick 方法中 this 指的是 onClick 方法的上下文,而不是 MainActivity,您在其中实现 SensorEventListener

关于java - android开发-注册和取消注册onClick,无法解析方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33079507/

相关文章:

android - 从android与socket.io通信

java - 在字符串数组中搜索确切的字符串?

java - 如何使用 JPQL 获取 blob 字段的长度?

java - 为什么java还接受 ""和 ' '之外的unicode?

java - 如何将类设置为始终在其自己的项目文件夹中搜索给定路径?

javascript - 是否可以阻止 Cordova 的相机插件在设备上存储图像?

Android:按百分比排名?

android - 如何复制 Facebook 相册画廊 (android)

android - Phonegap navigator.geolocation.getCurrentPosition 出错

java - MariaDb 列存储批量插入