gpt4 book ai didi

java - Android Studio : How to get EditText values from Fragment 1 to Fragment 2 respectively on button clicked

转载 作者:太空宇宙 更新时间:2023-11-04 10:54:02 25 4
gpt4 key购买 nike

当单击按钮时,我试图获取从 fragment 1 到 fragment 2 的编辑信息。

要编辑详细信息(需要首先进入个人资料页面,通过单击抽屉导航标题处的 ImageView ):

  1. 着陆页是个人资料 (Fragment2)
  2. 点击编辑按钮前往编辑个人资料(Fragment1)
  3. 将显示编辑个人资料(Fragment1)
  4. 编辑信息
  5. 点击完成
  6. 个人资料(Fragment2)将显示已编辑的详细信息

这是代码:

MainActivity,其中 Fragment2 可以使用抽屉导航标题打开

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

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// ActionBar actionBar = getSupportActionBar();
// actionBar.setDisplayShowHomeEnabled(true);
// actionBar.setIcon(R.mipmap.ic_launcher);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});

final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View header_view = navigationView.getHeaderView(0);
ImageView img = (ImageView) header_view.findViewById(R.id.profile_image);
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
FragmentManager fragmentManager = getFragmentManager();fragmentManager.beginTransaction()
.replace(R.id.content_frame
, profile)
.commit();
drawer.closeDrawer(GravityCompat.START);
}
});

}

编辑配置文件( fragment 1)

public class EditProfile extends Fragment implements OnClickListener {
View myView; //A view object called myView

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.editprofile, container, false);
Button btn = (Button) myView.findViewById(R.id.btnEPDone);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
EditText editName = (EditText) myView.findViewById(R.id.editName);
EditText editDate = (EditText)myView.findViewById(R.id.editDOB);

Profile profile = new Profile();
Bundle args = new Bundle();
args.putString("name", editName.getText().toString());
args.putString("date", editDate.getText().toString());
profile.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();fragmentManager.beginTransaction()
.replace(R.id.content_frame
, profile)
.commit();
}
});
return myView;
}
}

配置文件( fragment 2)

public class Profile extends Fragment implements View.OnClickListener{
View myView; //A view object called myView

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.profile, container, false);
TextView txtName = (TextView) myView.findViewById(R.id.viewName);
txtName.setText(getArguments().getString("name"));
TextView txtDate = (TextView) myView.findViewById(R.id.viewDOB);
txtDate.setText(getArguments().getString("date"));
Button btn = (Button) myView.findViewById(R.id.btnVPDone);
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
FragmentManager fragmentManager = getFragmentManager();fragmentManager.beginTransaction()
.replace(R.id.content_frame
, new EditProfile())
.commit();
}
});
return myView;
}
}

Logcat

    11/26 16:19:41: Launching app
$ adb install-multiple -r -t -p com.example.hci /Users/Desktop/HCI/app/build/intermediates/split-apk/debug/slices/slice_9.apk
Split APKs installed
$ adb shell am start -n "com.example.hci/com.example.hci.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Connected to process 4411 on device emulator-5554
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/zygote: Not late-enabling -Xcheck:jni (already on)
W/zygote: Unexpected CPU variant for X86 using defaults: x86
I/InstantRun: starting instant run server: is main process
D/OpenGLRenderer: HWUI GL Pipeline
I/zygote: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
D/OpenGLRenderer: Swap behavior 0
D/EGL_emulation: eglCreateContext: 0x944c8f00: maj 2 min 0 rcv 2
D/EGL_emulation: eglMakeCurrent: 0x944c8f00: ver 2 0 (tinfo 0xaf637820)
D/EGL_emulation: eglMakeCurrent: 0x944c8f00: ver 2 0 (tinfo 0xaf637820)
I/zygote: Do partial code cache collection, code=11KB, data=20KB
I/zygote: After code cache collection, code=11KB, data=20KB
I/zygote: Increasing code cache capacity to 128KB
I/zygote: Do partial code cache collection, code=11KB, data=38KB
I/zygote: After code cache collection, code=11KB, data=38KB
I/zygote: Increasing code cache capacity to 256KB
I/zygote: JIT allocated 71KB for compiled code of void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int)
I/zygote: Compiler allocated 4MB to compile void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int)
I/zygote: Do full code cache collection, code=91KB, data=57KB
I/zygote: After code cache collection, code=89KB, data=43KB
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.hci, PID: 4411
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
at com.example.hci.Profile.onCreateView(Profile.java:31)
at android.app.Fragment.performCreateView(Fragment.java:2508)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1279)
at android.app.FragmentManagerImpl.addAddedFragments(FragmentManager.java:2407)
at android.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2186)
at android.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2142)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2043)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:719)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Application terminated.

[已解决]在Fragment1中编辑名称和日期后,名称和日期的 TextView 都显示编辑的日期,而不是Fragment2中分别显示的名称和日期。

我编辑详细信息(名称和日期),单击按钮后,详细信息将显示在 fragment 2(个人资料)上。查看fragment2(个人资料)的详细信息后,我想再次编辑,但这次我只编辑了一个详细信息(即日期)。单击 fragment 1(编辑个人资料)中的按钮后, TextView (日期)会更新,但 fragment 2(个人资料)中的 TextView (名称)为空白。

是否可以保存第一次编辑详细信息,并在进行第二次编辑后显示它?

当我单击抽屉导航标题转到个人资料 (Fragment2) 时,应用程序崩溃

如何解决这些问题?

最佳答案

您的 ProfileFragment 类应该具有接受 Bundle 作为参数的静态方法,如下所示:

public static ProfileFragment newInstance(@Nullable Bundle bundle) {
ProfileFragment fragment = new ProfileFragment();
fragment.setArguments(bundle);
return fragment;
}

在您的 EditProfileFragment 中,您应该调用此:

ProfileFragment profile = ProfileFragment.newInstance(bundleValues);
FragmentManager fragmentManager = getFragmentManager();fragmentManager.beginTransaction()
.replace(R.id.content_frame
, profile)
.commit();

关于java - Android Studio : How to get EditText values from Fragment 1 to Fragment 2 respectively on button clicked,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47493493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com