2015-04-29 87 views
1

我有gridview項目點擊項目我移動到具有片段的活動。當我去活動回來GridView和立即去爲它失敗與下面的異常其他項目活動:片段已激活:非法狀態異常

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2318) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396) 
     at android.app.ActivityThread.access$800(ActivityThread.java:139) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:149) 
     at android.app.ActivityThread.main(ActivityThread.java:5257) 
     at java.lang.reflect.Method.invokeNative(Method.java) 
     at java.lang.reflect.Method.invoke(Method.java:515) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633) 
     at dalvik.system.NativeStart.main(NativeStart.java) 
Caused by: java.lang.IllegalStateException: Fragment already active 
     at android.support.v4.app.Fragment.setArguments(Fragment.java:530) 
     at com.buzzreel.MyActivity.onCreate(MyActivity.java:81) 
     at android.app.Activity.performCreate(Activity.java:5411) 
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396) 
     at android.app.ActivityThread.access$800(ActivityThread.java:139) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:149) 
     at android.app.ActivityThread.main(ActivityThread.java:5257) 
     at java.lang.reflect.Method.invokeNative(Method.java) 
     at java.lang.reflect.Method.invoke(Method.java:515) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633) 
     at dalvik.system.NativeStart.main(NativeStart.java) 

這是MyActivity的onCreate(我的片段的代碼):

Fragment myFragment = MyFragment.getInstance(); 
       Bundle profileArgs = new Bundle(); 
       profileArgs.putSerializable("data", customObject); 
       myFragment.setArguments(profileArgs); 

       FragmentManager manager = getSupportFragmentManager(); 

       FragmentTransaction transaction = manager.beginTransaction(); 

       transaction.add(R.id.fragment_info, myFragment, 
          "MyFragment"); 
       transaction.commit(); 

這是getInstance方法在片段類:

public static MyFramgent getInstance() { 
         return new MyFramgent(); 
       } 

回答

0

的問題是,您使用的是相同的片段,每次堆棧中命名您點擊這裏一個GridView項:

transaction.add(R.id.fragment_info, myFragment, "MyFragment"); 

你需要做的是設置該字符串爲null:

transaction.add(R.id.fragment_info, myFragment, null); 

,或者如果你正在使用的名字從返回堆棧,然後使用不同的名稱爲每個GridView的的onclick的

+0

嘗試不工作..有時它仍然崩潰。 – virendrao