单击时替换片段

时间:2016-09-28 08:46:40

标签: android android-fragments floating-action-button

当我点击FloatingActionButton时,我想用新的片段替换当前的片段。我尝试使用fragmentReplace,但我的应用程序一直在崩溃。

我错过了什么吗?

代码:

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            android.app.Fragment someFrag = new NewEventActivity();
            replaceFragment(someFrag);

            //Snackbar.make(view, "Hallo du heimlich pupsende :P", Snackbar.LENGTH_LONG)
             //       .setAction("Action", null).show();
        }

    });

}


public void replaceFragment(android.app.Fragment someFrag){
    android.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.newEvent, someFrag);
    transaction.addToBackStack(null);
    transaction.commit();
}

片段:

public class NewEventActivity extends android.app.Fragment {

LayoutInflater inflater;


public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.new_event_fragment, container, false);


    return rootView;
   }
 }

logcat的

  

进程:com.example.danny.myapplication,PID:3123                                                                                  java.lang.IllegalArgumentException:找不到id 0x7f0c0082的视图   片段的(com.example.danny.myapplication:id / newEvent)   NewEventActivity {bd7d3d#2 id = 0x7f0c0082}                                                                                      在   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1059)                                                                                      在   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)                                                                                      在   android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)                                                                                      在   android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)                                                                                      在   android.support.v4.app.FragmentManagerImpl $ 1.run(FragmentManager.java:517)                                                                                      在android.os.Handler.handleCallback(Handler.java:739)                                                                                      在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                      在android.os.Looper.loop(Looper.java:148)                                                                                      在android.app.ActivityThread.main(ActivityThread.java:5417)                                                                                      at java.lang.reflect.Method.invoke(Native Method)                                                                                      在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                                      在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

2 个答案:

答案 0 :(得分:2)

您的视图 R.id.newEvent 应该是您在活动中的setContentView()方法中定义的内容的子布局。

答案 1 :(得分:0)

试试这个,

FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().replace(R.id.main_activity,
new YourNewFragment()).addToBackStack(YourPreviousFragment.class.getName())
.commit();