Helper Class - Error Unable to start activity ComponentInfo:
Asked Answered
F

2

1

Error Unable to start activity ComponentInfo: java.lang.IllegalStateException: System services not available to Activities before onCreate()

I´m experimenting with seperating the code and the use of a helper class. (Created different Java files) What I did is created an Activity Java file that is registred in the Manifest and I didn´t register this following class (Java file):

import android.app.Activity;
import android.location.LocationManager;
import android.net.ConnectivityManager;
....


public class DeviceMonitor extends Activity {

    boolean laag=false;
    int level=-1;
    double batterylevel=-1;


    public boolean GPSEnabled() {

    final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) { // NO GPS ENABLED
        //ErrorMessage.append(R.string.enablegps);
        //ErrorMessage.append("\n");
        Log.d("StartPrepare","GPS DISABLED");
        return false;
                    } else {
        Log.d("StartPrepare","GPS ENABLED");
        return true;
                    }
    }

I removed the OnCreate() method, is this correct? Should I have registred in the Manifest, if yes, how?

I received the following error while calling from the registred Activity like this:

DeviceMonitor MyDevice = new DeviceMonitor();
if (MyDevice.GPSEnabled()){gpsenabled=true;}else{gpsenabled=false;fout=true;}

Error:

E/AndroidRuntime(1912): java.lang.RuntimeException: Unable to start activity ComponentInfo{package}: java.lang.IllegalStateException: System services not available to Activities before onCreate()

Anybody that can give me some light on the helper classes (I´m kind of new in Java/Android) and has any clue what the error can cause? I tried to add the OnCreate() method, but it didn´t help.

Thanks a lot!

Fathom answered 28/12, 2011 at 21:55 Comment(2)
Context is available inside onCreate() method of an activity. You have removed onCreate() which might be causing the issue. If you want to treat this as Helper class, why you extended Activity? I would suggest have some method over here which will be called from your activity. While calling the method your activity need to pass the context.Alvertaalves
So the method in the activity that is calling should pass the context? Can you give me en example in code? ThanksFathom
Y
1

Do NOT do this...

DeviceMonitor MyDevice = new DeviceMonitor();

DeviceMonitor extends Activity and you should never create an instance of an Activity using new. An Android Activity is a special-case class and shouldn't be treated like a normal Java class.

If you want to start an Activity you need to do it using startActivity...) or one of the other 'start' methods.

If you want a 'helper' class just create a standard Java class which doesn't extend anything. When you create an instance of it from your main Activity, pass the Activity Context to it in its constructor then use that to access Android services etc. Example...

public class DeviceMonitor {

    Context mContext = null;

    public DeviceMonitor (Context context) {
        mContext = context;
    }
}

EDIT: To create your helper and pass a Context from your main Activity do this...

// An Activity IS a Context so pass 'this'
DeviceMonitor MyDevice = new DeviceMonitor(this);
Yore answered 28/12, 2011 at 22:3 Comment(3)
thanks! I don´t want to start another activity, so I indeed should remove the extend activity. Though I´m not sure how I should solve the unavailable methods. If I don´t extend the class actity, suddenly the getSystemservice is not available for example. Do I still need the onCreate() method?Fathom
As I said, use the Context you passed into the helper class as in mContext.getSystemService(...). No, your helper is not an Android component so onCreate(...) is not a relevant method for it.Yore
Also see the edit to my answer on passing this to the helper's constructor as the ContextYore
L
0

You need to declare your

boolean GPSEnabled 

after onCreate() method is callded in your class or service.

Lingulate answered 28/12, 2011 at 21:57 Comment(1)
Sorry, if I ask stupid questions, but I´m still figuring all this out. @thinksteep, if I don´t extend the Activity class, my getSystemService is not recognized (not available). What is the correct way to handle this? Further I put back the onCreate() method, but it doesnt solve the issue. Do I have to register the file in the Manifest?Fathom

© 2022 - 2024 — McMap. All rights reserved.