“System services not available to Activities before onCreate()” Error message?

荒凉一梦 提交于 2019-11-29 06:13:12

I think it's because your instantiating an onClick listener before on create is called. Try instantiating the onClick listener inside the onCreate() method.

This may or may not be the case with the AlertDialog too, but I'm not entirely sure.

Technically I believe it is the following line that causes the problem:

ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

However, because this is being called within the isNetworkConnected() method which in turn is called within your onClick method, moving the instantiation of the onClick fixes the problem.

The clue is in the exception System services not available to Activities before onCreate()

Error is due to create this object creation.

AlertDialog.Builder builder = new AlertDialog.Builder(this);

you should do this after onCreate has been invoked.

To call system services we have to use running activity. That means we need executed onCreate method that inherited to the super. So to identify that we have to use the current application context to call system service.

use

ConnectivityManager cm = (ConnectivityManager) getBaseContext().getSystemService(Context.CONNECTIVITY_SERVICE);

or if we have context object that reference to Context, we can use it as below

ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

The problem is that you define "listener" as a global variable. Since it's given in the error message: System services not available to Activities before onCreate().

Your onCreate method should be like this:

private OnClickListener listener = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listener = new OnClickListener() {

    public void onClick(View v) {
        if (isNetworkConnected()) {
            builder.setMessage("Internet connected!").setCancelable(false)
            .setPositiveButton("OK", null);
            builder.create().show();
        } else {
            builder.setMessage("Internet isn\'t connected!")
            .setCancelable(false)
            .setPositiveButton("OK", null);
            builder.create().show();
        }

    }
};


    findViewById(R.id.icoMyIcon).setOnClickListener(listener);

}
Jitesh Dalsaniya

add the following permission to AndroidManifest.xml file.

i think you forget to add this permission.

android.permission.ACCESS_NETWORK_STATE

it will help you.

Also, if there's an inner class, say class MyAdapter extends ArrayAdapter<myModel> or similar, it helps NOT to instantiate it - (MyAdapter = new mAdapter<mModel>()) before the activity's onCreate().

Correct answer is

    AlertDialog.Builder builder = new AlertDialog.Builder(this);

which is already mentioned by jeet and reason is you have initialized AlertDialog before any lifecycle method of activity executed with Activity context that is logically not correct.

And solution to your problem is

private OnClickListener listener = new OnClickListener() {

public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
    if (isNetworkConnected()) {
        builder.setMessage("Internet connected!").setCancelable(false)
        .setPositiveButton("OK", null);
        builder.create().show();
    } else {
        builder.setMessage("Internet isn\'t connected!")
        .setCancelable(false)
        .setPositiveButton("OK", null);
        builder.create().show();
    }

}

};

Initialize alert dialog when it need to visible. Reason behind posting answer to this old thread is the accepted answer and Jeet's answer did not solve the issue even if you move your onclick listener out of onCreate() still issue will be same.

Today I came across same issue with kotlin where if internet not availbe then show error dialog and my silly mistake was

instead of passing context as "this" I passed it as MainActivity()

Correct R.string.error.errorDialog(this) //

Wrong R.string.error.errorDialog(MainActivity())

in my case, I got error message : “System services not available to Activities before onCreate()”

when I initialize class property using context like below

 class MainActivity : AppCompatActivity() {

        // this line below
        private val notificationManager: NotificationManagerCompat = NotificationManagerCompat.from(this) 

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
        }

    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!