android - fragment.getview() 从 Activity 中返回 null

标签 android android-fragments nullpointerexception android-viewpager

我有一个包含多个 fragment 的 viewpager。其中之一,Frag1,应该接收一个包含数据的对象并用该数据填充一些 EditText。

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

知道为什么它为空吗?

Activity 代码

   (...)

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
                 }
              }
    }

fragment 代码

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 中吗? 现在的方式在我看来是行不通的。

这是一些示例代码:

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

}

希望对您有所帮助。 :)

关于android - fragment.getview() 从 Activity 中返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17684983/

相关文章:

Android文件夹列表,按名称跳过文件夹

android - 如何为自定义 ExpandableListView 调用 notifyDataSetChanged?

java - 为什么 RecyclerView 在其他 Kotlin Android 之前加载

android - Android的位图不为空,但总是使nullpointerexception

java - 尝试在 java 中设置 double 组时出现 NullPointerException

java - 调用 Method.invoke 时获取 java.lang.NullPointerException

Android:添加 ScanCallback 让我 "Unfortunately, App has stopped."找不到 MainActivity

android - 通话或短信电话位置

android - 如何重用 FragmentManager 中的现有 fragment 而不是重新创建它们?

android - Android如何在ViewPager中处理 fragment 生命周期?