In my app, I have an entry activity (dummy, without layout) which checks the accounts in the account manager:
Here's the code in the EntryActivity
:
public class EntryActivity extends Activity {
// Tag
private String TAG = getClass().getSimpleName();
// Inst.
private AccountManager accountManager;
private Account[] accounts;
private Authenticator authenticator;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
authenticator = new Authenticator(this);
// Check for the account in account manager
accountManager = AccountManager.get(this);
accounts = accountManager.getAccountsByType(HConstants.ACCOUNT_TYPE);
int nAccounts = accounts.length;
if(nAccounts!=0){
// There's an account
}
else{
// Prompt user to enter creds
final Intent intent = new Intent(this, AuthenticationActivity.class);
startActivity(intent);
}
}
The problem is, I can't start the AuthenticationActivity
and I get this error:
java.lang.IllegalStateException: System services not available to Activities before onCreate()
How can I resolve this?
Update: Code for AuthenticationActivity
:
public class AuthenticationActivity extends Activity {
// Instances
AccountManager accountManager = AccountManager.get(this);
// Values for email and password at the time of the login attempt.
private String mEmail;
private String mPassword;
// UI references.
private EditText mEmailView;
private EditText mPasswordView;
private View mLoginFormView;
private View mLoginStatusView;
private TextView mLoginStatusMessageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.authentication_activity);
// Set up the login form.
mEmailView = (EditText) findViewById(R.id.email);
mEmailView.setText(mEmail);
mPasswordView = (EditText) findViewById(R.id.password);
mLoginFormView = findViewById(R.id.login_form);
mLoginStatusView = findViewById(R.id.login_status);
mLoginStatusMessageView = (TextView) findViewById(R.id.login_status_message);
}
Put your check into onStart()
instead of onCreate()
. After starting the other activity, call finish()
to stop your dummy activity.
Try using onResume()
method. If this does not work directly try the following code (I don't fully recommend this):
new Handler().postDelayed(new Runnable(){
public void run(){
//Start your activity..
}
}
This code inside your onResume
I guess the problem is in this line:
private String TAG = getClass().getSimpleName();
Try to move it into the onCreate
method after the super
. Hope this helps.
The problem was in this line:
AccountManager accountManager = AccountManager.get(this);
I was using the context (system service) before onCreate()
. By putting it inside the method, the error was resolved.
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.