Call Class
-
I can't seem to call the ChronometerDemo class from the TestSensor class. My intention is to start the ChronometerDemo once the values of the X and Y-axis are 0.
-
TestSensors extends ChronometerDemo1
you extended the ChrometerDemo1 class. regards Torsten
I never finish anyth...
So after extending then it still doesn't work.
-
Ok. Thanks for the info. I didn't know cos' I'm new here.
-
Hi all, I'm trying to call my 'ChronometerDemo1' class from 'TestSensors' but its just not working. Need some help. Sorry if its abit long. Thanks in advance.
package com.example.testsensors; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.os.SystemClock; import android.util.Log; import android.view.Menu; import android.widget.Chronometer; import android.widget.Toast; public class ChronometerDemo1 extends Activity { private static final String TAG = "CustomChronometerActivity"; private static final String MS_ELAPSED = "com.etc.etc.MsElapsed"; private static MyChronometer chrono; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //start the chronometer chrono = new MyChronometer(this); chrono.start(); setContentView(chrono); } @Override protected void onPause() { Log.i(TAG, "onPause()"); super.onPause(); chrono.stop(); } @Override protected void onResume() { Log.i(TAG, "onResume()"); super.onResume(); chrono.start(); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); Log.i(TAG, "onSaveInstanceState()"); chrono.stop(); outState.putInt(MS_ELAPSED, chrono.getMsElapsed()); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); Log.i(TAG, "onRestoreInstanceState()"); int ms = savedInstanceState.getInt(MS_ELAPSED); chrono.setMsElapsed(ms); chrono.start(); } class MyChronometer extends Chronometer { public int msElapsed; public boolean isRunning = false; public MyChronometer(Context context) { super(context); } public int getMsElapsed() { return msElapsed; } public void setMsElapsed(int ms) { setBase(getBase() - ms); msElapsed = ms; } @Override public void start() { super.start(); setBase(SystemClock.elapsedRealtime() - msElapsed); isRunning = true; } @Override public void stop() { super.stop(); if(isRunning) { msElapsed = (int)(SystemClock.elapsedRealtime() - this.getBase()); } isRunning = false; } } public static void onCreate() { // TODO Auto-generated method stub chrono.start(); } }
In the class
TestSensors
you need to decide correctly the relationship between it andChronometerDemo1
. If the relationship isis a
, then it shouldextend
, if the relationship ishas a
then there should be a member variable. You seem to be confusing the two. Now if the you really do wish toextend ChronometerDemo1
, then you do not need to create an instance as it already is aChronometerDemo1
with added bits; thats the extension. So finally, how do you call the methods on the super class?super.onCreate();
Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
-
package com.example.testsensors; import java.util.List; import android.app.Activity; import android.content.res.Resources; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.widget.TextView; public class TestSensors extends ChronometerDemo1 implements SensorEventListener { private boolean mRegisteredSensor; private SensorManager mSensorManager; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mRegisteredSensor = false; mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); } protected void onResume() { super.onResume(); List sensors = mSensorManager .getSensorList(Sensor.TYPE_ALL); if (sensors.size() > 0) { Sensor sensor = sensors.get(0); mRegisteredSensor = mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_FASTEST); } } @Override protected void onPause() { if (mRegisteredSensor) { mSensorManager.unregisterListener(this); mRegisteredSensor = false; } super.onPause(); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { // TODO Auto-generated method stub } @Override public void onSensorChanged(SensorEvent event) { // TODO Auto-generated method stub StringBuffer buff = new StringBuffer(); /*if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) { Log.v("ORIENTATION", String.valueOf(event.values[0]) + ", " + String.valueOf(event.values[1]) + ", " + String.valueOf(event.values[2])); buff.append("ORIENTATION\n"); buff.append("Azimuth,rotation the Y axis").append(event.values[0]).append("\n"); buff.append("Pitch,
What do you mean when you say "its just not working"? It doesn't compile, it throws an exception, nothing happens? Looking at your code, I can see a potential problem. If onSensorChanged is called for the first time when values[0] is greater than 0.5 and values[1] is less than 1.2 then you will get a null pointer, because the static instance chrono of ChronometerDemo1 will not have been initialised. I think that the other comments are right, you seem to have got a bit confused with the "is a" versus "has a" relationship.
-
What do you mean when you say "its just not working"? It doesn't compile, it throws an exception, nothing happens? Looking at your code, I can see a potential problem. If onSensorChanged is called for the first time when values[0] is greater than 0.5 and values[1] is less than 1.2 then you will get a null pointer, because the static instance chrono of ChronometerDemo1 will not have been initialised. I think that the other comments are right, you seem to have got a bit confused with the "is a" versus "has a" relationship.
I'm doing a Accelerometer on Android. I want the program to trigger the Chronometer if the values of the X and Y axis are 0. My codes for the Chronometer is correct. But I'm not sure if the code below is the correct way of calling the Chronometer class. Thanks for the help in advance.
package com.example.testsensors; import android.content.Intent; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; public class Decision extends ChronometerDemo1 { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } public void onSensorChanged(SensorEvent event) { if (event.values[0] <= 0.3 && event.values[0] > 0.0) { Intent i = new Intent(this,ChronometerDemo1.class); startActivity(i); } else if (event.values[1] <= 1.2 && event.values[1] > 0.0) { Intent i = new Intent(this,ChronometerDemo1.class); startActivity(i); } else if (event.values[2] <= 10 && event.values[2] > 0.0) { Intent i = new Intent(this,ChronometerDemo1.class); startActivity(i); } else{ //ChronometerDemo1.onCreate(); Intent i = new Intent(this,ChronometerDemo1.class); startActivity(i); } } public static void onCreate() { // TODO Auto-generated method stub } }
-
I'm doing a Accelerometer on Android. I want the program to trigger the Chronometer if the values of the X and Y axis are 0. My codes for the Chronometer is correct. But I'm not sure if the code below is the correct way of calling the Chronometer class. Thanks for the help in advance.
package com.example.testsensors; import android.content.Intent; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; public class Decision extends ChronometerDemo1 { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } public void onSensorChanged(SensorEvent event) { if (event.values[0] <= 0.3 && event.values[0] > 0.0) { Intent i = new Intent(this,ChronometerDemo1.class); startActivity(i); } else if (event.values[1] <= 1.2 && event.values[1] > 0.0) { Intent i = new Intent(this,ChronometerDemo1.class); startActivity(i); } else if (event.values[2] <= 10 && event.values[2] > 0.0) { Intent i = new Intent(this,ChronometerDemo1.class); startActivity(i); } else{ //ChronometerDemo1.onCreate(); Intent i = new Intent(this,ChronometerDemo1.class); startActivity(i); } } public static void onCreate() { // TODO Auto-generated method stub } }
This tutorial series may help: Android Full Application Tutorial[^] It seems to be fairly comprehensive.
-
Hi all, I'm trying to call my 'ChronometerDemo1' class from 'TestSensors' but its just not working. Need some help. Sorry if its abit long. Thanks in advance.
package com.example.testsensors; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.os.SystemClock; import android.util.Log; import android.view.Menu; import android.widget.Chronometer; import android.widget.Toast; public class ChronometerDemo1 extends Activity { private static final String TAG = "CustomChronometerActivity"; private static final String MS_ELAPSED = "com.etc.etc.MsElapsed"; private static MyChronometer chrono; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //start the chronometer chrono = new MyChronometer(this); chrono.start(); setContentView(chrono); } @Override protected void onPause() { Log.i(TAG, "onPause()"); super.onPause(); chrono.stop(); } @Override protected void onResume() { Log.i(TAG, "onResume()"); super.onResume(); chrono.start(); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); Log.i(TAG, "onSaveInstanceState()"); chrono.stop(); outState.putInt(MS_ELAPSED, chrono.getMsElapsed()); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); Log.i(TAG, "onRestoreInstanceState()"); int ms = savedInstanceState.getInt(MS_ELAPSED); chrono.setMsElapsed(ms); chrono.start(); } class MyChronometer extends Chronometer { public int msElapsed; public boolean isRunning = false; public MyChronometer(Context context) { super(context); } public int getMsElapsed() { return msElapsed; } public void setMsElapsed(int ms) { setBase(getBase() - ms); msElapsed = ms; } @Override public void start() { super.start(); setBase(SystemClock.elapsedRealtime() - msElapsed); isRunning = true; } @Override public void stop() { super.stop(); if(isRunning) { msElapsed = (int)(SystemClock.elapsedRealtime() - this.getBase()); } isRunning = false; } } public static void onCreate() { // TODO Auto-generated method stub chrono.start(); } }
-
I'm sorry, no, I don't know. So now how should I edit?
-
This tutorial series may help: Android Full Application Tutorial[^] It seems to be fairly comprehensive.
Ok, thanks.
-
I've looked at the source code and I can't see where an instance method is called from a static context. Maybe I'm missing it, which method are you referring to?
-
I've looked at the source code and I can't see where an instance method is called from a static context. Maybe I'm missing it, which method are you referring to?
-
public static void onCreate() { // TODO Auto-generated method stub chrono.start(); } here ,chrono is instance which should not been called in a static method onCreate. you may try it : make the onCreate method not static;
chrono is a static variable, why shouldn't it be accessed from within a static method? The weakness in the design is that chrono is a static variable which is initialised in a non-static method (which is confusingly also called onCreate), so if the static method onCreate is called before the instance method onCreate then this will fail with a null pointer exception. Is that what you mean?
-
chrono is a static variable, why shouldn't it be accessed from within a static method? The weakness in the design is that chrono is a static variable which is initialised in a non-static method (which is confusingly also called onCreate), so if the static method onCreate is called before the instance method onCreate then this will fail with a null pointer exception. Is that what you mean?