在FragmentA中单击按钮时如何从Fragment导航到Fragment

时间:2014-04-29 05:35:33

标签: android android-fragments

我有一个用于登录的片段,它有一个按钮,当点击它时应打开register_fragment,同样在注册时它们的按钮在点击时应该从register_fragemnt获得login_fragment。

这是我试过的代码。

public class Login_fragment extends Fragment implements View.OnClickListener{

    Button btnLinkToRegister;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.login_fragment,container, false);

    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        btnLinkToRegister= (Button) getActivity().findViewById(R.id.btnLinkToRegisterScreen);
        btnLinkToRegister.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Fragment register_frag =  new Register_fragment();
        FragmentTransaction transaction = getActivity().getFragmentManager().beginTransaction();
        transaction.replace(R.id.main_activity, register_frag);
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        transaction.addToBackStack(null);
        transaction.commit();
    }



}

Log Cat中显示的消息

04-29 05:25:05.857: E/AndroidRuntime(897): FATAL EXCEPTION: main
04-29 05:25:05.857: E/AndroidRuntime(897): android.content.res.Resources$NotFoundException: Resource ID #0x7f050044 type #0x12 is not valid
04-29 05:25:05.857: E/AndroidRuntime(897):  at android.content.res.Resources.loadXmlResourceParser(Resources.java:2144)
04-29 05:25:05.857: E/AndroidRuntime(897):  at android.content.res.Resources.getLayout(Resources.java:853)
04-29 05:25:05.857: E/AndroidRuntime(897):  at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
04-29 05:25:05.857: E/AndroidRuntime(897):  at com.example.appointment_project.Register_fragment.onCreateView(Register_fragment.java:15)
04-29 05:25:05.857: E/AndroidRuntime(897):  at android.app.Fragment.performCreateView(Fragment.java:1695)
04-29 05:25:05.857: E/AndroidRuntime(897):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
04-29 05:25:05.857: E/AndroidRuntime(897):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
04-29 05:25:05.857: E/AndroidRuntime(897):  at android.app.BackStackRecord.run(BackStackRecord.java:682)
04-29 05:25:05.857: E/AndroidRuntime(897):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
04-29 05:25:05.857: E/AndroidRuntime(897):  at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
04-29 05:25:05.857: E/AndroidRuntime(897):  at android.os.Handler.handleCallback(Handler.java:725)
04-29 05:25:05.857: E/AndroidRuntime(897):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-29 05:25:05.857: E/AndroidRuntime(897):  at android.os.Looper.loop(Looper.java:137)
04-29 05:25:05.857: E/AndroidRuntime(897):  at android.app.ActivityThread.main(ActivityThread.java:5041)
04-29 05:25:05.857: E/AndroidRuntime(897):  at java.lang.reflect.Method.invokeNative(Native Method)
04-29 05:25:05.857: E/AndroidRuntime(897):  at java.lang.reflect.Method.invoke(Method.java:511)
04-29 05:25:05.857: E/AndroidRuntime(897):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-29 05:25:05.857: E/AndroidRuntime(897):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-29 05:25:05.857: E/AndroidRuntime(897):  at dalvik.system.NativeStart.main(Native Method)

显示登录片段,但是当我单击按钮时,我将注册片段,活动崩溃。 请帮助我,我是Android编程的新手。

编辑----- 我在Login_fragment.java中创建了一个由MainActivity.java实现的接口,该方法在类中被覆盖,但我的活动仍然崩溃

MainActivity.java

public class MainActivity extends ActionBarActivity implements SwitchFragments {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // get an instance of FragmentTransaction from your Activity
           FragmentManager fragmentManager = getFragmentManager();
           FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

           //add a fragment
           Login_fragment login_frag= new Login_fragment();
           fragmentTransaction.add(R.id.main_activity,login_frag,"Login Fragment");
           fragmentTransaction.commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void switch_fragmen() {
        // TODO Auto-generated method stub
        Register_fragment register = new Register_fragment();
        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.main_activity,register);
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        transaction.addToBackStack(null);
        transaction.commit();
        }



}

LoginFragment.java

public class Login_fragment extends Fragment implements View.OnClickListener{

    Button btnLinkToRegister;
    SwitchFragments switchFragments;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.login_fragment,container, false);

    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        btnLinkToRegister= (Button) getActivity().findViewById(R.id.btnLinkToRegisterScreen);
        btnLinkToRegister.setOnClickListener(this);
    }
    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        switchFragments = (SwitchFragments) activity;
    }
    @Override
    public void onClick(View v) {

        switchFragments.switch_fragmen();

    }

    public interface SwitchFragments{
        public void switch_fragmen();
    }

}

可能是什么问题?

2 个答案:

答案 0 :(得分:0)

Resources$NotFoundException

如果您正在访问Activity的{​​{1}}布局,那么您无法访问。

Fragment

您似乎通过Fragment类管理transaction.replace(R.id.main_activity, register_frag); 的问题。 这不是一个好方法。

最好尝试从FragmentTransaction本身替换Fragment

编辑:----------------------

Activity

在MainActivity中实现此界面。

然后

public class Register_fragment extends Fragment implements View.OnClickListener{
    Button login;
    Communicate com;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        com = (Communicate) activity;
    }
    // Created an Interface
    interface Communicate{
        public void changeLayout();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        login =// initiliza here
    }

    @Override
    public void onClick(View v) {
        com.changeLayout();

    }


}

必需修复:

尝试在@Override public void changeLayout() { Register_fragment register_frag = new Register_fragment(); android.app.FragmentTransaction transaction = getFragmentManager().beginTransaction(); transaction.replace(R.id.main_activity, register_frag); transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); transaction.addToBackStack(null); transaction.commit(); }

中初始化
onCreateView

答案 1 :(得分:0)

试试这个

FragmentManager fm = getActivity().getFragmentManager();
fm.beginTransaction().replace(R.id.main_activity,
                        new RegisterFragment(), "registerFragment")
                        .addToBackStack("register").commit();