簡體   English   中英

fragment.getview()從活動返回null

[英]fragment.getview() returning null from activity

我有一個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);
}

}

如果我錯了,請糾正我,但您不應該使用getView().findViewById(R.id.name_edit); 並將其放在onViewCreated中? 現在,它的方式不適用於imo。

這里是一些示例代碼:

@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);

}

希望能幫助到你。 :)

嘗試調用viewPager.getChildAt(int x); 而不是viewPagerAdapter.getItem(int x).getView();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM