fragment.getview()从activity返回null

时间:2013-07-16 19:07:22

标签: android android-fragments nullpointerexception android-viewpager

我有一个托管多个片段的viewpager。其中一个,Frag1,应该用数据接收一个对象,并用该数据弹出一些EditText。

问题是,当我尝试从活动中访问片段时,getView()始终返回null,从而无法调用Frag1类的方法。我正在使用LogCat来检查其他变量是否为null,但似乎问题来自getView()检查。

知道为什么它是null吗?

活动代码

   (...)

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

        mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());

        final ActionBar actionBar = getActionBar();

        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowTitleEnabled(false);

        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mAppSectionsPagerAdapter);
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                // When swiping between different app sections, select the corresponding tab.
                // We can also use ActionBar.Tab#select() to do this if we have a reference to the
                // Tab.
                actionBar.setSelectedNavigationItem(position);
            }
        });

public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {

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

        @Override
        public Fragment getItem(int i) {
            switch (i) {
                case 0:
                    return new Frag1();

                case 1:
                    return new Frag2();

                case 2:
                    return new Frag1();

                case 3:
                    return new Frag1();

                default:
                    return new Frag1();
            }
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            return "Section " + (position + 1);
        }
    }


    public void Populate(Patient patient) {
        Frag1 fragment = 
                  (Frag1) mAppSectionsPagerAdapter.getItem(0);

        Log.d(patient.getName(), "data check");
        Log.d(fragment.hello(), "fragment check");

        if (fragment.getView() == null) {
            Log.d("*** DEBUG ***", "found the problem champ");
        }

              if(fragment != null)  // could be null if not instantiated yet
              {
                 if(fragment.getView() != null) 
                 {
                    // no need to call if fragment's onDestroyView() 
                    //has since been called.
                    fragment.setName(patient.getName());
                    Log.d(patient.getName(), "supbrah");// do what updates are required
                 }
              }
    }

片段代码

public class Frag1 extends Fragment {

EditText name;
Spinner spinner;
AutoCompleteTextView textView;
AutoCompleteTextView textView1;
Spinner spinner1;
AutoCompleteTextView textView2;
View v;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
    v = inflater.inflate(R.layout.activity_form, container, false);

    name = (EditText) v.findViewById(R.id.name_edit);

    return v;
}

public String hello() {
    return "sup brah";
}

public String getName() {
    String patient = name.getText().toString();
    return patient;
}

public void setName(String text) {
    name.setText(text, TextView.BufferType.EDITABLE);
}

}

2 个答案:

答案 0 :(得分:5)

如果我错了,请纠正我,但是你不应该使用getView().findViewById(R.id.name_edit);并将其放入onViewCreated吗? 它现在的方式不应该起作用。

这是一些示例代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    if (container == null) {
        return null;
    }
    return inflater.inflate(R.layout.activity_form, container, false);

}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onViewCreated(view, savedInstanceState);

name = (EditText) getView().findViewById(R.id.name_edit);

}

希望它有所帮助。 :)

答案 1 :(得分:1)

尝试调用viewPager.getChildAt(int x);而不是viewPagerAdapter.getItem(int x).getView();