IllegalStateException异常的片段未连接到活动 [英] IllegalStateException for Fragment not attached to Activity

查看:109
本文介绍了IllegalStateException异常的片段未连接到活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想转换我的应用程序现在使用片段之一。该应用程序还采用了Android许可检查。虽然测试我有执照检查code注释掉了,现在已经应用程序的工作像我想要的。现在我已经准备好取消对code这样我就可以把它放回市场,但只有这样,我得到这个消息,每个其他时间我启动应用程序:

java.lang.IllegalStateException:片段MainHomeFragment {40544bd8}未连接到活动

这会发生,如果我测试的手机或模拟器。它似乎像许可证检查未整理速度不够快,所以当它膨胀的碎片没有任何活动将其附加到。起初,我才刚刚开始应用程序,然后pressing后退按钮,一旦它加载并试图再次的时候了。我以为许可证检查程序可能仍一直运行在第一次尝试这样下次我等5分钟使用返回键,仍然得到同样的问题了。奇怪的是后应用程序吹了第二次,如果我再次尝试它的工作原理,但在第4尝试再次失败等等的东西不能得到清理出的第一个尝试是成功的。如果我注释掉LicenseChecker code,然后再次运行它屡试不爽。我试图把呼叫转移到LicenseChecker在一个新的类作为一个线程,这样就不会耽误所创建的活动,但仍然得到了同样的问题。什么我应该尝试解决这个问题的任何想法?

下面是的onCreate $ C $下我的主要FragmentActivity:

 公共无效的onCreate(包savedInstanceState){
  super.onCreate(savedInstanceState);

  ConnectivityManager connectivityManager
    =(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
  的NetworkInfo activeNetwork =
    connectivityManager.getActiveNetworkInfo();

  如果(activeNetwork!= NULL){
    android_id = Secure.getString(this.getContentResolver(),
      Secure.ANDROID_ID);
    mObsfuscator =新AESObfuscator(SALT,getPackageName(),android_id);
    ServerManagedPolicy serverPolicy =新
      ServerManagedPolicy(这一点,mObsfuscator);
    mLicenseCheckerCallback =新MyLicenseCheckerCallback();
    mChecker =新LicenseChecker(这一点,serverPolicy,
      BASE64_PUBLIC_KEY);
    mChecker.checkAccess(mLicenseCheckerCallback);
  }

  的setContentView(R.layout.fragment_layout);
}
 

解决方案

你为什么不开始做的setContentView 与装载视图,然后在完成发牌程序后,你做真正的的setContentView 您的片断?

我认为这工作正常。

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.

这篇关于IllegalStateException异常的片段未连接到活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆