Android Development – Registering and unregistering onClick, unable to resolve methods
Consider this code:
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) {
}
}
The app basically listens to a bunch of buttons and sets some visible and invisible things for a button-based adaptive UI.
Next is to save memory so that the SensorEventListener does not listen to each sensor at the same time, which is included in the OnClick method.
The purpose is to register and deregister when clicked.
The first arrow registers the accelerometer, and then the next arrow unregisters the accelerometer and registers another sensor.
Then get the event information to display in SensorChanged
.
First of all, I tried without any handler, just wrote it like this :
sm.registerListener.( this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
This flags the following error:
Cannot resolve method 'registerListener(anonymous
android.view.View.OnClickListener, android.hardware.Sensor, int)'
So I thought it might need a handler, so the updated code shows that I added a handler, which needs to be set to Final because android studio flagged an error. So I added the handler code that you can see under the OnCreate method. The insert handler brings this:
Cannot resolve method 'registerListener(anonymous
android.view.View.OnClickListener, android.hardware.Sensor, int,
android.os.Handler)'
I’m not sure why this is so hard to do. I want the sensor manager to listen only to the sensors I need, when I choose one. Any help or clarification on this matter would be appreciated.
Solution
Use MainActivity.this
instead of this
as the first parameter in registerListener
and unregisterListener
. For example:
sm.registerListener.( MainActivity.this, accelerometer,
SensorManager.SENSOR_DELAY_NORMAL);
Why: In the onClick method,
this
refers to the context of the onClick
method, not the MainActivity
, where you implement the SensorEventListener