Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Call Class

Call Class

Scheduled Pinned Locked Moved Java
androidcomhelptutorial
20 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Smaini Nurbs

    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(); } }

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #4

    Please use <pre></pre> tags rather than <code></code> to surround your code blocks. Use the code block not the inline code button.

    Just say 'NO' to evaluated arguments for diadic functions! Ash

    S 1 Reply Last reply
    0
    • T TorstenH

      Need some help OK - it would help if you tell us what kind of help you need. regards Torsten

      I never finish anyth...

      S Offline
      S Offline
      Smaini Nurbs
      wrote on last edited by
      #5

      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.

      T 1 Reply Last reply
      0
      • L Lost User

        Please use <pre></pre> tags rather than <code></code> to surround your code blocks. Use the code block not the inline code button.

        Just say 'NO' to evaluated arguments for diadic functions! Ash

        S Offline
        S Offline
        Smaini Nurbs
        wrote on last edited by
        #6

        Ok. Thanks for the info. I didn't know cos' I'm new here.

        L 1 Reply Last reply
        0
        • S Smaini Nurbs

          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.

          T Offline
          T Offline
          TorstenH
          wrote on last edited by
          #7

          TestSensors extends ChronometerDemo1

          you extended the ChrometerDemo1 class. regards Torsten

          I never finish anyth...

          S 1 Reply Last reply
          0
          • T TorstenH

            TestSensors extends ChronometerDemo1

            you extended the ChrometerDemo1 class. regards Torsten

            I never finish anyth...

            S Offline
            S Offline
            Smaini Nurbs
            wrote on last edited by
            #8

            So after extending then it still doesn't work.

            1 Reply Last reply
            0
            • S Smaini Nurbs

              Ok. Thanks for the info. I didn't know cos' I'm new here.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #9

              No problem, it just makes code easier to read when trying to answer questions.

              Just say 'NO' to evaluated arguments for diadic functions! Ash

              1 Reply Last reply
              0
              • S Smaini Nurbs

                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(); } }

                N Offline
                N Offline
                Nagy Vilmos
                wrote on last edited by
                #10

                In the class TestSensors you need to decide correctly the relationship between it and ChronometerDemo1. If the relationship is is a, then it should extend, if the relationship is has a then there should be a member variable. You seem to be confusing the two. Now if the you really do wish to extend ChronometerDemo1, then you do not need to create an instance as it already is a ChronometerDemo1 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

                1 Reply Last reply
                0
                • S Smaini Nurbs

                  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,

                  D Offline
                  D Offline
                  David Skelly
                  wrote on last edited by
                  #11

                  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.

                  S 1 Reply Last reply
                  0
                  • D David Skelly

                    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.

                    S Offline
                    S Offline
                    Smaini Nurbs
                    wrote on last edited by
                    #12

                    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 } }

                    D 1 Reply Last reply
                    0
                    • S Smaini Nurbs

                      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 } }

                      D Offline
                      D Offline
                      David Skelly
                      wrote on last edited by
                      #13

                      This tutorial series may help: Android Full Application Tutorial[^] It seems to be fairly comprehensive.

                      S 1 Reply Last reply
                      0
                      • S Smaini Nurbs

                        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(); } }

                        M Offline
                        M Offline
                        moxwose
                        wrote on last edited by
                        #14

                        a static funtion can't call the instance function; you must known that;

                        S D 2 Replies Last reply
                        0
                        • M moxwose

                          a static funtion can't call the instance function; you must known that;

                          S Offline
                          S Offline
                          Smaini Nurbs
                          wrote on last edited by
                          #15

                          I'm sorry, no, I don't know. So now how should I edit?

                          1 Reply Last reply
                          0
                          • D David Skelly

                            This tutorial series may help: Android Full Application Tutorial[^] It seems to be fairly comprehensive.

                            S Offline
                            S Offline
                            Smaini Nurbs
                            wrote on last edited by
                            #16

                            Ok, thanks.

                            1 Reply Last reply
                            0
                            • M moxwose

                              a static funtion can't call the instance function; you must known that;

                              D Offline
                              D Offline
                              David Skelly
                              wrote on last edited by
                              #17

                              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?

                              M 1 Reply Last reply
                              0
                              • D David Skelly

                                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?

                                M Offline
                                M Offline
                                moxwose
                                wrote on last edited by
                                #18

                                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;

                                D 1 Reply Last reply
                                0
                                • M moxwose

                                  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;

                                  D Offline
                                  D Offline
                                  David Skelly
                                  wrote on last edited by
                                  #19

                                  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?

                                  M 1 Reply Last reply
                                  0
                                  • D David Skelly

                                    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?

                                    M Offline
                                    M Offline
                                    moxwose
                                    wrote on last edited by
                                    #20

                                    yes; what may be the reason your program failed;

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups