ViewPager的TextView抛出空指针异常

时间:2016-07-16 11:37:04

标签: android textview android-viewpager nullreferenceexception

我有一个包含ViewPager的片段。在ViewPager中我有4个内容屏幕,因此可以在其中滑动。内容有一些TextView组件,我试图从第一个片段访问它们,但是当我去使用setText或类似时,我得到一个空指针异常。我已经检查过,TextView的id是有效的,我做了一个干净的构建。

以下是我的相关代码片段:

private TextView textViewOne;

private PagerAdapter mPagerAdapter;
private ViewPager pager;
View rootView;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_layout_viewpager, container, false);

    InitializePaging();

    textViewOne = (TextView) rootView.findViewById(R.id.create_name);

    //would return null if I did this:
    textViewOne.setText("test");
    return rootView;
}

private void InitializePaging() {
    mPagerAdapter = new ViewPagerAdapter(getContext(), getChildFragmentManager(),
            Arrays.asList(PageOne.class, PageTwo.class, PageThree.class, PageFour.class));

    pager = (ViewPager) rootView.findViewById(R.id.content_pager);
    pager.setAdapter(mPagerAdapter);
}

PageOne课程:

public class PageOne extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if(container == null) {
        return null;
    }
        return (LinearLayout) inflater.inflate(R.layout.fragment_page_one, container, false);
    }
}

这是fragment_layout_viewpager xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
        <android.support.v4.view.ViewPager
            android:id="@+id/content_pager"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

您正在膨胀func click(sender: UIButton) { print("click") } ,因此fragment_layout_viewpager应存在于该布局中,否则会抛出空指针。

答案 1 :(得分:0)

Create an interface with 1 abstract method. Let the Fragment implement the interface and call the callback method from Activity.In the below example, I try to pass value on click of button from MainActivity to ThirdFragment.

public interface FragmentCommunicator { 
    public void passDataToFragment(String someValue);
 }

  public class MainActivity extends FragmentActivity {
    FragmentCommunicator fragmentCommunicator;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button btn= (Button)findViewById(R.id.button) ;
        btnsetOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              randomvalue= getRansomString();
                if(fragmentCommunicator!=null)
                fragmentCommunicator.passDataToFragment(randomvalue);
            }
        });
        ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
        pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
    }
    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }
        @Override
        public Fragment getItem(int pos) {
            switch(pos) {
                case 0: return FirstFragment.newInstance("FirstFragment, Instance 1");
                case 1: return ThirdFragment.newInstance("ThirdFragment, Instance 1");
                default: return FirstFragment.newInstance("FirstFragment,Instance 1");
}
        }
        @Override
        public int getCount() {
            return 2;
        }
    }
    public static class FirstFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.first_frag, container, false);
            TextView tv = (TextView) v.findViewById(R.id.tvFragFirst);
            tv.setText(getArguments().getString("msg"));
            return v;
        }
        public static FirstFragment newInstance(String text) {
            Log.e("FirstFragment","newInstance called");
            FirstFragment f = new FirstFragment();
            Bundle b = new Bundle();
            b.putString("msg", text);
            f.setArguments(b);
            return f;
        }
    }
    public class ThirdFragment extends Fragment implements FragmentCommunicator {
    TextView tv;
    String value="";
    public FragmentCommunicator fragmentCommunicator;
    @Override
    public void passDataToFragment(String someValue) {
        this.value = someValue;
        tv.setText(value);
    }
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {           
            ((MainActivity)activity).fragmentCommunicator = this;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement FragmentCommunicator");
        }
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.first_frag, container, false);
        value=getArguments().getString("msg");
        tv = (TextView) v.findViewById(R.id.tvFragFirst);
        tv.setText(value);
        return v;
    }
    public static ThirdFragment newInstance(String text) {
        ThirdFragment f = new ThirdFragment();
        Bundle b = new Bundle();
        b.putString("msg",text);
        f.setArguments(b);
        return f;
    }
}