James
James

Reputation: 474

Android network connection

Could someone please explain why I'm getting the error:

java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.project/com.project.Deals}: java.lang.IllegalStateException: System services not available to Activities before onCreate() 

When I am using the two classes below, been doing this for ages now. I appreciate the help

public class Deals extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView textview = new TextView(this);
        textview.setText("This is the Artists tab");
        setContentView(textview);

        NetworkConnection nc = new NetworkConnection();
        boolean networkAvail = nc.isNetworkConnAvail();
        if (networkAvail == true){
        }

    }
};

public class NetworkConnection extends Activity {
/** Called when the activity is first created. */

public boolean isNetworkConnAvail() {

        ConnectivityManager connMgr = (ConnectivityManager) 
            getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo != null)
            return networkInfo.isConnected();

        return false;
    }
}

Upvotes: 0

Views: 318

Answers (2)

amp
amp

Reputation: 12352

Add this to your manifest:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

And remove this:

extends Activity

in class NetworkConnection

Update

Better idea is to change your code to:

public class Deals extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

boolean networkAvail = isNetworkConnAvail();
if (networkAvail == true){
  //do something
}

}

public boolean isNetworkConnAvail() {

ConnectivityManager connMgr = (ConnectivityManager) 
    getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null)
    return networkInfo.isConnected();

return false;
}

}

`

Upvotes: 1

user1243584
user1243584

Reputation:

The problem is in your NetworkConnection class you never call onCreate try this instead:

public boolean isNetworkConnAvail(Context context) {

    ConnectivityManager connMgr = (ConnectivityManager) 
        context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    if (networkInfo != null)
        return networkInfo.isConnected();

    return false;
}

And call it like this from your other class:

nc.isNetworkConnAvail(this);

Or alternatively call the onCreate method of the super class in your constructor in NetworkConnection. If you are only extending Activity for the sake of being able to use getSystemService then you may as well not extend Activity and just have the Context passed through either in the constructor or in the method itself as this will then give you access to those methods :)

Upvotes: 1

Related Questions