片段从活动按钮打开

时间:2016-06-23 12:02:04

标签: java android android-fragments

MainActivity.java

btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentOne fragment = new FragmentOne();
                fragmentTransaction.add(R.id.layoutFragmentContainer, fragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }
        });

...

FragmentOne.java

public class FragmentOne extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.list,container,false);
    }
}

当我点击按钮搜索时,片段出现,但是当我再次点击它时,应用程序崩溃。可以有人帮忙,我是android的新手,我无法解决这个问题。

.... 日志:

 Process: com.example.user1.volleyballmanager, PID: 26892
                                                                                     java.lang.IllegalStateException: commit already called
                                                                                         at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:625)
                                                                                         at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:617)
                                                                                         at com.example.user1.volleyballmanager.MainActivity$2.onClick(MainActivity.java:52)--- 
line 52 : fragmentTransaction.commit();
                                                                                         at android.view.View.performClick(View.java:5156)
                                                                                         at android.view.View$PerformClick.run(View.java:20755)
                                                                                         at android.os.Handler.handleCallback(Handler.java:739)
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                         at android.os.Looper.loop(Looper.java:145)
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:5832)
                                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                                         at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

2 个答案:

答案 0 :(得分:1)

将此方法放入主要活动

//FRAGMENT BACK STACK
public void getFragmentWithTag(Fragment fragment, String tag) {
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.addToBackStack(null);
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    transaction.replace(R.id.activityFrame, fragment, tag).commit();
}

您可以像这样从您的片段中调用它。

activity.getFragmentWithTag(Fragment, FragmentTag)

答案 1 :(得分:0)

您不能拥有多个事务提交。 所以你需要开始和关闭交易。所以当你点击一个时,它就可以了。然后再次点击,它会给出错误,上面写着一个交易< - >一次提交。