从另一个活动替换片段。 java.lang.IllegalArgumentException:找不到id的视图

时间:2014-09-08 08:04:01

标签: android android-fragments

我想导航到活动中的片段。 在导航抽屉中使用两个片段FragmentA和FragmentB。 请在下面找到代码:

FragmentA.java

public class FragmentA extends Fragment{

Button btnContinue;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragA, container, false);
    btnRegister=(Button)rootView.findViewById(R.id.btn_register);
    btnRegister.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        Intent i =new Intent(getActivity(),Register.class);
        startActivity(i);

       }
     });

    return rootView;
  }
}

Register.java是另一项活动,我点击了保存详情按钮。

btnSave.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        new Save_register_detail_task(Register.this,newfirstname, newlastname, newemail, newpassword).execute();

       }
     });

Save_register_detail_task.java(AsyncTask)。 在其onPostExecute中,我有以下代码: R.id.frame_container在导航抽屉的布局中定义。

Fragment fragment = new FragmentB();
            fragment.setArguments(bundle);  
            //activity.finish();
            FragmentManager fragmentManager = ((FragmentActivity)activity).getSupportFragmentManager();

            FragmentTransaction fragmentTransaction = fragmentManager
                    .beginTransaction();
            fragmentTransaction.replace(R.id.frame_container,
                    fragment, "fragb")
                    .addToBackStack("register")
                    .commit();

的logcat -

09-08 13:24:21.381: E/FragmentManager(32202): No view found for id 0x7f050143     (com.demo.projectdemo:id/frame_container) for fragment FragmentB{429831a8 #0 id=0x7f050143 fragb}
09-08 13:24:21.381: E/FragmentManager(32202): Activity state:
09-08 13:24:21.431: E/AndroidRuntime(32202): FATAL EXCEPTION: main
09-08 13:24:21.431: E/AndroidRuntime(32202): java.lang.IllegalArgumentException: No view   found for id 0x7f050143 (com.demo.projectdemo:id/frame_container) for fragment  FragmentB{429831a8 #0 id=0x7f050143 fragb}
09-08 13:24:21.431: E/AndroidRuntime(32202):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:930)
09-08 13:24:21.431: E/AndroidRuntime(32202):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
09-08 13:24:21.431: E/AndroidRuntime(32202):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
09-08 13:24:21.431: E/AndroidRuntime(32202):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
09-08 13:24:21.431: E/AndroidRuntime(32202):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
09-08 13:24:21.431: E/AndroidRuntime(32202):    at android.os.Handler.handleCallback(Handler.java:615)
09-08 13:24:21.431: E/AndroidRuntime(32202):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-08 13:24:21.431: E/AndroidRuntime(32202):    at android.os.Looper.loop(Looper.java:137)
09-08 13:24:21.431: E/AndroidRuntime(32202):    at android.app.ActivityThread.main(ActivityThread.java:4960)
09-08 13:24:21.431: E/AndroidRuntime(32202):    at java.lang.reflect.Method.invokeNative(Native Method)
09-08 13:24:21.431: E/AndroidRuntime(32202):    at java.lang.reflect.Method.invoke(Method.java:511)
09-08 13:24:21.431: E/AndroidRuntime(32202):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)

我应该用什么代替R.id.frame_container?或者是否有任何其他方式来声明片段管理器?

0 个答案:

没有答案