gpt4 book ai didi

android - 无法使用 ViewPager 和 FragmentPagerAdapter 访问 fragment View

转载 作者:行者123 更新时间:2023-11-29 16:49:07 24 4
gpt4 key购买 nike

试图通过摸索学习Android开发,如果问题太简单,请原谅。

我有以下代码由于 NullPointerException 而不断崩溃 - 我已经尝试了几种方法来访问 fragment 的 View ,但我总是得到 null。谁能帮我解决我做错的事情?

我想做的是在滚动页面时将 EditText 的内容保存到 SharedPreferences。对于我的生活,我似乎无法访问显示选项卡时加载的 fragment 的 View 。

public class ViewPagerAdapter extends FragmentPagerAdapter {

final int PAGE_COUNT = 3;
// Tab Titles
private String tabtitles[] = new String[] { "Profile", "Address",
"Security"};
Context context;

public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public int getCount() {
return PAGE_COUNT;
}

@Override
public Fragment getItem(int position) {
switch (position) {
// Open FragmentTab1.java
case 0:
CreateUserFragmentProfile fragmenttab1 = new
CreateUserFragmentProfile();
return fragmenttab1;
// Open FragmentTab2.java
case 1:
CreateUserFragmentLocation fragmenttab2 = new
CreateUserFragmentLocation();
return fragmenttab2;
// Open FragmentTab3.java
case 2:
CreateUserFragmentSecurity fragmenttab3 = new
CreateUserFragmentSecurity();
return fragmenttab3;

}
return null;
}

@Override
public CharSequence getPageTitle(int position) {
return tabtitles[position];
}

}

Fragment 代码扩展了 Fragment,几乎什么都不做。

public class CreateUserFragmentSecurity extends Fragment {

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup
container, @Nullable Bundle savedInstanceState) {
//return super.onCreateView(inflater, container,
savedInstanceState);
View view = inflater.inflate(R.layout.create_fragment_security,
container, false);


return view;

}

public void storeData(){

}
}

Activity代码如下:

public class CreateUser extends AppCompatActivity {


public SharedPreferences preferences;
public SharedPreferences.Editor editor;
public View currView;
public String fname, lname, dob, gender, email, addr1, addr2, addr3,
addrstate, city, passwd, passwdPlain;
public Long mobile, pin;
//public FragmentPagerAdapter adapter;

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

preferences = getSharedPreferences("Reviv", MODE_PRIVATE);
editor = preferences.edit();

// Locate the viewpager in activity_create_user.xml
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);

// Set the ViewPagerAdapter into ViewPager

final FragmentPagerAdapter adapter = new
ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(3);

viewPager.addOnPageChangeListener(new
ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
boolean hasChanged = false;
switch (position){ // store data on tab switch
case 0: // Profile tab
//currView =
viewPager.getChildAt(viewPager.getCurrentItem()); // returns
null
currView =
adapter.getItem(viewPager.getCurrentItem()).getView(); //
crashes, returns null
EditText etFname =
currView.findViewById(R.id.etFname);


fname = etFname.getText().toString();

hasChanged = false;

if(!StringUtils.isEmpty(fname)) {
editor.putString("fname", fname);
hasChanged = true;
}

if(hasChanged == true) editor.commit();

break;
case 1: // Address tab
// currView =
viewPager.getChildAt(viewPager.getCurrentItem()); // returns
null
currView =
adapter.getItem(viewPager.getCurrentItem()).getView();
EditText etAddr1 =
currView.findViewById(R.id.etAddr1);



addr1 = etAddr1.getText().toString();


hasChanged = false;

if(!StringUtils.isEmpty(addr1)) {
editor.putString("addr1", addr1);
hasChanged = true;
}


if(hasChanged == true) editor.commit();


break;
case 2: // Security tab
//currView =
viewPager.getChildAt(viewPager.getCurrentItem()); // returns null
currView =
adapter.getItem(viewPager.getCurrentItem()).getView();

EditText etPasswd =
currView.findViewById(R.id.etPasswd);


passwd = etPasswd.getText().toString();

hasChanged = false;

if(!StringUtils.isEmpty(passwd)) {
editor.putString("passwd", passwd);
hasChanged = true;
}



if(hasChanged == true) editor.commit();

break;
}

}

@Override
public void onPageSelected(int position) {
// do nothing
}

@Override
public void onPageScrollStateChanged(int state) {
// do nothing
}
});
}
}

我的堆栈轨迹如下:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference at com.portmanteau.reviv.CreateUser$1.onPageScrolled(CreateUser.java:58) at android.support.v4.view.ViewPager.dispatchOnPageScrolled(ViewPager.java:1921) at android.support.v4.view.ViewPager.onPageScrolled(ViewPager.java:1895) at android.support.v4.view.ViewPager.pageScrolled(ViewPager.java:1833) at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:690) at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1777) at android.view.View.layout(View.java:19678) at android.view.ViewGroup.layout(ViewGroup.java:6057) at android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1197) at android.view.View.layout(View.java:19678) at android.view.ViewGroup.layout(ViewGroup.java:6057) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at android.view.View.layout(View.java:19678) at android.view.ViewGroup.layout(ViewGroup.java:6057) at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:434) at android.view.View.layout(View.java:19678) at android.view.ViewGroup.layout(ViewGroup.java:6057) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at android.view.View.layout(View.java:19678) at android.view.ViewGroup.layout(ViewGroup.java:6057) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635) at android.widget.LinearLayout.onLayout(LinearLayout.java:1544) at android.view.View.layout(View.java:19678) at android.view.ViewGroup.layout(ViewGroup.java:6057) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at com.android.internal.policy.DecorView.onLayout(DecorView.java:758) at android.view.View.layout(View.java:19678) at android.view.ViewGroup.layout(ViewGroup.java:6057) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2510) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2219) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1405) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6829) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:966) at android.view.Choreographer.doCallbacks(Choreographer.java:778) at android.view.Choreographer.doFrame(Choreographer.java:713) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:952) at android.os.Handler.handleCallback(Handler.java:789) at android.os.Handler.dispatchMessage(Handler.java:98) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6803) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

最佳答案

尝试这样的事情:

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
boolean hasChanged = false;
if (positionOffset > 0) {
currView = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager + ":" + position).getView();
if (currView != null) {
EditText editText = null;
if (position == 0) editText = currView.findViewById(R.id.etPasswdPlain);
else if (position == 1) editText = currView.findViewById(R.id.etAddr1);
else if (position ==2) editText = currView.findViewById(R.id.etFname);
if (editText != null) {
String value = editText.getText().toString();
Log.d("TAG", "Value is " + value);
}
}
}
}

请注意,这仅在您从左向右滑动(从第 1 页到第 2 页以及从第 2 页到第 3 页)时才有效。

即当您从第 1 页滑动到第 2 页时,第 1 页 中的数据将被保存并

当您从第2页滑动到第3页时,第2页中的数据将被保存。

对于从右向左滑动,假设从第3页滑动到第2页时必须保存第3页数据并保存>page 2 数据从page 2 滑动到page 1,你必须做一些逻辑,但你可以从这里得到想法。

关于android - 无法使用 ViewPager 和 FragmentPagerAdapter 访问 fragment View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46862902/

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