我试图在单击按钮时将片段1到片段2的编辑信息获取。
要编辑详细信息(需要首先在Profile
页面上,单击image view
上的CC_2):
- 着陆页是
Profile (Fragment2)
- 单击
Edit
按钮转到Edit Profile (Fragment1)
-
Edit Profile (Fragment1)
将显示 - 编辑信息
- 单击
Done
-
Profile (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);
}
});
}
editProfile(fragment1)
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;
}
}
profile(fragment2)
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中编辑了名称和日期后,名称和日期的文本视图都显示了已编辑的日期,而不是fragment2中的名称和日期。
我编辑详细信息(名称和日期)以及单击按钮,详细信息显示在片段2(配置文件)上。查看Fragment2(配置文件)的详细信息后,我想再次编辑,但是这次我仅编辑了一个细节(即日期)。单击片段1(编辑配置文件)中的按钮后,更新了文本视图(日期),但文本视图(名称)在fragment 2(配置文件)中为空白。
是否可以保存第一个编辑详细信息,并在进行第二次编辑后显示?
当我单击导航抽屉标头以转到 Profile (Fragment2)
如何解决这些问题?
您的个人资料段类应该具有静态方法,该方法接受捆绑包为这样的参数:
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();
您可以使用额外的参数传递参数:
片段1
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();
}
});
片段2
@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;
}