gpt4 book ai didi

android - onClick 内部 fragment 给我 NPE

转载 作者:行者123 更新时间:2023-11-29 21:50:47 25 4
gpt4 key购买 nike

我已经为此纠结了很长一段时间。

我从使用常规 Activity 切换到现在使用 fragment (根据用户的建议),但我遇到了同样的问题。

我有 3 个页面,现在是 ViewPager 中的 3 个 fragment 。我想要 addSite fragment 中带有 id addSiteButton 的按钮;单击时,滚动到 setCurrentItem(2)。

感谢任何帮助!这是我的代码。

fragment Activity

public class fieldsActivity extends FragmentActivity {


private static final int NUMBER_OF_PAGES = 3;

ViewPager mPager;
MyFragmentPagerAdapter mMyFragmentPagerAdapter;


/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// to create a custom title bar for activity window
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

setContentView(R.layout.fields);
// use custom layout title bar
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.topbar);

mPager = (ViewPager) findViewById(R.id.fieldspager);
mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mMyFragmentPagerAdapter);
mPager.setCurrentItem(1);


}


private static class MyFragmentPagerAdapter extends FragmentPagerAdapter {

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

@Override
public Fragment getItem(int page) {

switch (page) {

case 0: return new settingFields();

case 1: return new addSite();

case 2: return new createSite();

}

return null;


}

@Override
public int getCount() {

return NUMBER_OF_PAGES;
}

}

带有按钮的类

public class addSite extends Fragment {


public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment

if (container == null) {
return null;
}

RelativeLayout mRelativeLayout = (RelativeLayout) inflater.inflate(R.layout.add_site, container, false);
final ViewPager mPager = (ViewPager) mRelativeLayout.findViewById(R.id.fieldspager);
Button addSiteButton = (Button) mRelativeLayout.findViewById(R.id.addSiteButton);
addSiteButton.setOnClickListener(new View.OnClickListener() {


@Override
public void onClick(View view) {

mPager.setCurrentItem(2, true);

}
});

return mRelativeLayout;

}
}

我感觉自己在原地打转,没有得到想要的 Action 。

感谢您的帮助!

最佳答案

只需将 Fragment 作为 FragmentActivity 中的一个类,并在 onCreateView() 中移除变量阴影,因为您的寻呼机是一个全局变量。

public class fieldsActivity extends FragmentActivity {


private static final int NUMBER_OF_PAGES = 3;

ViewPager mPager;
MyFragmentPagerAdapter mMyFragmentPagerAdapter;


/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// to create a custom title bar for activity window
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

setContentView(R.layout.fields);
// use custom layout title bar
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.topbar);

mPager = (ViewPager) findViewById(R.id.fieldspager);
mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mMyFragmentPagerAdapter);
mPager.setCurrentItem(1);


}

public static class addSite extends Fragment {


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

RelativeLayout mRelativeLayout = (RelativeLayout) inflater.inflate(R.layout.add_site, container, false);
//final ViewPager mPager = (ViewPager) mRelativeLayout.findViewById(R.id.fieldspager);
Button addSiteButton = (Button) mRelativeLayout.findViewById(R.id.addSiteButton);
addSiteButton.setOnClickListener(new View.OnClickListener() {


@Override
public void onClick(View view) {

mPager.setCurrentItem(2, true);

}
});

return mRelativeLayout;

}
}
}

还可以考虑在覆盖方法时使用 @Override 注释,这样您就不会犯错误并使用正确的命名约定。 Java 类以大写开头。

关于android - onClick 内部 fragment 给我 NPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14468712/

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