简体   繁体   中英

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

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

}

`

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM