这个让我疯了.基本上,我想创建一个ViewPager并为其添加一些片段.然后,我想做的就是在Fragment的TextViews中设置一个值.我可以添加Fragments,并且它们会附加,但是当我在第一个Fragment中找到其中一个TextViews的findViewById()时,它会抛出一个NullPointerException.我,为了我的生活,无法弄清楚为什么.
public class SheetActivity extends FragmentActivity { // ///////////////////////////////////////////////////////////////////////// // Variable Declaration // ///////////////////////////////////////////////////////////////////////// private ViewPager viewPager; private PagerTitleStrip titleStrip; private String type; private FragmentPagerAdapter fragmentPager; //UPDATE @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sheet); viewPager = (ViewPager) findViewById(R.id.viewPager); titleStrip = (PagerTitleStrip) findViewById(R.id.viewPagerTitleStrip); // Determine which type of sheet to create Intent intent = getIntent(); this.type = intent.getStringExtra("type"); FragmentManager manager = getSupportFragmentManager(); switch (type) { case "1": viewPager.setAdapter(new InstallAdapter(manager)); break; case "2": viewPager.setAdapter(new InstallAdapter(manager)); break; } fragmentPager = (FragmentPagerAdapter) viewPager.getAdapter(); //UPDATE } @Override public void onResume() { super.onResume(); fragmentPager.getItem(0).setText("something"); //UPDATE } class MyAdapter extends FragmentPagerAdapter { private final String[] TITLES = { "Title1","Title2" }; private final int PAGE_COUNT = TITLES.length; private ArrayList<Fragment> FRAGMENTS = null; public MyAdapter(FragmentManager fm) { super(fm); FRAGMENTS = new ArrayList<Fragment>(); FRAGMENTS.add(new FragmentA()); FRAGMENTS.add(new FragmentB()); } @Override public Fragment getItem(int pos) { return FRAGMENTS.get(pos); } @Override public int getCount() { return PAGE_COUNT; } @Override public CharSequence getPageTitle(int pos) { return TITLES[pos]; } } }
我创建的所有片段只覆盖了onCreateView()方法,因此我可以显示正确的XML布局.除此之外,他们是’股票’.为什么我不能与任何片段中的元素进行交互?
更新:
那么做这样的事情?
public class FragmentA extends Fragment { @Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle inState) { return inflater.inflate(R.layout.fragment_a,container,false); } public void setText(String text) { TextView t = (TextView) getView().findViewById(R.id.someTextView); //UPDATE t.setText(text); } }
分段的XML布局A.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/someTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="22sp" /> </LinearLayout>
解决方法
除非您计划在运行时更改值,否则可以将值作为参数传递给片段.我使用Bundle并将其作为args传递给Fragment,然后从它的args中检索它.
More info here.如果你实现了这个,你的新片段实例化可能如下所示:
public InstallAdapter(FragmentManager fm) { super(fm); FRAGMENTS = new ArrayList<Fragment>(); FRAGMENTS.add(FragmentA.newInstance("<text to set to the TextView>")); FRAGMENTS.add(FragmentB.newInstance("<text to set to the TextView>")); }
但是,如果您计划在运行时更新该值(它将在用户运行应用程序时更改),那么您希望使用接口来管理片段与活动之间的通信. Info here.这可能是这样的:
//Declare your values for activity; ISetTextInFragment setText; ISetTextInFragment setText2; ... //Add interface public interface ISetTextInFragment{ public abstract void showText(String testToShow); } ... //your new InstallAdapter public InstallAdapter(FragmentManager fm) { super(fm); FRAGMENTS = new ArrayList<Fragment>(); Fragment fragA = new FragmentA(); setText= (ISetTextInFragment)fragA; FRAGMENTS.add(fragA); Fragment fragB = new FragmentB(); setText2= (ISetTextInFragment)fragB; FRAGMENTS.add(fragB); } //then,you can do this from your activity: ... setText.showText("text to show"); ...
它将更新片段中的文本视图.
虽然它可以“更容易”完成,但建议使用这些方法,因为它们可以减少错误的机会,并使代码更易读和可维护.
public class FragmentA extends Fragment implements ISetTextInFragment { TextView myTextView; @Override public View onCreateView(LayoutInflater inflater,Bundle inState) { View v = inflater.inflate(R.layout.fragment_a,false); myTextView = (TextView)v.findViewbyId(R.id.someTextView) return v; } @Override public void showText(String text) { myTextView.setText(text); } }
如果之后你仍然得到一个空指针异常,你的TextView不在我需要的地方,即在R.layout.fragment_a文件中,它需要位于那里.当然,除非你在片段完成加载之前调用接口方法.