从另一个片段打开片段 - 错误

时间:2015-09-01 20:52:46

标签: android android-fragments

..在其他片段中,这可行。

                Fragment fragment = null;
                fragment = new Fragment_2();
                FragmentManager fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction()
                        .replace(R.id.frame_container, fragment).commit();

但是在onclick事件中

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

public class Fragment1 extends Fragment  implements
Button btnuevaventa;
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
btnuevaventa=(Button)getView().findViewById(R.id.bt_nuevaventa);
        btnuevaventa.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Fragment fragment = null;
                fragment = new Fragment_2();
                FragmentManager fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction()
                        .replace(R.id.frame_container, fragment).commit();
            }
        });

这是activity_main.xml

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Framelayout to display Fragments -->
    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</android.support.v4.widget.DrawerLayout>

logcat的

09-01 16:50:53.039: E/AndroidRuntime(5555): FATAL EXCEPTION: main
09-01 16:50:53.039: E/AndroidRuntime(5555): java.lang.IllegalArgumentException: No view found for id 0x7f09003d (com.example.appcreativeventas:id/frame_container) for fragment Fragment_Inicio_Tab{420f42b0 #1 id=0x7f09003d}
09-01 16:50:53.039: E/AndroidRuntime(5555):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
09-01 16:50:53.039: E/AndroidRuntime(5555):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
09-01 16:50:53.039: E/AndroidRuntime(5555):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
09-01 16:50:53.039: E/AndroidRuntime(5555):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
09-01 16:50:53.039: E/AndroidRuntime(5555):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
09-01 16:50:53.039: E/AndroidRuntime(5555):     at android.os.Handler.handleCallback(Handler.java:615)
09-01 16:50:53.039: E/AndroidRuntime(5555):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-01 16:50:53.039: E/AndroidRuntime(5555):     at android.os.Looper.loop(Looper.java:137)
09-01 16:50:53.039: E/AndroidRuntime(5555):     at android.app.ActivityThread.main(ActivityThread.java:4960)
09-01 16:50:53.039: E/AndroidRuntime(5555):     at java.lang.reflect.Method.invokeNative(Native Method)
09-01 16:50:53.039: E/AndroidRuntime(5555):     at java.lang.reflect.Method.invoke(Method.java:511)
09-01 16:50:53.039: E/AndroidRuntime(5555):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
09-01 16:50:53.039: E/AndroidRuntime(5555):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
09-01 16:50:53.039: E/AndroidRuntime(5555):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

你处理这个错误,你想通过托管它们的Activity加载片段,而不是从其他片段加载...所以你使用一个接口告诉Activity执行代码,因为在这种情况下R.id.fragment_container是目前不是Fragment中的id。尝试类似:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

public class Fragment1 extends Fragment 
    Button btnuevaventa;
    ClickCallback mCallback;
    public interface ClickCallback{
        void buttonClicked();
    }

    @Override 
    public void onAttached(Activity activity){
        super.onAttach(activity);
        mCallback = (ClickCallback) activity;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        btnuevaventa=(Button)getView().findViewById(R.id.bt_nuevaventa);
        btnuevaventa.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                mCallback.buttonClicked();
            }
        });
    }
}

然后在你的Activity中实现接口:

Activity implements ClickCallback

@Override
public void buttonClicked(){
    Fragment fragment = null;
    fragment = new Fragment_2();
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
                    .replace(R.id.frame_container, fragment).commit();

}