I am trying to convert one of my apps to now use Fragments. This app also uses the Android License checker. While testing I had the license checker code commented out and now have the app working like I want. I am now ready to uncomment that code so I can put it back on the Market but when I do I get this message every other time I start the app:
java.lang.IllegalStateException: Fragment MainHomeFragment{40544bd8} not attached to Activity
This happens if I test on the phone or with the emulator. It almost seems like the license checker is not finishing fast enough so when it inflates the Fragment there is no Activity to attach it to. At first I was just starting the app and then pressing the back button once it loaded and tried it again right away. I thought the license checker might have still been running from the first try so the next time I waited 5 minutes before using the back key and still get the same issue. The weird thing is after the app blows up the 2nd time if I try it again it works but on the 4th try it fails again so something must not be getting cleared out from the first try that is successful. If I comment out the LicenseChecker code and run it again it works every time. I tried putting the call to the LicenseChecker in a new Class as a Thread so it wouldn't hold up the Activity being created but still get the same issue. Any ideas on what I should try to fix this?
Here is the onCreate code for my main FragmentActivity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork =
connectivityManager.getActiveNetworkInfo();
if (activeNetwork != null) {
android_id = Secure.getString(this.getContentResolver(),
Secure.ANDROID_ID);
mObsfuscator = new AESObfuscator(SALT, getPackageName(), android_id);
ServerManagedPolicy serverPolicy = new
ServerManagedPolicy(this, mObsfuscator);
mLicenseCheckerCallback = new MyLicenseCheckerCallback();
mChecker = new LicenseChecker(this, serverPolicy,
BASE64_PUBLIC_KEY);
mChecker.checkAccess(mLicenseCheckerCallback);
}
setContentView(R.layout.fragment_layout);
}
Why don't you initially do a setContentView
with loading view and then after finishing the licensing procedure, you do the "real" setContentView
with your fragment?
I think that would work ok.