将数据从活动传递到片段

时间:2018-03-16 16:17:58

标签: android android-fragments

我在android studio中为用户注册做了一个活动。当用户填写他们的详细信息时,我希望此信息从此注册活动传递到Fragment(称为My Profile)。以便用户可以查看他们的详细信息。 我已经为活动和片段编写了代码,但似乎有错误,因为当我运行应用程序并单击活动中的注册按钮时,它表示我的应用程序已停止工作。请检查我的代码。以下是活动代码:

 public class RegisterScreen extends AppCompatActivity {

EditText firstNameet;
Button register2btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register_screen);
    android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
     final android.support.v4.app.FragmentTransaction t =manager.beginTransaction();
    final MyProfile m4 = new MyProfile();

    register2btn = (Button) findViewById(R.id.register2btn);
    firstNameet = (EditText) findViewById(R.id.firstnameet);

    register2btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Bundle b =new Bundle();
            b.putString("name", firstNameet.getText().toString());
            m4.setArguments(b);
            t.add(R.id.relativelayout1, m4);
            t.commit();
        }
    });
}
}

这是我的片段代码

public class MyProfile extends Fragment {
TextView t1;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    //return inflater.inflate(R.layout.my_profile, null);
    View v= inflater.inflate(R.layout.my_profile, container,false);
    t1 =(TextView) v.findViewById(R.id.firstnameet);
    Bundle bb=getArguments();
    String name=bb.getString("name");
    t1.setText(name);
    return v;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    getActivity().setTitle("My Profile");
}


}

以下是Logcat:

03-16 13:34:07.641 3171-3171/? E/libprocessgroup: failed to make and chown 
/acct/uid_10060: Read-only file system
03-16 13:34:07.641 3171-3171/? W/Zygote: createProcessGroup failed, kernel 
missing CONFIG_CGROUP_CPUACCT?
03-16 13:34:07.641 3171-3171/? I/art: Late-enabling -Xcheck:jni
03-16 13:34:07.672 3171-3171/? W/System: ClassLoader referenced unknown 
path: /data/app/sararafiq94.com.carparkingreservationsystem-1/lib/x86
03-16 13:34:07.684 3171-3171/? W/art: Before Android 4.1, method 
android.graphics.PorterDuffColorFilter 
 android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android        .graphics.PorterDuffColorFilter, android.content.res.ColorStateList, 
    android.graphics.PorterDuff$Mode) would have incorrectly overridden the 
    package-private method in android.graphics.drawable.Drawable
    03-16 13:34:07.778 3171-3184/? D/OpenGLRenderer: Use 
    EGL_SWAP_BEHAVIOR_PRESERVED: true

                                             [ 03-16 13:34:07.782  3171: 
3171 D/         ]
                                             HostConnection::get() New Host 
Connection established 0xe9b60970, tid 3171
03-16 13:34:07.974 3171-3184/sararafiq94.com.carparkingreservationsystem 
D/libEGL: loaded /system/lib/egl/libEGL_emulation.so
03-16 13:34:07.975 3171-3184/sararafiq94.com.carparkingreservationsystem 
D/libEGL: loaded /system/lib/egl/libGLESv1_CM_emulation.so
03-16 13:34:07.981 3171-3184/sararafiq94.com.carparkingreservationsystem 
D/libEGL: loaded /system/lib/egl/libGLESv2_emulation.so


[ 03-16 13:34:07.990  3171: 3184 D/         ]

HostConnection::get() New Host Connection established 0xeea127e0, tid 3184
03-16 13:34:08.026 3171-3184/sararafiq94.com.carparkingreservationsystem 
I/OpenGLRenderer: Initialized EGL, version 1.4
03-16 13:34:08.113 3171-3184/sararafiq94.com.carparkingreservationsystem 
W/EGL_emulation: eglSurfaceAttrib not implemented
03-16 13:34:08.114 3171-3184/sararafiq94.com.carparkingreservationsystem 
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xeea135a0, 
error=EGL_SUCCESS

0 个答案:

没有答案