Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to initialize object which is extend to class Activity
    text
    copied!<p>I have main activity which call other object, for example GPS class. This GPS class extends Activiy class. </p> <p>snapshot main activity:</p> <pre><code>public class MemoPosition extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); GPS GPSnode = new GPS(); if (!GPSnode.isGPSenabled()){ createGpsDisabledAlert(); } } private void createGpsDisabledAlert(){ } } </code></pre> <p>snapshot GPS class:</p> <pre><code>public class GPS extends Activity{ private Double latitude; private Double longitude; private LocationManager locationManager; public void GPS(){ // Construction String context = Context.LOCATION_SERVICE; locationManager = (LocationManager)getSystemService(context); if (isGPSenabled()){ // do something } } </code></pre> <p>I've also put the GPS class to manifest as part of Activity apps. Running the code, and fatal error Force close. Logcat:</p> <pre><code>12-24 22:51:32.270: ERROR/AndroidRuntime(5558): FATAL EXCEPTION: main 12-24 22:51:32.270: ERROR/AndroidRuntime(5558): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dromdev.project.memoposition/com.dromdev.project.memoposition.MemoPosition}: java.lang.NullPointerException 12-24 22:51:32.270: ERROR/AndroidRuntime(5558): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 12-24 22:51:32.270: ERROR/AndroidRuntime(5558): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 12-24 22:51:32.270: ERROR/AndroidRuntime(5558): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 12-24 22:51:32.270: ERROR/AndroidRuntime(5558): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 12-24 22:51:32.270: ERROR/AndroidRuntime(5558): at android.os.Handler.dispatchMessage(Handler.java:99) 12-24 22:51:32.270: ERROR/AndroidRuntime(5558): at android.os.Looper.loop(Looper.java:123) 12-24 22:51:32.270: ERROR/AndroidRuntime(5558): at android.app.ActivityThread.main(ActivityThread.java:4627) 12-24 22:51:32.270: ERROR/AndroidRuntime(5558): at java.lang.reflect.Method.invokeNative(Native Method) 12-24 22:51:32.270: ERROR/AndroidRuntime(5558): at java.lang.reflect.Method.invoke(Method.java:521) 12-24 22:51:32.270: ERROR/AndroidRuntime(5558): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:876) 12-24 22:51:32.270: ERROR/AndroidRuntime(5558): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634) 12-24 22:51:32.270: ERROR/AndroidRuntime(5558): at dalvik.system.NativeStart.main(Native Method) 12-24 22:51:32.270: ERROR/AndroidRuntime(5558): Caused by: java.lang.NullPointerException 12-24 22:51:32.270: ERROR/AndroidRuntime(5558): at com.dromdev.project.memoposition.GPS.isGPSenabled(GPS.java:48) 12-24 22:51:32.270: ERROR/AndroidRuntime(5558): at com.dromdev.project.memoposition.MemoPosition.onCreate(MemoPosition.java:23) 12-24 22:51:32.270: ERROR/AndroidRuntime(5558): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 12-24 22:51:32.270: ERROR/AndroidRuntime(5558): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 12-24 22:51:32.270: ERROR/AndroidRuntime(5558): ... 11 more </code></pre> <p>I'm curious if class which inherit activity class couldn't be initialized as normal Activity. Should it be called by Intent ? I just need GPS class as object which responsible to GPS functionality, however it's required to extend Activity for some context. Please advice.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload